bash 如何以编程方式确定当前签出的 Git 分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1593051/
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
How to programmatically determine the current checked out Git branch
提问by JasonSmith
In a Unix or GNU scripting environment (e.g. a Linux distro, Cygwin, OSX), what is the best way to determine which Git branch is currently checked out in a working directory?
在 Unix 或 GNU 脚本环境(例如 Linux 发行版、Cygwin、OSX)中,确定当前在工作目录中检出哪个 Git 分支的最佳方法是什么?
One use of this technique would be automatically labeling a release (like svnversion
would do with Subversion).
这种技术的一个用途是自动标记一个版本(就像svnversion
Subversion 一样)。
Please also see my related question: How to programmatically determine whether a Git checkout is a tag, and if so what is the tag name?
另请参阅我的相关问题:如何以编程方式确定 Git checkout 是否为标签,如果是,标签名称是什么?
回答by Jakub Nar?bski
The correct solution is to take a peek at contrib/completions/git-completion.bashdoes that for bash prompt in __git_ps1
. Removing all extras like selecting how to describe detached HEAD situation, i.e. when we are on unnamed branch, it is:
正确的解决方案是查看contrib/completions/git-completion.bash对 .bash 中的 bash 提示符执行此操作__git_ps1
。删除所有额外的东西,比如选择如何描述分离的 HEAD 情况,即当我们在未命名的分支上时,它是:
branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
branch_name="(unnamed branch)" # detached HEAD
branch_name=${branch_name##refs/heads/}
git symbolic-refis used to extract fully qualified branch name from symbolic reference; we use it for HEAD, which is currently checked out branch.
git symbols-ref用于从符号引用中提取完全限定的分支名称;我们将它用于 HEAD,这是当前已检出的分支。
Alternate solution could be:
替代解决方案可能是:
branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
branch_name=${branch_name:-HEAD}
where in last line we deal with the detached HEAD situation, using simply "HEAD" to denote such situation.
在最后一行我们处理分离的 HEAD 情况,简单地使用“HEAD”来表示这种情况。
Added 11-06-2013
添加 11-06-2013
Junio C. Hamano (git maintainer) blog post, Checking the current branch programatically, from June 10, 2013 explains whys(and hows) in more detail.
Junio C. Hamano(git 维护者)博客文章,以编程方式检查当前分支,从 2013 年 6 月 10 日开始,更详细地解释了原因(和方法)。
回答by Michael Erickson
Does anyone see anything wrong with just asking Git to describe the branch you are on?
有没有人认为仅仅要求 Git 描述您所在的分支有什么问题?
git rev-parse --symbolic-full-name --abbrev-ref HEAD
That can be used within $() and passed easily in Bash, Powershell, Perl, etc. It isn't fooled if you have several branches on the commit you are on, and if you currently aren't on a branch, it simply replies with "HEAD".
这可以在 $() 中使用,并在 Bash、Powershell、Perl 等中轻松传递。回复“头”。
Alternatively, you can use
或者,您可以使用
git symbolic-ref --short -q HEAD
Which will give you the same output, but it won't return anything at all if you are detached. This one is useful if you want an error when detached though, just remove the -q.
这将为您提供相同的输出,但如果您分离,它根本不会返回任何内容。如果你想在分离时出错,这个很有用,只需删除 -q。
回答by jonny
you can use
git name-rev --name-only HEAD
您可以使用
git name-rev --name-only HEAD
回答by Johnny Utahh
From this answer: https://stackoverflow.com/a/1418022/605356:
从这个答案:https: //stackoverflow.com/a/1418022/605356:
$ git rev-parse --abbrev-ref HEAD
master
Apparently works with Git 1.6.3 or newer.
显然适用于 Git 1.6.3 或更新版本。
回答by fitorec
Try with:
尝试:
git symbolic-ref --short -q HEAD
Or you try with git branch
with --no-color
force simple plain string the output:
或者你尝试git branch
用--no-color
武力简单的纯字符串输出:
git branch --no-color
With grep in regex mode(-E
) you can check if exists the character '*':
使用正则表达式模式下的 grep( -E
),您可以检查字符 '*' 是否存在:
git branch --no-color | grep -E '^\*'
The results its similar to:
结果类似于:
* currentBranch
You can use the next options:
您可以使用以下选项:
sed 's/\*[^a-z]*//g'
cut -d ' ' -f 2
awk '{print }'
for example:
例如:
git branch --no-color | grep -E '^\*' | sed 's/\*[^a-z]*//g'
git branch --no-color | grep -E '^\*' | sed cut -d ' ' -f 2
git branch --no-color | grep -E '^\*' | awk '{print }'
if exists a error you cant use an default value:
如果存在错误,则不能使用默认值:
cmd || echo 'defualt value';
All into in a bash function:
全部放入 bash 函数中:
function get_branch() {
git branch --no-color | grep -E '^\*' | awk '{print }' \
|| echo "default_value"
# or
# git symbolic-ref --short -q HEAD || echo "default_value";
}
Use:
用:
branch_name=`get_branch`;
echo $branch_name;
回答by muhammed basil
This one worked for me in the bash file.
这个在 bash 文件中对我有用。
git branch | grep '^*' | sed 's/* //'
################bash file###################
#!/bin/bash
BRANCH=$(git branch | grep '^*' | sed 's/* //' )
echo $BRANCH
回答by tjb
adapting the accepted answer to windows powershell:
将公认的答案改编为 windows powershell:
Split-Path -Leaf (git symbolic-ref HEAD)
回答by August Lilleaas
This one works for me. The --no-color
part is, or can be, important if you want a plain string back.
这个对我有用。--no-color
如果你想要一个普通的字符串,这个部分是,或者可能是重要的。
git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)//'
回答by tonywoode
I'm trying for the simplest and most self-explanatory method here:
我在这里尝试最简单和最不言自明的方法:
git status | grep "On branch" | cut -c 11-
回答by silvansky
I found two really simple ways to do that:
我找到了两种非常简单的方法来做到这一点:
$ git status | head -1 | cut -d ' ' -f 4
and
和
$ git branch | grep "*" | cut -d ' ' -f 2