如何在git中获取master主管的提交ID?

时间:2011-02-17 04:34:08

标签: git

我想在我的网站上显示master主管的git commit id(即SHA)作为标识符。

如何从git中提取此信息?

5 个答案:

答案 0 :(得分:64)

git rev-parse HEAD将为您提供最新提交的SHA-1。

答案 1 :(得分:26)

而不是HEAD SHA1,我宁愿选择 git describe ,作为获取“构建ID”的更具可读性的方式。例如:

git describe --abbrev=4 HEAD

如果你不关心标签,并且认为git describe porcelain command ,不应该在脚本中使用,那么,是的,{ {1}}( plumbing command )更合适。

但是,如果你要在你的网站上显示一个SHA1作为id,我会选择:

git rev-parse

(为了仅显示SHA1的前7位数)


git rev-parse --short HEAD (意思是全部40位数字)仍然有用,当您想要检查刚刚部署的内容确实是git rev-parse HEAD所指的内容时。
例如,见deployment script

首先触发更新:

HEAD

更新本身会刷新网站内容:

#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

然后它使用# 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 } 来检查刚签出的内容:

git rev-parse HEAD

答案 2 :(得分:17)

以下命令将返回HEAD的SHA-1:

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

答案 3 :(得分:3)

只是略微不那么优雅:

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

答案 4 :(得分:0)

您可以使用:

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.