仅显示 Git 中的当前分支

发布于 2024-08-04 10:27:15 字数 90 浏览 6 评论 0原文

是否存在相当于以下内容的 Git 命令:

git branch | awk '/\*/ { print $2; }'

Is there a Git command equivalent to:

git branch | awk '/\*/ { print $2; }'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

薄凉少年不暖心 2024-08-11 10:27:15
$ git rev-parse --abbrev-ref HEAD
master

这应该适用于 Git 1.6.3 或更高版本。

$ git rev-parse --abbrev-ref HEAD
master

This should work with Git 1.6.3 or newer.

战皆罪 2024-08-11 10:27:15

使用 Git 2.22(2019 年第 2 季度),您将拥有一种更简单的方法:gitbranch --show-current

请参阅 提交 0ecb1fc(2018 年 10 月 25 日),作者:丹尼尔斯·乌马诺夫斯基 (umanovskis)
(由 Junio C Hamano -- gitster -- 合并于 提交 3710f60,2019 年 3 月 7 日)

branch:引入--show-current显示选项

当使用--show-current调用时,git分支将打印当前
分支名称并终止。
仅打印实际名称,没有 refs/heads
在分离的 HEAD 状态下,没有任何输出。

旨在用于脚本编写和交互式/信息用途。
与 git Branch --list 不同,不需要过滤即可获取
分支名称。

请参阅 2018 年 10 月的 Git 邮件列表上的原始讨论,以及实际补丁


警告:正如评论中提到的 作者:奥利维尔

这并不适用于所有情况!
例如,当您位于子模块中时,它不起作用。
git symbolic-ref --short HEAD”始终有效。

With Git 2.22 (Q2 2019), you will have a simpler approach: 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)

branch: introduce --show-current display option

When called with --show-current, git branch will print the current
branch name and terminate.
Only the actual name gets printed, without refs/heads.
In detached HEAD state, nothing is output.

Intended both for scripting and interactive/informative use.
Unlike git branch --list, no filtering is needed to just get the
branch name.

See the original discussion on the Git mailing list in Oct. 2018, and the actual patch.


Warning: as mentioned in the comments by Olivier:

This does not work in every situation!
When you are for instance in a submodule, it does not work.
'git symbolic-ref --short HEAD' always works.

も让我眼熟你 2024-08-11 10:27:15

在 Git 1.8.1 中,您可以使用带有“--short”选项的 git symbolic-ref 命令:

$ git symbolic-ref HEAD
refs/heads/develop
$ git symbolic-ref --short HEAD
develop

In Git 1.8.1 you can use the git symbolic-ref command with the "--short" option:

$ git symbolic-ref HEAD
refs/heads/develop
$ git symbolic-ref --short HEAD
develop
星光不落少年眉 2024-08-11 10:27:15

的输出感兴趣

git symbolic-ref HEAD

您可能对具体

basename $(git symbolic-ref HEAD)

,具体取决于您的需求和您可能希望执行的布局,或者

git symbolic-ref HEAD | cut -d/ -f3-

您可能也对 .git/HEAD 文件感兴趣。

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.

柠檬心 2024-08-11 10:27:15

据我所知,没有办法只显示 Git 中的当前分支,所以我一直在使用:

git branch | grep '*'

From what I can tell, there is no way to natively show just the current branch in Git, so I have been using:

git branch | grep '*'
凉城 2024-08-11 10:27:15

我想这应该很快并且可以与 Python API 一起使用:

git branch --contains HEAD
* master

I guess this should be quick and can be used with a Python API:

git branch --contains HEAD
* master
一腔孤↑勇 2024-08-11 10:27:15

这并不短,但它也处理分离的分支:

git branch | awk -v FS=' ' '/\*/{print $NF}' | sed 's|[()]||g'

This is not shorter, but it deals with detached branches as well:

git branch | awk -v FS=' ' '/\*/{print $NF}' | sed 's|[()]||g'
扭转时空 2024-08-11 10:27:15

我使用的

/etc/bash_completion.d/git

是 Git 附带的它,并提供带有分支名称和参数完成的提示。

I'm using

/etc/bash_completion.d/git

It came with Git and provides a prompt with branch name and argument completion.

那伤。 2024-08-11 10:27:15

对于那些喜欢别名的人:
将以下内容添加到您的 .zshrc 中,以便您获得更轻松的 git 命令流程:

alias gpsu="git push --set-upstream origin $(git symbolic-ref --short HEAD)"

For those liking aliases:
Put the following to your .zshrc so you get easier git command flow:

alias gpsu="git push --set-upstream origin $(git symbolic-ref --short HEAD)"

死开点丶别碍眼 2024-08-11 10:27:15

有人可能会发现这个(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

Someone might find this (git show-branch --current) helpful. The current branch is shown with a * mark.

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
梦归所梦 2024-08-11 10:27:15

为了完整起见,至少在 Linux 上,echo $(__git_ps1) 应该为您提供由括号括起来的当前分支的名称。

这在某些情况下可能很有用,因为它不是 Git 命令(同时依赖于 Git),特别是用于设置 Bash 命令提示符以显示当前分支

例如:

/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>

For completeness, echo $(__git_ps1), on Linux at least, should give you the name of the current branch surrounded by parentheses.

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.

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>
飞烟轻若梦 2024-08-11 10:27:15

我正在使用

git name-rev HEAD

获取

HEAD remotes/origin/branch

I'm using

git name-rev HEAD

get

HEAD remotes/origin/branch
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文