248

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_prompt

As a result I now get:

enter image description here

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:

enter image description here

UPDATE: I have now tried this guide instead:

https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt

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:

enter image description here

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=yes

I 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:

http://pastebin.com/M8kjEiH3

AJM
  • 115
u123
  • 2,932
  • 7
  • 20
  • 28
  • Was force_color_prompt uncommented before? – muru Feb 07 '16 at 08:13
  • Yes I have tried with both uncommented and commented same result. The guide posted above says its should be commented out. – u123 Feb 07 '16 at 08:18
  • Can you post your complete .bashrc? IIRC the default .bashrc doesn't enable colour prompts, so you have to change it to show colours. It depends on what you changed. – muru Feb 07 '16 at 08:20
  • Your new snippet has colour codes in them (the \[\033[32m\] and similar parts) without any checks. The old snippet checks if colour prompts are enabled before using them, and if they're not enabled, well. – muru Feb 07 '16 at 08:25
  • As far as I can see the old snippet also has that when it evals to true. And I did try with 'force_color_prompt=yes' enabled/disabled. I made sure to restart gnome-terminal after each change. – u123 Feb 07 '16 at 08:28
  • That's why I asked you to show the rest of the .bashrc. – muru Feb 07 '16 at 08:29
  • 1
    Have a look at line 64, which should tell you why uncommenting force_color_prompt didn't help. – muru Feb 07 '16 at 08:39
  • I have added a link to a pastebin of the bashrc file containing the version of the old snippet that does not work. – u123 Feb 07 '16 at 08:40
  • @u123 The .bashrc you just posted confirms my theory, you're setting the prompt after unset color_prompt force_color_prompt. Remove line 64 and it'll work. – kos Feb 07 '16 at 08:42
  • Thanks that works. I never touched this part of my .bashrc file. Assumed that the old snippet would work without having to modify other parts of the bashrc file. I will stick with the new snippet since it works with a default .bashrc file - no need for removing line 64. – u123 Feb 07 '16 at 08:45
  • You could also just comment it. – kos Feb 07 '16 at 08:47
  • 2
    @u123 don't worry about the default .bashrc too much. If you mess up, you can always get the original from /etc/skel/.bashrc. – muru Feb 07 '16 at 08:52
  • How can I add this only if the current directory is a git repo? I tried an if/else based on whether or not the contents of $git_parse_branch() were empty, but this didn't work – jjjjjj Aug 29 '18 at 14:27
  • For anyone wondering about the color codes and other magic symbols... – djvg Oct 23 '18 at 14:02
  • Thank you! I love it when deploying. – Player1 Dec 06 '19 at 15:04
  • I'd recommend using git-prompt.sh instead. Nevertheless, there's no need to use git branch and pipe to sed. Just use git describe --contains --all HEAD – CervEd Jun 22 '21 at 19:00
  • The leaseweb.com URL at the start of this post is no longer valid - it now just redirects to the labs.leaseweb.com homepage and the content no longer appears to be hosted anywhere on their site. I'm going to see if I can find an archived version. – AJM May 05 '23 at 17:08

15 Answers15

228

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)\$ '
fi

Is 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\$ '
fi

Which ends with:

unset color_prompt force_color_prompt

The .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

screensot

Mark
  • 153
kos
  • 35,891
208

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_prompt

Reload the .bashrc file with this command:

$ source ~/.bashrc
Sam
  • 2,189
23

For now, I followed this https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f and working, liking it so far, though I'm planning to customize it further.

In Terminal

mkdir ~/.bash

Copy the raw git-prompt.sh file from git contrib in to the ~/.bash directory: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

Inside ~/.bashrc or ~/.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.

muru
  • 197,895
  • 55
  • 485
  • 740
daparic
  • 340
  • 3
    This should be the accepted answer, as it's clear, concise, and does the job, and it works on other platforms too. – miguelmorin May 15 '19 at 10:48
  • The following at the end of ~/.bashrc simply appends to the distribution's default color prompt export PROMPT_COMMAND='__git_ps1 "'${PS1%???}'" "\n\\\$ "' – Danilo Roascio Nov 24 '20 at 15:41
12

Quick hack:

  1. 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\]$ "
  1. Restart the terminal, or source ~/.bashrc:

enter image description here

More detail: https://medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745

thucnguyen
  • 1,527
7

Append 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\]\$ '
3

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!!

abu_bua
  • 10,783
3

I use bash-git-prompt. It's configurable and easier than writing your own, which I imagine is what many readers are looking for.

John
  • 31
  • 4
2

Why 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!

  • Indeed, git branch --show-current 2> /dev/null | sed -e 's/\(.*\)/ (\1)/' works for me. – igor Jul 10 '21 at 09:48
  • 3
    Please always try to provide the full answer even if you you are trying to improve someone else's answer. :) – Rajan Dec 11 '21 at 18:19
2

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 https://gist.github.com/avelino/3188137

Rajan
  • 105
2

My 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\] $ "
ivan.ukr
  • 391
  • 4
  • 12
1

My problem was that I hadn't enabled the option

Run command as a login shell in

TerminalEditProfile PreferencesCommand

TheOdd
  • 3,012
Joker
  • 111
  • I think it's visually apparent from the question that this probably isn't the case here, but it may apply to others. – Oli Dec 11 '21 at 22:28
0

replace

parse_git_branch

with

parse_git_branch 2>/dev/null

in your PS1 definition and live happily ever after.

andrej
  • 416
0
sudo vi .bashrc

Write 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
Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
nimesh
  • 11
0

Best and short solution. Modify .bashrc file located in ~/ directory. Change PS1 variable. In my case it is 60 line. Excerpts of .bashrc file with modified PS1 variable:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;36m\]`__git_ps1`\[\033[00m\]\n\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

I've add __git_ps1 variable and \n for new line for better readability. Worked in Ubuntu 22.04 LTS and 20.04.4 LTS with git installed. Example: sample of the terminal

-1

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:

terminal screenshot

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'
}
export PS1="$PS1\[\e[91m\]\$(parse_git_branch)\[\e[00m\]"