Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
:octocat: Git branch name in Linux/Mac Bash Terminal

Mac OS : Show your git branch name on your bash terminal

⚠️ Does not work in zsh terminal

Add these lines in your ~/.bash_profile file

# Show current git branch name
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\] $ "

Alternate approach - Don't show full path

# Git branch in prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
  • Quit and re-open your terminal , cd to a git repo folder

Tested on macOS Sierra/High Sierra/Mojave (default bash terminal)

Source, Source2

Ubuntu: Show your git 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
  • Close and re-open your gnome-terminal , cd to a git repo folder

Source

Tested on Ubuntu 18.04/20.04 on gnome terminal

@bmomani1
Copy link

bmomani1 commented Oct 8, 2017

thanks

@muhaimincs
Copy link

muhaimincs commented Oct 27, 2017

how do I minimalise it to folder project and branch name in bracket

@ankurk91
Copy link
Author

ankurk91 commented Oct 28, 2017

@muhaimincs
Why you want minimalise to a folder?

@tadeubdev
Copy link

tadeubdev commented Jan 8, 2018

thaks!

@marcdomain
Copy link

marcdomain commented Jan 30, 2018

Please how do I customize the colour of fonts on Ubuntu Terminator

@ankurk91
Copy link
Author

ankurk91 commented Jan 30, 2018

Why do you want to customise Color on your terminator :)

@pinglinh
Copy link

pinglinh commented Feb 7, 2018

Hey thanks for posting this!

How can I minimize the folder structure? for example it's showing me ~/Documents/academy
but if I cd'ed into academy, I only want it to show academy and not documents anymore

@ankurk91
Copy link
Author

ankurk91 commented Feb 8, 2018

@pinglinh
I have added alternate method. See updated gist above.

@pinglinh
Copy link

pinglinh commented Feb 8, 2018

Thank you so much!! @ankurk91

@sonnesen
Copy link

sonnesen commented May 11, 2018

Works on Ubuntu 18.04. Thanks.

@julliasaad
Copy link

julliasaad commented May 14, 2018

thanks <3

@jptannus
Copy link

jptannus commented May 29, 2018

It worked on Ubuntu 18.04 here too, thanks!

@fsierra
Copy link

fsierra commented Aug 22, 2018

It worked on Ubuntu 18.04. Thanks!!

@eduardo-marcolino
Copy link

eduardo-marcolino commented Aug 27, 2018

Works on Xubuntu 18.04 too. Thanks!

@leorocco
Copy link

leorocco commented Aug 28, 2018

Thanks!!! :D

@madiou84
Copy link

madiou84 commented Sep 8, 2018

Thanks

@weilinzung
Copy link

weilinzung commented Sep 28, 2018

Is possible to add the current branch name on all the timestamp lines? Something like this:

FROM:

[18:37:39] Starting 'build-js'...
[18:37:49] finished 'build-js'...

TO:

[18:37:39 - **current-branch-name** ] Starting 'build-js'...
[18:37:49 - **current-branch-name** ] finished 'build-js'...

Thanks.

@ucavalcante
Copy link

ucavalcante commented Oct 3, 2018

tks and this work on Debian too

@lurodrig
Copy link

lurodrig commented Mar 18, 2019

It works, thanks!

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