How do I show the git branch with colours in Bash prompt?
Sebastian Wright
I am using this guide to show the branch name in gnome terminal (Ubuntu 15.10) when working in a git repository. Based on the above I now have the below in my ~/.bashrc file:
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes ...
# Add git branch if its present to PS1
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_promptAs a result I now get:
so it works. But why has the coloring of my user@host been removed? And I would also expect that the branch name should be colored. Before it looked like this:
UPDATE: I have now tried this guide instead:
adding this to .bashrc:
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "and that works:
Notice in .bashrc I also have this (default):
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yesI have yet to find the reason why that snippet gives the correct result and the other version does not. Any input on this?
Here is the version of my .bashrc that has the old snippet enabled that does not work:
1614 Answers
This snippet:
# Add git branch if its present to PS1
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)\$ '
fiIs meant to replace the default prompt definition:
if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fiWhich ends with:
unset color_prompt force_color_promptThe .bashrc you posted shows you're adding it after the default prompt definition and unset color_prompt force_color_prompt (line #64).
Either replace the default prompt definition with the snippet or leave your ~/.bashrc as it is and comment the default prompt definition along with unset color_prompt force_color_prompt on line #64:
So part of your .bashrc could look like
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
# THE SIX LINES BELOW are the default prompt and the unset (which were in the original .bashrc)
#if [ "$color_prompt" = yes ]; then
# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
#else
# PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
#fi
#unset color_prompt force_color_prompt 10 Ubuntu: Show your branch name on your terminal
Add these lines in your ~/.bashrc file
# 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_promptReload the .bashrc file with this command:
$ source ~/.bashrc 19 For now, I followed this and working, liking it so far, though I'm planning to customize it further.
2In Terminal
mkdir ~/.bashCopy the raw
git-prompt.shfile from git contrib in to the~/.bashdirectory:Inside
~/.bashrcor~/.bash_profile(choose the file where you normally put any bash customizations/setup), add the lines:source ~/.bash/git-prompt.sh # Show git branch name at command prompt export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color # Terminal Prompt: # Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more) export PROMPT_COMMAND='__git_ps1 "\w" "\n\\\$ "' # Git branch (relies on git-prompt.sh)As long as you're inside a git repo, your Bash prompt should now show the current git branch in color signifying if its got uncommitted changes.
Quick hack:
- Adding this to
~/.bashrc:
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]$ "- Restart the terminal, or source
~/.bashrc:
More detail:
1Append the lines below to ~/.bashrc:
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ ' 2 Go to home folder
click Ctrl+h to show hidden files.
Open .bashrc file and at the end paste the next:
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "In case you have your terminal opened, close and open again. Enjoy!!
1I use bash-git-prompt. It's configurable and easier than writing your own, which I imagine is what many readers are looking for.
0Why bother with using sed? as in ...
git branch 2> /dev/null | sed -e '/^[^]/d' -e 's/ (.*)/(\1)/'
Much easier to just use:
git branch --show-current
It outputs the current branch and no extra characters!
2My variant for KUbuntu 20.04 LTS, derived from the original value of the PS1:
# put into ~/.bashrc
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ " Topic is old but I will post being able to help someone. In the .bashrc file located inside the user's local folder, replace the existing PS1 with this one:
if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[1;36m\]$(__git_ps1 " (%s)")\[\033[01;34m\]$\[\033[00m\]'If you want to change the color of the branch change the color before the parameter $ (__ git_ps1 "(% s)") that is, change this value:
\[\033[1;36m\]color chart
My problem was that I hadn't enabled the option
Run command as a login shell in
Terminal → Edit → Profile Preferences → Command
1replace
parse_git_branchwith
parse_git_branch 2>/dev/nullin your PS1 definition and live happily ever after.
In my case I didn't overwrite the PS1 variable because I had to preserve the prepended environment that is added by anaconda package, that is the (base) in the image bellow. I use Ubuntu 20.04, and all I wanted is to append the branch at the end, thanks to @thucnguyen as my solution is based on his one:
parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'
}
export PS1="$PS1\[\e[91m\]\$(parse_git_branch)\[\e[00m\]" sudo vi .bashrcWrite below code the button of the file:
git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\[\033[1;36m\]\u\[\033[1;31m\]@\[\033[1;32m\]\h:\[\033[1;35m\]\w\[\033[1;31m\]\$\[\033[0m\]\$(git_branch)\$ "source .bashrc 1