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:18

For my own reference (but it might be useful to others) I made an overview of most (basic command line) techniques mentioned in this thread, each applied to several use cases: HEAD is (pointing at):

  • local branch (master)
  • remote tracking branch, in sync with local branch (origin/master at same commit as master)
  • remote tracking branch, not in sync with a local branch (origin/feature-foo)
  • tag (v1.2.3)
  • submodule (run inside the submodule directory)
  • general detached head (none of the above)

Results:

  • git branch | sed -n '/\* /s///p'
    • local branch: master
    • remote tracking branch (in sync): (detached from origin/master)
    • remote tracking branch (not in sync): (detached from origin/feature-foo)
    • tag: (detached from v1.2.3)
    • submodule: `(HEAD detached at 285f294)
    • general detached head: (detached from 285f294)
  • git status | head -1
    • local branch: # On branch master
    • remote tracking branch (in sync): # HEAD detached at origin/master
    • remote tracking branch (not in sync): # HEAD detached at origin/feature-foo
    • tag: # HEAD detached at v1.2.3
    • submodule: # HEAD detached at 285f294
    • general detached head: # HEAD detached at 285f294
  • git describe --all
    • local branch: heads/master
    • remote tracking branch (in sync): heads/master (note: not remotes/origin/master)
    • remote tracking branch (not in sync): remotes/origin/feature-foo
    • tag: v1.2.3
    • submodule: remotes/origin/HEAD
    • general detached head: v1.0.6-5-g2393761
  • cat .git/HEAD:
    • local branch: ref: refs/heads/master
    • submodule: cat: .git/HEAD: Not a directory
    • all other use cases: SHA of the corresponding commit
  • git rev-parse --abbrev-ref HEAD
    • local branch: master
    • all the other use cases: HEAD
  • git symbolic-ref --short HEAD
    • local branch: master
    • all the other use cases: fatal: ref HEAD is not a symbolic ref

(FYI this was done with git version 1.8.3.1)

查看更多
爱死公子算了
3楼-- · 2018-12-31 17:18

Returns either branch name or SHA1 when on detached head:

git rev-parse --abbrev-ref HEAD | grep -v ^HEAD$ || git rev-parse HEAD

This is a short version of @dmaestro12's answer and without tag support.

查看更多
若你有天会懂
4楼-- · 2018-12-31 17:19
git branch | grep -e "^*" | cut -d' ' -f 2

will show only the branch name

查看更多
公子世无双
5楼-- · 2018-12-31 17:20

The following shell command tells you the branch that you are currently in.

git branch | grep ^\*

When you don't want to type that long command every time you want to know the branch and you are using Bash, give the command a short alias, for example alias cb, like so.

alias cb='git branch | grep ^\*'

When you are in branch master and your prompt is $, you will get * master as follows.

$ cb
* master
查看更多
忆尘夕之涩
6楼-- · 2018-12-31 17:21

you can also use GIT_BRANCH variable as appears here: https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin

The git plugin sets several environment variables you can use in your scripts:

GIT_COMMIT - SHA of the current

GIT_BRANCH - Name of the branch currently being used, e.g. "master" or "origin/foo"

GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (the current SHA on first build in branch)

GIT_URL - Repository remote URL

GIT_URL_N - Repository remote URLs when there are more than 1 remotes, e.g. GIT_URL_1, GIT_URL_2

GIT_AUTHOR_EMAIL - Committer/Author Email

GIT_COMMITTER_EMAIL - Committer/Author Email

查看更多
萌妹纸的霸气范
7楼-- · 2018-12-31 17:21

Add it to PS1 using Mac :

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

Before running the command above :

enter image description here

After running that command :

enter image description here

Dont worry, if it is not GIT repository , it will not display error because of [-d .git] which checks if .git folder exists or not.

查看更多
登录 后发表回答