只显示 Git 中的当前分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1417957/
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
Show just the current branch in Git
提问by Ollie Saunders
I tried looking for a special Git command for this, but I couldn't find one. Is there anything shorter or faster than the following?
我试图为此寻找一个特殊的 Git 命令,但我找不到。有什么比以下更短或更快的吗?
git branch | awk '/\*/ { print ; }'
回答by earl
$ git rev-parse --abbrev-ref HEAD
master
This should work with Git 1.6.3 or newer.
这应该适用于 Git 1.6.3 或更新版本。
回答by dieresys
In Git 1.8.1 you can use the git symbolic-refcommand with the "--short" option:
在 Git 1.8.1 中,您可以使用带有“--short”选项的git symbols-ref命令:
$ git symbolic-ref HEAD
refs/heads/develop
$ git symbolic-ref --short HEAD
develop
回答by VonC
With Git 2.22 (Q2 2019), you will have a simpler approach: git branch --show-current
.
使用 Git 2.22(2019 年第二季度),您将拥有更简单的方法:git branch --show-current
.
See commit 0ecb1fc(25 Oct 2018) by Daniels Umanovskis (umanovskis
).
(Merged by Junio C Hamano -- gitster
--in commit 3710f60, 07 Mar 2019)
请参阅Daniels Umanovskis ( ) 的commit 0ecb1fc(2018 年 10 月 25 日)。(由Junio C Hamano合并-- --在提交 3710f60 中,2019 年 3 月 7 日)umanovskis
gitster
branch
: introduce--show-current
display optionWhen called with
--show-current
,git branch
will print the current branch name and terminate.
Only the actual name gets printed, withoutrefs/heads
.
In detached HEAD state, nothing is output.Intended both for scripting and interactive/informative use.
Unlikegit branch --list
, no filtering is needed to just get the branch name.
branch
: 介绍--show-current
显示选项调用 with 时
--show-current
,git branch
将打印当前分支名称并终止。
仅打印实际名称,不打印refs/heads
.
在分离的 HEAD 状态下,不输出任何内容。用于脚本和交互式/信息使用。
与 不同git branch --list
,无需过滤即可获取分支名称。
回答by Michael Krelin - hacker
You may be interested in the output of
你可能对输出感兴趣
git symbolic-ref HEAD
In particular, depending on your needs and layout you may wish to do
特别是,根据您的需求和布局,您可能希望这样做
basename $(git symbolic-ref HEAD)
or
或者
git symbolic-ref HEAD | cut -d/ -f3-
and then again there is the .git/HEAD
file which may also be of interest for you.
然后又是.git/HEAD
您可能感兴趣的文件。
回答by theruss
From what I can tell, there is no way to natively show just the current branch in Git, so I have been using:
据我所知,没有办法在 Git 中本地显示当前分支,所以我一直在使用:
git branch | grep '*'
回答by urvish
I guess this should be quick and can be used with a Python API:
我想这应该很快,并且可以与 Python API 一起使用:
git branch --contains HEAD
* master
回答by iny
I'm using
我正在使用
/etc/bash_completion.d/git
It came with Git and provides a prompt with branch name and argument completion.
它与 Git 一起提供并提供带有分支名称和参数完成的提示。
回答by arn0n
This is not shorter, but it deals with detached branches as well:
这不是更短,但它也处理分离的分支:
git branch | awk -v FS=' ' '/\*/{print $NF}' | sed 's|[()]||g'
回答by tymtam
For completeness, echo $(__git_ps1)
, on Linux at least, should give you the name of the current branch surrounded by parentheses.
为了完整echo $(__git_ps1)
起见,至少在 Linux 上,应该为您提供用括号括起来的当前分支的名称。
This may be useful is some scenarios as it is not a Git command (while depending on Git), notably for setting up your Bash command prompt to display the current branch.
这在某些情况下可能很有用,因为它不是 Git 命令(虽然取决于 Git),特别是用于设置 Bash 命令提示符以显示当前分支。
For example:
例如:
/mnt/c/git/ConsoleApp1 (test-branch)> echo $(__git_ps1)
(test-branch)
/mnt/c/git/ConsoleApp1 (test-branch)> git checkout master
Switched to branch 'master'
/mnt/c/git/ConsoleApp1 (master)> echo $(__git_ps1)
(master)
/mnt/c/git/ConsoleApp1 (master)> cd ..
/mnt/c/git> echo $(__git_ps1)
/mnt/c/git>
回答by Kaliyug Antagonist
Someone might find this (git show-branch
--current
) helpful. The current branch is shown with a * mark.
有人可能会发现这 ( ) 有帮助。当前分支以 * 标记显示。git show-branch
--current
host-78-65-229-191:idp-mobileid user-1$ git show-branch --current
! [CICD-1283-pipeline-in-shared-libraries] feat(CICD-1283): Use latest version of custom release plugin.
* [master] Merge pull request #12 in CORES/idp-mobileid from feature/fix-schema-name to master
--
+ [CICD-1283-pipeline-in-shared-libraries] feat(CICD-1283): Use latest version of custom release plugin.
+ [CICD-1283-pipeline-in-shared-libraries^] feat(CICD-1283): Used the renamed AWS pipeline.
+ [CICD-1283-pipeline-in-shared-libraries~2] feat(CICD-1283): Point to feature branches of shared libraries.
-- [master] Merge pull request #12 in CORES/idp-mobileid from feature/fix-schema-name to master