In Git, how do I figure out what my current revision is?

Git

Git Problem Overview


I just want to know what my current version number is.

Git Solutions


Solution 1 - Git

What do you mean by "version number"? It is quite common to tag a commit with a version number and then use

$ git describe --tags

to identify the current HEAD w.r.t. any tags. If you mean you want to know the hash of the current HEAD, you probably want:

$ git rev-parse HEAD

or for the short revision hash:

$ git rev-parse --short HEAD

It is often sufficient to do:

$ cat .git/refs/heads/${branch-main}

but this is not reliable as the ref may be packed.

Solution 2 - Git

There are many ways git log -1 is the easiest and most common, I think

Solution 3 - Git

This gives you just the revision.

git rev-parse HEAD

Solution 4 - Git

This gives you the first few digits of the hash and they are unique enough to use as say a version number.

git rev-parse --short HEAD

Solution 5 - Git

below will work with any previously pushed revision, not only HEAD

for abbreviated revision hash:

git log -1 --pretty=format:%h

for long revision hash:

git log -1 --pretty=format:%H

Solution 6 - Git

For a simple way to output the current commit that includes the version number, I use:

git show $(git rev-parse HEAD)

From there I can parse the output as required.

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
QuestionTIMEXView Question on Stackoverflow
Solution 1 - GitWilliam PursellView Answer on Stackoverflow
Solution 2 - GitmanojldsView Answer on Stackoverflow
Solution 3 - GithtanataView Answer on Stackoverflow
Solution 4 - GitKen McConnellView Answer on Stackoverflow
Solution 5 - GitAlexey BychkoView Answer on Stackoverflow
Solution 6 - GitJohn.MView Answer on Stackoverflow