How do I get the commit id of the head of master in git?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

git rev-parse HEAD

Solution 2 - Git

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"
}

Solution 3 - Git

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

git log -1 --pretty="%H"

Solution 4 - Git

Just slightly less elegant:

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

Solution 5 - Git

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.

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
QuestionBlankmanView Question on Stackoverflow
Solution 1 - GitAlan Haggai AlaviView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitJeetView Answer on Stackoverflow
Solution 4 - GitLarsView Answer on Stackoverflow
Solution 5 - GitJintao ZhangView Answer on Stackoverflow