Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git command to get HEAD SHA1 with "-dirty" suffix if workspace is not clean

Tags:

git

I'm working to add a version number to a build process. For versions built on developer machines, I want to know whether the workspace contained changes that may have been included in the build, and suffix the SHA1 with -dirty.

For example:

c2bc0d223739c841c5e810c6c439562aa9d67f5f
c2bc0d223739c841c5e810c6c439562aa9d67f5f-dirty

The first form can be obtained via:

git rev-parse HEAD

However I cannot work out how to append the -dirty suffix when the workspace is dirty. git describe provides this, but only on tag names. We don't really use tags (the last was 400 or so revisions ago) and they're not as conclusive as the SHA1.

Can this be done in a single command or will I need to do some bash scripting?

like image 304
Drew Noakes Avatar asked Jan 09 '14 10:01

Drew Noakes


People also ask

How do I find the commit ID?

To find a git commit id (or hash), you can simply use the git log command. This would show you the commit history, listing the commits in chronological order, with the latest commit first.


2 Answers

You can use --match with something that's never going to match to force describe to not match any tags. It's a bit hackish but it works:

git describe --match=NeVeRmAtCh --always --abbrev=40 --dirty
like image 156
CB Bailey Avatar answered Oct 11 '22 08:10

CB Bailey


This worked for me. I took the accepted answer and found the bare minimum version that worked.

git describe --always --dirty
like image 1
frankgreco Avatar answered Oct 11 '22 08:10

frankgreco