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

This comment has been minimized.

Copy link

@bmomani1 bmomani1 commented Oct 8, 2017

thanks

@muhaimincs

This comment has been minimized.

Copy link

@muhaimincs muhaimincs commented Oct 27, 2017

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

@ankurk91

This comment has been minimized.

Copy link
Owner Author

@ankurk91 ankurk91 commented Oct 28, 2017

@muhaimincs
Why you want minimalise to a folder?

@tadeubarbosa

This comment has been minimized.

Copy link

@tadeubarbosa tadeubarbosa commented Jan 8, 2018

thaks!

@marcdomain

This comment has been minimized.

Copy link

@marcdomain marcdomain commented Jan 30, 2018

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

@ankurk91

This comment has been minimized.

Copy link
Owner Author

@ankurk91 ankurk91 commented Jan 30, 2018

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

@pinglinh

This comment has been minimized.

Copy link

@pinglinh 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

This comment has been minimized.

Copy link
Owner Author

@ankurk91 ankurk91 commented Feb 8, 2018

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

@pinglinh

This comment has been minimized.

Copy link

@pinglinh pinglinh commented Feb 8, 2018

Thank you so much!! @ankurk91

@sonnesen

This comment has been minimized.

Copy link

@sonnesen sonnesen commented May 11, 2018

Works on Ubuntu 18.04. Thanks.

@julliasaad

This comment has been minimized.

Copy link

@julliasaad julliasaad commented May 14, 2018

thanks <3

@jptannus

This comment has been minimized.

Copy link

@jptannus jptannus commented May 29, 2018

It worked on Ubuntu 18.04 here too, thanks!

@fsierra

This comment has been minimized.

Copy link

@fsierra fsierra commented Aug 22, 2018

It worked on Ubuntu 18.04. Thanks!!

@eduardo-marcolino

This comment has been minimized.

Copy link

@eduardo-marcolino eduardo-marcolino commented Aug 27, 2018

Works on Xubuntu 18.04 too. Thanks!

@leorocco

This comment has been minimized.

Copy link

@leorocco leorocco commented Aug 28, 2018

Thanks!!! :D

@madiou84

This comment has been minimized.

Copy link

@madiou84 madiou84 commented Sep 8, 2018

Thanks

@weilinzung

This comment has been minimized.

Copy link

@weilinzung 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

This comment has been minimized.

Copy link

@ucavalcante ucavalcante commented Oct 3, 2018

tks and this work on Debian too

@lurodrig

This comment has been minimized.

Copy link

@lurodrig 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