How to get the current branch name in Git?

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.


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


git rev-parse --abbrev-ref HEAD

That will display the current branch.

Reference:

  • Show just the current branch in Git (Sep 2009)

  • You have also git symbolic-ref HEAD which displays the full refspec.

    To show only the branch name in Git v1.8 and later (thank's to Greg for pointing that out):

    $ git symbolic-ref --short HEAD
    

    On Git v1.7+ you can also do:

    $ git rev-parse --abbrev-ref HEAD
    

    Both should give the same branch name if you're on a branch. If you're on a detached head answers differ.

    Note:

    On an earlier client, this seems to work:

    $ git symbolic-ref HEAD | sed -e "s/^refs/heads///"
    

    – Darien 26. Mar 2014

    链接地址: http://www.djcxy.com/p/430.html

    上一篇: event.preventDefault()与返回false

    下一篇: 如何在Git中获取当前分支名称?