How to get the current branch name in Git?

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

    You can permanently set up your bash output to show your git-branch name. It is very handy when you work with different branches, no need to type $ git status all the time. Github repo git-aware-prompt .

    Open your terminal (ctrl-alt-t) and enter the commands

    mkdir ~/.bash
    cd ~/.bash
    git clone git://github.com/jimeh/git-aware-prompt.git
    

    Edit your .bashrc with sudo nano ~/.bashrc command (for Ubuntu) and add the following to the top:

    export GITAWAREPROMPT=~/.bash/git-aware-prompt
    source "${GITAWAREPROMPT}/main.sh"
    

    Then paste the code

    export PS1="\${debian_chroot:+(\$debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "
    

    at the end of the same file you pasted the installation code into earlier. This will give you the colorized output:

    0 讨论(0)
  • 2020-11-22 08:27
    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 rev-parse --abbrev-ref HEAD
    

    or with Git 2.22 and above:

    git branch --show-current
    
    0 讨论(0)
  • 2020-11-22 08:29

    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

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

    You can just type in command line (console) on Linux, in the repository directory:

    $ git status
    

    and you will see some text, among which something similar to:

    ...
    On branch master
    ...
    

    which means you are currently on master branch. If you are editing any file at that moment and it is located in the same local repository (local directory containing the files that are under Git version control management), you are editing file in this branch.

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

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

    What about this?

    { git symbolic-ref HEAD 2> /dev/null || git rev-parse --short HEAD 2> /dev/null } | sed "s#refs/heads/##"
    
    0 讨论(0)
提交回复
热议问题