Skip to content

Instantly share code, notes, and snippets.

@danielalvarenga
Last active July 21, 2025 04:53
Show Gist options
  • Save danielalvarenga/2df8cabbd6f3041c2378 to your computer and use it in GitHub Desktop.
Save danielalvarenga/2df8cabbd6f3041c2378 to your computer and use it in GitHub Desktop.
Show branch in terminal Ubuntu
# Add in ~/.bashrc or ~/.bash_profile
function parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
RED="\[\033[01;31m\]"
YELLOW="\[\033[01;33m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
NO_COLOR="\[\033[00m\]"
# without host
PS1="$GREEN\u$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
# with host
# PS1="$GREEN\u@\h$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
@neerajbg
Copy link

neerajbg commented Dec 1, 2023

Thanks. Worked for me on fedora 39

@enheit
Copy link

enheit commented Dec 29, 2023

Thanks!

@hoshang8282
Copy link

thank you :-)

@tiagocriar
Copy link

thanks

@amjarmed
Copy link

thanks

@psk001
Copy link

psk001 commented Feb 19, 2024

doesnt work on ubuntu 22 , just displays
image

bash

image

PS1

image

@fcuantico
Copy link

I am using Ubuntu 22.04.3 and this video was helpful: https://www.youtube.com/watch?v=RBVub5BbxdI
I did the following changes:
1.
Screenshot from 2024-02-20 18-54-55

Screenshot from 2024-02-20 18-56-02

The commented lines where the original values.

Hope this helps!

@EdgarPozas
Copy link

Thanks!

@marwan2
Copy link

marwan2 commented Oct 28, 2024

@psk001 this worked for me on ubuntu 22.04
Add at the end of .bashrc file:

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\] $ "

@RaphNLab
Copy link

RaphNLab commented Dec 1, 2024

Thanks a lot.

@gsw945
Copy link

gsw945 commented Jun 30, 2025

The default PS1 for Ubuntu 24.04.2 LTS is \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ .
Based on your responses above, combined with the default PS1 value, the script I use is as below:

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\]\$ "

@rendsops
Copy link

I tried every solution in the internet (including this) but it never worked for me. Still I can not see the branch

you and i, buddy. you and i.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment