Git command to display HEAD commit id?

GitVersion Control

Git Problem Overview


What command can I use to print out the commit id of HEAD?

This is what I'm doing by hand:

$ cat .git/HEAD
ref: refs/heads/v3.3
$ cat .git/refs/heads/v3.3
6050732e725c68b83c35c873ff8808dff1c406e1

But I need a script that can reliably pipe the output of some command to a text file such that the text file contains exactly the commit id of HEAD (nothing more or less, and not just a ref). Can anyone help?

Git Solutions


Solution 1 - Git

Use the command:

git rev-parse HEAD

For the short version:

git rev-parse --short HEAD

Solution 2 - Git

git log -1

for only commit id

git log | head -n 1 

Solution 3 - Git

Old thread, still for future reference...:) even following works

git show-ref --head

by default HEAD is filtered out. Be careful about following though ; plural "heads" with a 's' at the end. The following command shows branches under "refs/heads"

 git show-ref --heads

Solution 4 - Git

You can specify git log options to show only the last commit, -1, and a format that includes only the commit ID, like this:

git log -1 --format=%H

If you prefer the shortened commit ID:

git log -1 --format=%h

Solution 5 - Git

Play with Bash:

git show HEAD | sed -n 1p | cut -d " " -f 2

Solution 6 - Git

git rev-parse --abbrev-ref HEAD

Solution 7 - Git

You can use this command

$ git rev-list HEAD

You can also use the head Unix command to show the latest n HEAD commits like

$ git rev-list HEAD | head - 2

Solution 8 - Git

You can use

git log -g branchname

to see git reflog information formatted like the git log output along with commit id.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew ArnottView Question on Stackoverflow
Solution 1 - GitRandal SchwartzView Answer on Stackoverflow
Solution 2 - Gitcyb0kView Answer on Stackoverflow
Solution 3 - GitAbhijit MazumderView Answer on Stackoverflow
Solution 4 - GitJotaBeView Answer on Stackoverflow
Solution 5 - GitAli MorenoView Answer on Stackoverflow
Solution 6 - GitAvdhut MankavaleView Answer on Stackoverflow
Solution 7 - GitmedmikView Answer on Stackoverflow
Solution 8 - GitStuti VermaView Answer on Stackoverflow