Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Git Branch Name to Terminal Prompt (Mac)

Add Git Branch Name to Terminal Prompt (Mac)

image

Open ~/.bash_profile in your favorite editor and add the following content to the bottom.

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

By MARTIN FITZPATRICK at http://martinfitzpatrick.name/article/add-git-branch-name-to-terminal-prompt-mac/

@adyngom

This comment has been minimized.

Copy link

commented Nov 30, 2017

Awesome. Thanks :)

@ywang04

This comment has been minimized.

Copy link

commented Dec 7, 2017

Great, thanks a lot.

@shrikantm929

This comment has been minimized.

Copy link

commented Jan 22, 2018

super thanks

@agustisanchez

This comment has been minimized.

Copy link

commented Feb 27, 2018

Great!

@garecinos4

This comment has been minimized.

Copy link

commented Feb 28, 2018

nice!!

@ylihao

This comment has been minimized.

Copy link

commented Mar 6, 2018

Awesome stuff thanks!

@RandyGuck

This comment has been minimized.

Copy link

commented Mar 7, 2018

Just what I was looking for -- many thanks!

@hhemied

This comment has been minimized.

Copy link

commented Mar 12, 2018

Thank you ;)

@inoknalv

This comment has been minimized.

Copy link

commented Mar 13, 2018

Thank you for this 👍

@Domeny007

This comment has been minimized.

Copy link

commented Mar 16, 2018

Thanks

@bernardo-martinez

This comment has been minimized.

Copy link

commented Mar 27, 2018

thanks!. and remember to source ~/.bash_profile so you dont need to restart terminal

@kamono

This comment has been minimized.

Copy link

commented Apr 5, 2018

awesome possum!

@oroalej

This comment has been minimized.

Copy link

commented Apr 9, 2018

Hi, would like to ask how can i used this when I'm in sudo su? it gives me bash: parse_git_branch: command not found error when i'm in root. Thanks in advance <3

@khanlifts

This comment has been minimized.

Copy link

commented Apr 10, 2018

Soo simple, so effective.
This article helped me open .bash_profile and edit it:
https://natelandau.com/my-mac-osx-bash_profile/

@amondal2

This comment has been minimized.

Copy link

commented Apr 11, 2018

Awesome! was looking for exactly this.

@elishaking

This comment has been minimized.

Copy link

commented Apr 14, 2018

Great, thanks

@elbecita

This comment has been minimized.

Copy link

commented Apr 17, 2018

Sweet! Easy peasy. Thank you so much :)

@Jenpaulino3

This comment has been minimized.

Copy link

commented Apr 29, 2018

Thanks !

@kazitanvirahsan

This comment has been minimized.

Copy link

commented May 9, 2018

Thanks.

@cissygao

This comment has been minimized.

Copy link

commented May 24, 2018

Legend!

@undisputedashu

This comment has been minimized.

Copy link

commented May 24, 2018

Great thanks!!

@Krafalski

This comment has been minimized.

Copy link

commented May 25, 2018

Perfect! Thank you!

@jakoblundsted

This comment has been minimized.

Copy link

commented May 31, 2018

Thanks, this is awesome!

@sheetalwagh

This comment has been minimized.

Copy link

commented Jun 4, 2018

Thanks :)

@markcarrrr

This comment has been minimized.

Copy link

commented Jun 18, 2018

Great, thanks!

@ChuliangXiao

This comment has been minimized.

Copy link

commented Jun 20, 2018

Wonderful

@srivatsav

This comment has been minimized.

Copy link

commented Jun 21, 2018

Thanks a lot.

@mmmathur

This comment has been minimized.

Copy link

commented Jun 30, 2018

Thanks, this is exactly I was looking for

@amandakoster

This comment has been minimized.

Copy link

commented Jul 11, 2018

THANK YOU SO MUCH!

@tamil4kites

This comment has been minimized.

Copy link

commented Jul 22, 2018

Great......

@dagomx

This comment has been minimized.

Copy link

commented Jul 23, 2018

Thanks! This is very useful!

@neiloler

This comment has been minimized.

Copy link

commented Jul 24, 2018

Super handy, thanks!

@tetogomez

This comment has been minimized.

Copy link

commented Jul 26, 2018

thanks!!

@rainris

This comment has been minimized.

Copy link

commented Jul 27, 2018

Awesome!!

@Brsoyan

This comment has been minimized.

Copy link

commented Aug 16, 2018

Awesome. thanks!

@skarrman

This comment has been minimized.

Copy link

commented Aug 18, 2018

Great!

If one like the original syntax of the terminal you can use this template instead:

export PS1="\h:\W \u\[\033[32m\]\$(parse_git_branch)\[\033[00m\]$ "

@pratikch

This comment has been minimized.

Copy link

commented Sep 10, 2018

worked like charm!!!

@teocteles

This comment has been minimized.

Copy link

commented Sep 10, 2018

Thanks !!!!

@jorgeriv

This comment has been minimized.

Copy link

commented Sep 11, 2018

Thank you!

@julien-riel

This comment has been minimized.

Copy link

commented Sep 13, 2018

Thanks!

@aymos

This comment has been minimized.

Copy link

commented Sep 14, 2018

So useful!

@GzuPark

This comment has been minimized.

Copy link

commented Sep 21, 2018

Awesome! Thank you.

@ravikornu

This comment has been minimized.

Copy link

commented Sep 27, 2018

Thank you.

@shyam528

This comment has been minimized.

Copy link

commented Oct 25, 2018

It's Awesome :) Thanks

@danielnaranjo

This comment has been minimized.

Copy link

commented Oct 26, 2018

Thanks

@plaidpizazz

This comment has been minimized.

Copy link

commented Oct 26, 2018

Awesome!!

@nao360

This comment has been minimized.

Copy link

commented Oct 31, 2018

Finally ... no more periodic git branch 🥇

@sonus21

This comment has been minimized.

Copy link

commented Nov 2, 2018

Wow! Simple impressive 👍

@reshat2

This comment has been minimized.

Copy link

commented Nov 2, 2018

Awesome. Thanks.

@eddiewentw

This comment has been minimized.

Copy link

commented Nov 4, 2018

It works perfectly!!

@hkcoyant

This comment has been minimized.

Copy link

commented Nov 5, 2018

thanks!! work perfect on ubuntu 18.04 put in ~/.profile file

@bodikqlar

This comment has been minimized.

Copy link

commented Nov 5, 2018

thanks

@Mohendran

This comment has been minimized.

Copy link

commented Nov 7, 2018

Awesome

@rramirez0203

This comment has been minimized.

Copy link

commented Nov 7, 2018

thanks!

@nikhilesh-mittal

This comment has been minimized.

Copy link

commented Nov 8, 2018

Awesome :)

@mojo35

This comment has been minimized.

Copy link

commented Nov 10, 2018

Thank you so much!!!

@strelnykoff

This comment has been minimized.

Copy link

commented Nov 11, 2018

thanks

@vchengsong

This comment has been minimized.

Copy link

commented Nov 12, 2018

Awesome :)

@LOGANLEEE

This comment has been minimized.

Copy link

commented Nov 15, 2018

YOU'RE LOVELY!

@panSarin

This comment has been minimized.

Copy link

commented Nov 20, 2018

Thank you man ;]

@alprestedge

This comment has been minimized.

Copy link

commented Nov 26, 2018

SO simple - exactly what i needed!!

@t10kiyotaka

This comment has been minimized.

Copy link

commented Nov 27, 2018

Great!!!

@hopewise

This comment has been minimized.

Copy link

commented Nov 27, 2018

Thanks

@vivekvijaykumar

This comment has been minimized.

Copy link

commented Nov 28, 2018

Nice. Thanks!

@devtagare

This comment has been minimized.

Copy link

commented Nov 28, 2018

Awesome. Thanks!

@pouriaalmassi

This comment has been minimized.

Copy link

commented Nov 29, 2018

You the man.

@akine

This comment has been minimized.

Copy link

commented Nov 30, 2018

awesome! thank!!!!! <3

@testradav

This comment has been minimized.

Copy link

commented Nov 30, 2018

thanks!!! 👍

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
# git branch 2>/dev/null | /usr/bin/grep '^*' | colrm 1 2)"
}

PS1_date="\[\033[38;5;237m\]\d\[$(tput sgr0)\]\[\033[38;5;15m\]"
PS1_time="\[$(tput sgr0)\]\[\033[38;5;236m\]\t\[$(tput sgr0)\]\[\033[38;5;15m\]"
PS1_wdir="\[$(tput sgr0)\]\[\033[38;5;24m\]\W"
PS1_gitbranch="\e[38;5;204m\]\$(parse_git_branch)"
PS1_gt="\[$(tput bold)\]\[$(tput sgr0)\]\[\e[38;5;214m\]>"
PS1_other="\[$(tput sgr0)\]\[$(tput sgr0)\]\[\e[38;5;15m\]"

export PS1="${PS1_date} ${PS1_time} ${PS1_wdir}${PS1_gitbranch}${PS1_gt}${PS1_other} \[$(tput sgr0)\]"

image

@shaolinrabbi

This comment has been minimized.

Copy link

commented Dec 8, 2018

Perfect!

@amahsanferoz

This comment has been minimized.

Copy link

commented Dec 11, 2018

Thanks! Awesome!

@shalinjames

This comment has been minimized.

Copy link

commented Dec 14, 2018

Thanks a lot!.. Saved me!

@oborovets

This comment has been minimized.

Copy link

commented Dec 14, 2018

Awesome

@aisin

This comment has been minimized.

Copy link

commented Dec 15, 2018

Awesome! Thanks.

@alejandroreynagit

This comment has been minimized.

Copy link

commented Dec 17, 2018

Uufff, thanks!

@thomashibbard

This comment has been minimized.

Copy link

commented Dec 18, 2018

Perfect!

@ktajpuri

This comment has been minimized.

Copy link

commented Dec 28, 2018

Thanks dude

@claytonrm

This comment has been minimized.

Copy link

commented Dec 29, 2018

Thanks! '-'

@Mph1399

This comment has been minimized.

Copy link

commented Jan 5, 2019

Thanks!

@codecapsules

This comment has been minimized.

Copy link

commented Jan 12, 2019

Awesome 1000 thanks !

@joeyk16

This comment has been minimized.

Copy link

commented Jan 14, 2019

Thank you very much!

@vishwakt

This comment has been minimized.

Copy link

commented Jan 14, 2019

Awesome! Thanks! :D

@nikita-ahuja

This comment has been minimized.

Copy link

commented Jan 14, 2019

Great, thanks!

@glowfinger

This comment has been minimized.

Copy link

commented Jan 15, 2019

Thanks!

@avinash2209

This comment has been minimized.

Copy link

commented Jan 17, 2019

Thank you.

@chadoblinds

This comment has been minimized.

Copy link

commented Jan 18, 2019

Dope! Thank you!

@sarazafe

This comment has been minimized.

Copy link

commented Jan 21, 2019

Thanks a lot!!

@h3nr1ke

This comment has been minimized.

Copy link

commented Jan 23, 2019

thanks =)

@Gilbert1391

This comment has been minimized.

Copy link

commented Jan 24, 2019

Thanks!

@gupta-pratik

This comment has been minimized.

Copy link

commented Jan 25, 2019

Awesome.. thanks! 💯

@mghsuchak

This comment has been minimized.

Copy link

commented Jan 29, 2019

Thanks

@krnblni

This comment has been minimized.

Copy link

commented Jan 31, 2019

Thank you so much! :)

@AndersonLorena

This comment has been minimized.

Copy link

commented Feb 7, 2019

Thanks so much!

Also, I just would like to share a small change in order to start the cursor on the next line:
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\]\n$ "

Cheers,

@winnosuge

This comment has been minimized.

Copy link

commented Feb 13, 2019

Thanks!!

@ShawnToubeau

This comment has been minimized.

Copy link

commented Feb 22, 2019

You are my hero!

@cristianwork

This comment has been minimized.

Copy link

commented Feb 28, 2019

Thanks !

@KenkenIchimura

This comment has been minimized.

Copy link

commented Mar 6, 2019

Thanks!

@afed79

This comment has been minimized.

Copy link

commented Mar 10, 2019

Soo simple, so effective.
This article helped me open .bash_profile and edit it:
https://natelandau.com/my-mac-osx-bash_profile/

Helped me too! Thanks for the link

@roujdami

This comment has been minimized.

Copy link

commented Mar 21, 2019

Nice Thanks

@alisatl

This comment has been minimized.

Copy link

commented Mar 25, 2019

Thanks!!!

@oneEyedSunday

This comment has been minimized.

Copy link

commented Apr 2, 2019

Thanks man. Thankfully GH gist doesnt protect gists like Stack Overflow :)

@jubafourali

This comment has been minimized.

Copy link

commented Apr 8, 2019

Awesome thank's

@cudacoder

This comment has been minimized.

Copy link

commented Apr 8, 2019

Nice!
You should checkout Posh-Git, pair it with Pshazz for stylish themes and you have a sane Windows development environment...

@christopherfarrugia

This comment has been minimized.

Copy link

commented Apr 10, 2019

Thank you!

@christopherfarrugia

This comment has been minimized.

Copy link

commented Apr 10, 2019

Thank you!

@Kawaljit

This comment has been minimized.

Copy link

commented Apr 11, 2019

Thank you!
The following article helped me change the colour.
https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

@tessapower

This comment has been minimized.

Copy link

commented Apr 18, 2019

Thank you — super easy!

@ganesh-sapient

This comment has been minimized.

Copy link

commented Apr 21, 2019

Awesome

@madansmriti

This comment has been minimized.

Copy link

commented Apr 21, 2019

Perfect! Thank you!

@cristofer-dev

This comment has been minimized.

Copy link

commented Apr 22, 2019

Thanks!

@harsh3150

This comment has been minimized.

Copy link

commented Apr 24, 2019

thanks bro
if anyone is wondering how to open bash_profile then write down these things in terminal
step1= cd
step2= nano .bash_profile
step3= paste this at the bottom

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] $ "
step4= press control+o then press enter then press control+x
step5= source .bash_profile
all done

@manojpanta

This comment has been minimized.

Copy link

commented May 8, 2019

Thanks.

@lentty

This comment has been minimized.

Copy link

commented May 8, 2019

Thanks

@AppManNYC

This comment has been minimized.

Copy link

commented May 9, 2019

Fantastic!!!

@lucasmathioni

This comment has been minimized.

Copy link

commented May 15, 2019

thanks!

@srikar-kini-mc

This comment has been minimized.

Copy link

commented May 15, 2019

superb, so useful. Thanks.

@halfnibble

This comment has been minimized.

Copy link

commented May 18, 2019

Make the color blue instead?

@KITSEPAMIgorSergiienko

This comment has been minimized.

Copy link

commented May 20, 2019

thank you!

@soniapatel

This comment has been minimized.

Copy link

commented May 24, 2019

thanks!

@andrewh-itison

This comment has been minimized.

Copy link

commented May 29, 2019

Thanks, simple and effective. :-)

@joaquin386

This comment has been minimized.

Copy link

commented Jun 6, 2019

Nice!!! No need to execute "git branch" everytime. Thanks

@qufeichen

This comment has been minimized.

Copy link

commented Jun 7, 2019

Amazing, thank you!!

@israman30

This comment has been minimized.

Copy link

commented Jun 12, 2019

Awesome..!! thanks!

@kevinkq

This comment has been minimized.

Copy link

commented Jun 12, 2019

Nice. Thank you!

@42development

This comment has been minimized.

Copy link

commented Jun 16, 2019

cool!

@Hussain-AboAlamal

This comment has been minimized.

Copy link

commented Jun 16, 2019

worked for me but its override my previous customisations. text color changed, and current path gone

@turutosiya

This comment has been minimized.

Copy link

commented Jun 18, 2019

Super Duper Thanks!

@ulkoart

This comment has been minimized.

Copy link

commented Jun 21, 2019

Thanks!

@imalic3

This comment has been minimized.

Copy link

commented Jun 24, 2019

awesome work, thank!! <3

@binoopmj

This comment has been minimized.

Copy link

commented Jun 24, 2019

Great

@rsmelo92

This comment has been minimized.

Copy link

commented Jun 24, 2019

Nice!!!!

@daddach

This comment has been minimized.

Copy link

commented Jun 25, 2019

you make my day!!!

@esker

This comment has been minimized.

Copy link

commented Jul 10, 2019

THANKS

@Ogala

This comment has been minimized.

Copy link

commented Jul 15, 2019

Awesome!

@mattBrzezinski

This comment has been minimized.

Copy link

commented Jul 18, 2019

👍 Thank you!

@efrapp

This comment has been minimized.

Copy link

commented Jul 29, 2019

Thanks man!

@juanmarquezg

This comment has been minimized.

Copy link

commented Aug 3, 2019

nice!

@benoitdemaegdt

This comment has been minimized.

Copy link

commented Aug 8, 2019

Great ! Thanks !

@Marco-at-D4L

This comment has been minimized.

Copy link

commented Aug 15, 2019

Cool, that really helps. Thanks :)

@BeijiYang

This comment has been minimized.

Copy link

commented Aug 20, 2019

Awesome.

@deweydell

This comment has been minimized.

Copy link

commented Aug 21, 2019

THANK YOU

@rivkahcarl

This comment has been minimized.

Copy link

commented Aug 22, 2019

Thank you! this is great!

@sahildorwat

This comment has been minimized.

Copy link

commented Aug 24, 2019

thanks

@arvind-kumar-avinash

This comment has been minimized.

Copy link

commented Aug 26, 2019

Awesome!

I modified the last line of the script as follows as per my taste:

export PS1="[\w]\[\033[32m\]\$(parse_git_branch)\[\033[00m\]: "

@magpoc

This comment has been minimized.

Copy link

commented Sep 12, 2019

Thank you!

FYI, The color of the branch name can be changed by replace the 32m part as follows.

30m black
31m red
32m green
33m yellow
34m blue
35m purple
36m cyan
37m gray

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.