Easily get the first 7 letters of you commit_SHA!

let commit_SHA = '';
// add your commit_SHA between the '' marks and run the code...

let commitArray = commit_SHA.split('');
let w = commitArray.slice(0, 7).toString().split(',').join('');

// Example:
// let commit_SHA = '5d692065cf51a2f50ea8e7b19b5a7ae512f633ba';
// logs: 5d69206

I mean, I presume that you’re asking Git for the commit history in the first place to paste it into your code… so you could just ask Git “pretty please” and have it do the legwork for you. :slight_smile:

git log --pretty="%h %s"

That will produce one line per commit, with the abbreviated hash (which you’re after) followed by the commit comment, e.g:

# this is fabricated output, to give you the general idea...
$ git log --pretty="%h %s"
1c75832 added aboutme.html
846c3aa changed background colour to blue
1938d3c typo on resume.html

That being said, points for having a problem and cracking it with a coding solution. :slight_smile:


yeah just found out you can easily get that with
$ git log --oneline

but at the time I did not know such commands existed and I was having a hard time counting 7 from the 40 characters…

1 Like