是否有一个Git命令等价于:

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

当前回答

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

其他回答

在Git 1.8.1中,你可以使用带有"——short"选项的Git symbol -ref命令:

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

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

git branch | grep '*'
$ git rev-parse --abbrev-ref HEAD
master

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

使用Git 2.22(2019年第二季度),你将有一个更简单的方法:Git分支——show-current。

参见daniel Umanovskis (Umanovskis)的commit 0ecb1fc(2018年10月25日)。 (由Junio C Hamano合并- gitster -在提交3710f60, 2019年3月7日)

分支:引入——show-current显示选项

当使用——show-current调用时,git分支将打印当前 分支名称和终止。 只有实际的名字被打印出来,没有参考/头。 在分离HEAD状态下,没有输出。 用于脚本和交互式/信息性使用。 与git的branch——list不同,不需要过滤就可以获得 分支的名字。

查看2018年10月Git邮件列表上的原始讨论和实际补丁。


警告:正如Olivier在评论中提到的:

这并不是在任何情况下都有效! 例如,当你在子模块中,它就不起作用了。 'git symbol -ref -short HEAD'总是有效的。

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