如何获取Git中当前分支的名称?


当前回答

这个呢?

{ git symbolic-ref HEAD 2> /dev/null || git rev-parse --short HEAD 2> /dev/null } | sed "s#refs/heads/##"

其他回答

#!/bin/bash
function git.branch {
  br=`git branch | grep "*"`
  echo ${br/* /}
}
git.branch

使用Mac将其添加到PS1:

PS1='\W@\u >`[ -d .git ] && git branch | grep  ^*|cut -d" " -f2`> $ '

在运行上述命令之前:

运行该命令后:

不要担心,如果它不是GIT存储库,它不会显示错误,因为[-d.GIT]会检查.GIT文件夹是否存在。

git branch -l

这将列出所有本地分支机构,当前分支机构以绿色标记并打印

git branch | grep "*" | sed "s/* //" | awk '{printf $0}' | pbcopy

将结果直接复制到粘贴板。感谢@olivier refalo的开始…

我知道这很晚了,但在linux/mac上,从终端可以使用以下命令。

git status | sed -n 1p

说明:

gitstatus->获取工作树状态sed-n 1p->从状态体获取第一行

对上述命令的响应如下:

"On branch your_branch_name"