How to get the current branch name in Git?

后端 未结 30 2746
清酒与你
清酒与你 2020-11-22 07:47

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 sur

相关标签:
30条回答
  • 2020-11-22 08:31

    As of version 2.22 of git you could just use:

    git branch --show-current
    

    As per man page:

    Print the name of the current branch. In detached HEAD state, nothing is printed.

    0 讨论(0)
  • 2020-11-22 08:31

    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

    0 讨论(0)
  • 2020-11-22 08:32

    git branch show current branch name only.

    While git branch will show you all branches and highlight the current one with an asterisk, it can be too cumbersome when working with lots of branches.

    To show only the branch you are currently on, use:

    git rev-parse --abbrev-ref HEAD
    
    0 讨论(0)
  • 2020-11-22 08:33

    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)

    0 讨论(0)
  • 2020-11-22 08:33

    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
    
    0 讨论(0)
  • 2020-11-22 08:36

    Add it to PS1 using Mac :

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

    Before running the command above :

    After running that command :

    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.

    0 讨论(0)
提交回复
热议问题