How to get the current branch name in Git?

2018-12-31 16:57发布

I'm from a Subversion background and, when I had a branch, I knew what I was working on with "These working files point to this branch".

But with Git I'm not sure when I am editing a file in NetBeans or Notepad++, whether it's tied to the master or another branch.

There's no problem with git in bash, it tells me what I'm doing.

30条回答
笑指拈花
2楼-- · 2018-12-31 17:36

I recommend using any of these two commands.

git branch | grep -e "^*" | cut -d' ' -f 2

OR

git status | sed -n 1p | cut -d' ' -f 3

OR (more verbose)

git status -uno -bs| cut -d'#' -f 3 | cut -d . -f 1| sed -e 's/^[ \t]//1'| sed -n 1p

查看更多
梦醉为红颜
3楼-- · 2018-12-31 17:38
git symbolic-ref -q --short HEAD

I use this in scripts that need the current branch name. It will show you the current short symbolic reference to HEAD, which will be your current branch name.

查看更多
琉璃瓶的回忆
4楼-- · 2018-12-31 17:41

One more alternative:

git name-rev --name-only HEAD
查看更多
人气声优
5楼-- · 2018-12-31 17:42

A less noisy version for git status would do the trick

git status -bsuno

It prints out

## branch-name
查看更多
长期被迫恋爱
6楼-- · 2018-12-31 17:43

In Netbeans, ensure that versioning annotations are enabled (View -> Show Versioning Labels). You can then see the branch name next to project name.

http://netbeans.org/bugzilla/show_bug.cgi?id=213582

查看更多
公子世无双
7楼-- · 2018-12-31 17:44
git branch

should show all the local branches of your repo. The starred branch is your current branch.

If you want to retrieve only the name of the branch you are on, you can do:

git branch | grep \* | cut -d ' ' -f2
查看更多
登录 后发表回答