-1

I would like to show the branch name of git on terminal. However, I don't know how to do that.

I have installed git by homebrew.

Generally, I can see this two file on "/usr/local/etc/bash_completion.d/"

git-completion.bash git-prompt.sh

But they are not there...

padawin
  • 4,230
  • 15
  • 19
ryonz
  • 441
  • 2
  • 8
  • 19
  • what do you mean by "show a branch"? log the commits of a given branch? list the available branch names? get the name of the branch you are currently checked out? – padawin May 15 '19 at 09:13
  • I mean that we can anytime see the branch name we are using on terminal by using these things. – ryonz May 15 '19 at 09:16
  • I use https://github.com/jimeh/git-aware-prompt – Odalrick May 15 '19 at 10:26
  • Possible duplicate of [How to get the current branch name in Git?](https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git) – phd May 15 '19 at 11:12
  • https://stackoverflow.com/search?q=%5Bgit%5D+show+the+current+branch – phd May 15 '19 at 11:12

5 Answers5

2
git rev-parse --abbrev-ref HEAD

in terminal will output the name of currently checked out branch.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
1

You can use the builtin __git_ps1:

If you don't have the command __git_ps1, then add the following line in your .bash_profile to source git-completion.bash:

source /path/to/git-completion.bash

You can find the file git-completion.bash using:

mdfind git-completion.bash

Once it is done, you need to add a call to __git_ps1 in your $PS1 variable (which defines your prompt). For that, you need to redefine the variable in your .bash_profile. Here is an example:

PS1='\h:\W \u $(__git_ps1) \$'

If you already defined it before (to customise it for example), you can add in it the part $(__git_ps1).

Finally, restart your terminal.

As an extra, you can set in your .bash_profile the following:

export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUPSTREAM=auto

The first one will allow to display a * if you have local modifications and a + if you have staged changes. The second one will allow to display a $ if you have stashes. The third one displays info about your upstream (> if you are ahead and can push, < if you are behind and can pull, <> if you diverged).

It would look like that:

enter image description here

padawin
  • 4,230
  • 15
  • 19
1

if one is missing the original __git_ps1 function from @git/git/git-prompt.sh, this one-liner can be utilized as temporary solution. just like the original, it prints current branch name like main, dev etc. or nothing, if current directory is outside of any git tree

    __git_ps1 () {
        git rev-parse --abbrev-ref HEAD 2> /dev/null | tr -d '\n'
    }

to get the function working everywhere in bash properly one have to add the above code to /etc/bash.bashrc or local ~/.bashrc and append it with export -f __git_ps1 command. this will make the wrapper available even after sudo -i

0

In order to show current git branch on ubuntu terminal :

  1. Open the ~/.bashrc file with your favourite editor. (I did with nano : sudo nano ~/.bashrc)

  2. Then add the following script:

    git_branch() {
              git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
           }
    export PS1="[\u@\h \W]\[\033[00;32m\]\$(git_branch)\[\033[00m\]\$ "
    
  3. Load the bashrc file to reflect the branch using source ~/.bashrc

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
0
# Show git branch name
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt

Add this to you ~/.bashrc files

Saurabh Kumar
  • 2,088
  • 14
  • 17