git 如何在git中获取master负责人的提交ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5025087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:07:22  来源:igfitidea点击:

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

git

提问by Blankman

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

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

How can I pull this information from git?

如何从 git 中提取这些信息?

回答by Alan Haggai Alavi

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

此命令将为您提供最新提交的 SHA-1

git rev-parse HEAD

回答by VonC

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

而不是 HEAD SHA1,我宁愿使用git describe,作为获得“构建ID”的更易读的方式。例如:

git describe --abbrev=4 HEAD


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

如果您不关心标签,并且考虑到它git describe是不应该在脚本中使用的瓷器命令,那么,是的,git rev-parse管道命令)更合适。

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

但同样,如果您要在您的网站上显示 SHA1 作为 ID,我会选择:

git rev-parse --short HEAD

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

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



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 HEADrefers to.
See for instance this deployment script:

git rev-parse HEAD(意味着所有 40 位数字)仍然很有用,当您想检查您刚刚部署的内容是否确实是HEAD所指的内容时。
例如,请参阅此部署脚本

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 HEADto check what just has been checked out:

然后它用于git rev-parse HEAD检查刚刚签出的内容:

function checkGitID {
    checkID=
    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"
}

回答by Jeet

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

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

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

回答by Lars

Just slightlyless elegant:

只是稍微不那么优雅:

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

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

回答by Jintao Zhang

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.