开发者

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

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?


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

git rev-parse HEAD


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


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

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


Just slightly less elegant:

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


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜