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

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

当前回答

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

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

其他回答

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

喜欢别名的同学: 将以下文件放到你的.zshrc中,这样你就可以更容易地获得git命令流:

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

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

git branch | grep '*'

为了完整起见,至少在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>