83

I want to display the git commit id (i.e. SHA) of the head of master on my website as an identifier.

How can I pull this information from git?

Jordan Dea-Mattson
  • 5,791
  • 5
  • 38
  • 53
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

5 Answers5

134

This command will get you the latest commit's SHA-1

git rev-parse HEAD
Jossef Harush Kadouri
  • 32,361
  • 10
  • 130
  • 129
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
  • 9
    +1: I think this "plumbing" approach is better for a scripting-based solution as required by the OP, rather than my "porcelain" approach that I suggest below (i.e., "`git log ...`"). – Jeet Feb 17 '11 at 22:11
  • 1
    How to get the latest commit's SHA-1 on **master** branch – andy Aug 29 '16 at 06:55
  • 4
    *[andy](http://stackoverflow.com/users/1889327/andy)*: `git rev-parse master` will get you the result. – Alan Haggai Alavi Aug 30 '16 at 01:10
  • @AlanHaggaiAlavi this works in local git repo, what should be used in a repo which is cloned from remote server like github – Kasun Siyambalapitiya Nov 28 '16 at 10:14
  • 1
    *[Kasun Siyambalapitiya](http://stackoverflow.com/users/5105305/kasun-siyambalapitiya)*: This should work on any Git repository. You could use `git rev-parse master` or `git rev-parse origin/master` or any other reference name you want the commit ID of. – Alan Haggai Alavi Nov 28 '16 at 22:15
39

Instead of the HEAD SHA1, I would rather go with git describe, as a more readable way to get a "build id". For instance:

git describe --abbrev=4 HEAD

If you don't care about tags, and considering that git describe is a porcelain command, which shouldn't be used in a script, then, yes, git rev-parse (a plumbing command) is more appropriate.

But again, if you are to display a SHA1 on your website as an id, I would go with:

git rev-parse --short HEAD

(In order to display only the first 7 digits of the SHA1)


git rev-parse HEAD (meaning the all 40 digits) is still useful, when you want to check if what you just deployed is indeed what HEAD refers to.
See for instance this deployment script:

It first triggers an update:

#If requested, perform update before gathering information from repos.
if $update; then
    echo "Updating fred-official."
    cd "$fredDir"
    git_update
    if ! [[ "$forceFredID" = "" ]]
    then
        checkGitID "$forceFredID"
    fi
    echo "Updating website repo."
    cd "$websiteDir"
    git_update
    if ! [[ "$forceWebsiteID" = "" ]]
    then
        checkGitID "$forceWebsiteID"
    fi
    cd "$startingDir"
fi

The update itself refreshes the website content:

# Discard any local changes, update remote branches and tags, and
# check out to the latest master branch.
git_update() {
    #To update tags and branches.
    git remote update
    git clean -dfx
    git reset --hard origin/master
}

And then it uses git rev-parse HEAD to check what just has been checked out:

function checkGitID {
    checkID=$1
    echo Checking git ID is "$checkID"
    if ! git checkout "$checkID"
    then
        echo Failed to checkout "$checkID"
        exit 4
    fi
    if ! actualID=$(git rev-parse --verify HEAD)
    then
        echo Failed to verify "$checkID"
        git checkout master
        exit 5
    fi
    if ! [[ "$actualID" = "$checkID" ]]
    then
        echo Git verification failed, something very bad is happening
        exit 6
    fi
    echo Git ID verified: "$checkID"
}
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
20

The following command will return the SHA-1 of HEAD:

git log -1 --pretty="%H"
Jeet
  • 38,594
  • 7
  • 49
  • 56
3

Just slightly less elegant:

git log | head -1 | sed s/'commit '//

Lars
  • 37
  • 4
1

You can use:

git describe --always --dirty


   --dirty[=<mark>], --broken[=<mark>]
       Describe the state of the working tree. When the working tree matches HEAD, the output is the same
       as "git describe HEAD". If the working tree has local modification "-dirty" is appended to it. If a
       repository is corrupt and Git cannot determine if there is local modification, Git will error out,
       unless ‘--broken’ is given, which appends the suffix "-broken" instead.

   --all
       Instead of using only the annotated tags, use any ref found in refs/ namespace. This option enables
       matching any known branch, remote-tracking branch, or lightweight tag.
Jintao Zhang
  • 778
  • 5
  • 9