bash 如何在分离的 HEAD 状态下找到当前的 git 分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6059336/
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-18 00:01:39  来源:igfitidea点击:

How to find the current git branch in detached HEAD state

gitbashjenkinsbranchtravis-ci

提问by neu242

I can find the current git branch name by doing either of these:

我可以通过执行以下任一操作来找到当前的 git 分支名称:

git branch | awk '/^\*/ { print  }'
git describe --contains --all HEAD

But when in a detached HEAD state, such as in the post build phase in a Jenkinsmaven build (or in a Travis git fetch), these commands doesn't work.

但是当处于分离的 HEAD 状态时,例如在Jenkinsmaven 构建(或在 Travis git fetch)的后期构建阶段,这些命令不起作用。

My current working solution is this:

我目前的工作解决方案是这样的:

git show-ref | grep $(git log --pretty=%h -1) | sed 's|.*/\(.*\)||' | sort -u | grep -v HEAD

It displays any branch name that has the last commit on its HEAD tip. This works fine, but I feel that someone with stronger git-fu might have a prettier solution?

它显示在其 HEAD 提示上有最后一次提交的任何分支名称。这很好用,但我觉得有更强大的 git-fu 的人可能有更漂亮的解决方案?

采纳答案by Cascabel

A more porcelain way:

更瓷器的方式:

git log -n 1 --pretty=%d HEAD

# or equivalently:
git show -s --pretty=%d HEAD

The refs will be listed in the format (HEAD, master)- you'll have to parse it a little bit if you intend to use this in scripts rather than for human consumption.

参考将按格式列出(HEAD, master)- 如果您打算在脚本中使用它而不是供人类使用,则必须对其进行一些解析。

You could also implement it yourself a little more cleanly:

你也可以自己更干净地实现它:

git for-each-ref --format='%(objectname) %(refname:short)' refs/heads | awk "/^$(git rev-parse HEAD)/ {print $2}"

with the benefit of getting the candidate refs on separate lines, with no extra characters.

好处是可以在单独的行上获取候选参考,没有额外的字符。

回答by Epeli

I needed a bit different solution for Jenkins because it does not have local copies of the branches. So the current commit must be matched against the remote branches:

我需要为 Jenkins 提供一些不同的解决方案,因为它没有分支的本地副本。所以当前提交必须与远程分支匹配:

git ls-remote --heads origin | grep $(git rev-parse HEAD) | cut -d / -f 3

or without network:

或没有网络:

git branch --remote --verbose --no-abbrev --contains | sed -rne 's/^[^\/]*\/([^\ ]+).*$//p'

It's also worth noting that this might return multiple branch names when you have multiple branch heads at the same commit.

还值得注意的是,当您在同一提交中有多个分支负责人时,这可能会返回多个分支名称。

UPDATE:

更新:

I just noticed that Jenkins sets GIT_BRANCHenvironment variable which contains a value like origin/master. This can be used to get git branch in Jenksin too:

我刚刚注意到 Jenkins 设置了GIT_BRANCH环境变量,其中包含一个类似origin/master. 这也可用于在 Jenksin 中获取 git 分支:

echo $GIT_BRANCH | cut -d / -f 2

回答by Seth Robertson

git branch --contains HEAD

Obviously discarding (no branch). Of course, you may get an arbitrary number of branches which could describe the current HEAD (including of course none depending on how you got onto no-branch) which might have be fast-forward merged into the local branch (one of many good reasons why you should always use git merge --no-ff).

明显丢弃(无分支)。当然,您可能会得到任意数量的分支,这些分支可以描述当前的 HEAD(当然包括没有分支,这取决于您如何进入无分支),这些分支可能已快速合并到本地分支中(许多很好的原因之一)为什么你应该总是使用git merge --no-ff)。

回答by jthill

Here's git nthlastcheckout, it gets the exact string you used for your nth last checkout from the reflog:

这是git nthlastcheckout,它从 reflog 中获取您用于第 n 次最后一次结帐的确切字符串:

git config --global alias.nthlastcheckout '!nthlastcheckout'"() {
        git reflog |
        awk '$3==\"checkout:\" {++n}
             n=='${1-1}' {print $NF; exit}
             END {exit n!='${1-1}'}'
}; nthlastcheckout \"$@\""

Examples:

例子:

$ git nthlastcheckout
master
$ git nthlastcheckout 2
v1.3.0^2

回答by Ruslanas

The shortest I got working on bitbucket pipelines:

我在 bitbucket 管道上工作的最短时间:

git show -s --pretty=%D HEAD | awk '{gsub("origin/", ""); print }'

回答by Gaelan

git symbolic-ref HEADreturns refs/heads/branchnameif you are on a branch and errors if you aren't.

git symbolic-ref HEADrefs/heads/branchname如果您在分支上,则返回,如果不在,则返回错误。