How to get the current branch name in Git?

后端 未结 30 2749
清酒与你
清酒与你 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:14

    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.

    0 讨论(0)
  • 2020-11-22 08:15
    git branch | grep -e "^*" | cut -d' ' -f 2
    

    will show only the branch name

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

    If you really want the last branch/tag checked out in detached HEAD state as well.

    git reflog HEAD | grep 'checkout:' | head -1 | rev | cut -d' ' -f1 | rev
    

    Update This is nicer if you have and aren't scared of awk.

    git reflog HEAD | grep 'checkout:' | head -1 | awk '{print $NF}'
    
    0 讨论(0)
  • 2020-11-22 08:16

    you can use git bash on the working directory command is as follow

    git status -b
    

    it will tell you on which branch you are on there are many commands which are useful some of them are

    -s
    

    --short Give the output in the short-format.

    -b --branch Show the branch and tracking info even in short-format.

    --porcelain[=] Give the output in an easy-to-parse format for scripts. This is similar to the short output, but will remain stable across Git versions and regardless of user configuration. See below for details.

    The version parameter is used to specify the format version. This is optional and defaults to the original version v1 format.

    --long Give the output in the long-format. This is the default.

    -v --verbose In addition to the names of files that have been changed, also show the textual changes that are staged to be committed (i.e., like the output of git diff --cached). If -v is specified twice, then also show the changes in the working tree that have not yet been staged (i.e., like the output of git diff).

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

    Why not use git-aware shell prompt, which would tell you name of current branch? git status also helps.


    How git-prompt.sh from contrib/ does it (git version 2.3.0), as defined in __git_ps1 helper function:

    1. First, there is special case if rebase in progress is detected. Git uses unnamed branch (detached HEAD) during the rebase process to make it atomic, and original branch is saved elsewhere.

    2. If the .git/HEAD file is a symbolic link (a very rare case, from the ancient history of Git), it uses git symbolic-ref HEAD 2>/dev/null

    3. Else, it reads .git/HEAD file. Next steps depends on its contents:

      • If this file doesn't exist, then there is no current branch. This usually happens if the repository is bare.

      • If it starts with 'ref: ' prefix, then .git/HEAD is symref (symbolic reference), and we are on normal branch. Strip this prefix to get full name, and strip refs/heads/ to get short name of the current branch:

        b="${head#ref: }"
        # ...
        b=${b##refs/heads/}
        
      • If it doesn't start with 'ref: ', then it is detached HEAD (anonymous branch), pointing directly to some commit. Use git describe ... to write the current commit in human-readable form.

    I hope that helps.

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

    To display the current branch you're on, without the other branches listed, you can do the following:

    git rev-parse --abbrev-ref HEAD
    

    Reference:

    • Show just the current branch in Git (Sep 2009)
    0 讨论(0)
提交回复
热议问题