Skip to content

Instantly share code, notes, and snippets.

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

Add Git Branch Name to Terminal Prompt (Linux/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/

Bonus for Fish shell users

If you are Fish shell user and you want speed up your Git workflow, try it out https://github.com/joseluisq/gitnow.

@adyngom

This comment has been minimized.

Copy link

@adyngom adyngom commented Nov 30, 2017

Awesome. Thanks :)

@ywang04

This comment has been minimized.

Copy link

@ywang04 ywang04 commented Dec 7, 2017

Great, thanks a lot.

@shrikantm929

This comment has been minimized.

Copy link

@shrikantm929 shrikantm929 commented Jan 22, 2018

super thanks

@agustisanchez

This comment has been minimized.

Copy link

@agustisanchez agustisanchez commented Feb 27, 2018

Great!

@garecinos4

This comment has been minimized.

Copy link

@garecinos4 garecinos4 commented Feb 28, 2018

nice!!

@ylihao

This comment has been minimized.

Copy link

@ylihao ylihao commented Mar 6, 2018

Awesome stuff thanks!

@RandyGuck

This comment has been minimized.

Copy link

@RandyGuck RandyGuck commented Mar 7, 2018

Just what I was looking for -- many thanks!

@hhemied

This comment has been minimized.

Copy link

@hhemied hhemied commented Mar 12, 2018

Thank you ;)

@inoknalv

This comment has been minimized.

Copy link

@inoknalv inoknalv commented Mar 13, 2018

Thank you for this 👍

@Domeny007

This comment has been minimized.

Copy link

@Domeny007 Domeny007 commented Mar 16, 2018

Thanks

@bernardo-martinez

This comment has been minimized.

Copy link

@bernardo-martinez bernardo-martinez 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

@kamono kamono commented Apr 5, 2018

awesome possum!

@oroalej

This comment has been minimized.

Copy link

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

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

@amondal2 amondal2 commented Apr 11, 2018

Awesome! was looking for exactly this.

@elishaking

This comment has been minimized.

Copy link

@elishaking elishaking commented Apr 14, 2018

Great, thanks

@elbecita

This comment has been minimized.

Copy link

@elbecita elbecita commented Apr 17, 2018

Sweet! Easy peasy. Thank you so much :)

@Jenpaulino3

This comment has been minimized.

Copy link

@Jenpaulino3 Jenpaulino3 commented Apr 29, 2018

Thanks !

@kazitanvirahsan

This comment has been minimized.

Copy link

@kazitanvirahsan kazitanvirahsan commented May 9, 2018

Thanks.

@cissygao

This comment has been minimized.

Copy link

@cissygao cissygao commented May 24, 2018

Legend!

@undisputedashu

This comment has been minimized.

Copy link

@undisputedashu undisputedashu commented May 24, 2018

Great thanks!!

@Krafalski

This comment has been minimized.

Copy link

@Krafalski Krafalski commented May 25, 2018

Perfect! Thank you!

@jakoblundsted

This comment has been minimized.

Copy link

@jakoblundsted jakoblundsted commented May 31, 2018

Thanks, this is awesome!

@sheetalwagh

This comment has been minimized.

Copy link

@sheetalwagh sheetalwagh commented Jun 4, 2018

Thanks :)

@markcarrrr

This comment has been minimized.

Copy link

@markcarrrr markcarrrr commented Jun 18, 2018

Great, thanks!

@ChuliangXiao

This comment has been minimized.

Copy link

@ChuliangXiao ChuliangXiao commented Jun 20, 2018

Wonderful

@srivatsav

This comment has been minimized.

Copy link

@srivatsav srivatsav commented Jun 21, 2018

Thanks a lot.

@mmmathur

This comment has been minimized.

Copy link

@mmmathur mmmathur commented Jun 30, 2018

Thanks, this is exactly I was looking for

@amandakoster

This comment has been minimized.

Copy link

@amandakoster amandakoster commented Jul 11, 2018

THANK YOU SO MUCH!

@tamil4kites

This comment has been minimized.

Copy link

@tamil4kites tamil4kites commented Jul 22, 2018

Great......

@dagomx

This comment has been minimized.

Copy link

@dagomx dagomx commented Jul 23, 2018

Thanks! This is very useful!

@neiloler

This comment has been minimized.

Copy link

@neiloler neiloler commented Jul 24, 2018

Super handy, thanks!

@tetogomez

This comment has been minimized.

Copy link

@tetogomez tetogomez commented Jul 26, 2018

thanks!!

@rainris

This comment has been minimized.

Copy link

@rainris rainris commented Jul 27, 2018

Awesome!!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Aug 16, 2018

Awesome. thanks!

@skarrman

This comment has been minimized.

Copy link

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

@pratikch pratikch commented Sep 10, 2018

worked like charm!!!

@teocteles

This comment has been minimized.

Copy link

@teocteles teocteles commented Sep 10, 2018

Thanks !!!!

@jorgeriv

This comment has been minimized.

Copy link

@jorgeriv jorgeriv commented Sep 11, 2018

Thank you!

@julien-riel

This comment has been minimized.

Copy link

@julien-riel julien-riel commented Sep 13, 2018

Thanks!

@aymos

This comment has been minimized.

Copy link

@aymos aymos commented Sep 14, 2018

So useful!

@GzuPark

This comment has been minimized.

Copy link

@GzuPark GzuPark commented Sep 21, 2018

Awesome! Thank you.

@ravikornu

This comment has been minimized.

Copy link

@ravikornu ravikornu commented Sep 27, 2018

Thank you.

@shyam528

This comment has been minimized.

Copy link

@shyam528 shyam528 commented Oct 25, 2018

It's Awesome :) Thanks

@danielnaranjo

This comment has been minimized.

Copy link

@danielnaranjo danielnaranjo commented Oct 26, 2018

Thanks

@plaidpizazz

This comment has been minimized.

Copy link

@plaidpizazz plaidpizazz commented Oct 26, 2018

Awesome!!

@nao360

This comment has been minimized.

Copy link

@nao360 nao360 commented Oct 31, 2018

Finally ... no more periodic git branch 🥇

@sonus21

This comment has been minimized.

Copy link

@sonus21 sonus21 commented Nov 2, 2018

Wow! Simple impressive 👍

@reshat2

This comment has been minimized.

Copy link

@reshat2 reshat2 commented Nov 2, 2018

Awesome. Thanks.

@eddiewentw

This comment has been minimized.

Copy link

@eddiewentw eddiewentw commented Nov 4, 2018

It works perfectly!!

@hkcoyant

This comment has been minimized.

Copy link

@hkcoyant hkcoyant commented Nov 5, 2018

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

@bodikqlar

This comment has been minimized.

Copy link

@bodikqlar bodikqlar commented Nov 5, 2018

thanks

@Mohendran

This comment has been minimized.

Copy link

@Mohendran Mohendran commented Nov 7, 2018

Awesome

@rramirez0203

This comment has been minimized.

Copy link

@rramirez0203 rramirez0203 commented Nov 7, 2018

thanks!

@nikhilesh-mittal

This comment has been minimized.

Copy link

@nikhilesh-mittal nikhilesh-mittal commented Nov 8, 2018

Awesome :)

@mojo35

This comment has been minimized.

Copy link

@mojo35 mojo35 commented Nov 10, 2018

Thank you so much!!!

@strelnykoff

This comment has been minimized.

Copy link

@strelnykoff strelnykoff commented Nov 11, 2018

thanks

@vchengsong

This comment has been minimized.

Copy link

@vchengsong vchengsong commented Nov 12, 2018

Awesome :)

@LOGANLEEE

This comment has been minimized.

Copy link

@LOGANLEEE LOGANLEEE commented Nov 15, 2018

YOU'RE LOVELY!

@panSarin

This comment has been minimized.

Copy link

@panSarin panSarin commented Nov 20, 2018

Thank you man ;]

@alprestedge

This comment has been minimized.

Copy link

@alprestedge alprestedge commented Nov 26, 2018

SO simple - exactly what i needed!!

@t10kiyotaka

This comment has been minimized.

Copy link

@t10kiyotaka t10kiyotaka commented Nov 27, 2018

Great!!!

@hopewise

This comment has been minimized.

Copy link

@hopewise hopewise commented Nov 27, 2018

Thanks

@vivekvijaykumar

This comment has been minimized.

Copy link

@vivekvijaykumar vivekvijaykumar commented Nov 28, 2018

Nice. Thanks!

@devtagare

This comment has been minimized.

Copy link

@devtagare devtagare commented Nov 28, 2018

Awesome. Thanks!

@pouriaalmassi

This comment has been minimized.

Copy link

@pouriaalmassi pouriaalmassi commented Nov 29, 2018

You the man.

@akine

This comment has been minimized.

Copy link

@akine akine commented Nov 30, 2018

awesome! thank!!!!! <3

@testradav

This comment has been minimized.

Copy link

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

@shaolinrabbi shaolinrabbi commented Dec 8, 2018

Perfect!

@amahsanferoz

This comment has been minimized.

Copy link

@amahsanferoz amahsanferoz commented Dec 11, 2018

Thanks! Awesome!

@shalinjames

This comment has been minimized.

Copy link

@shalinjames shalinjames commented Dec 14, 2018

Thanks a lot!.. Saved me!

@oborovets

This comment has been minimized.

Copy link

@oborovets oborovets commented Dec 14, 2018

Awesome

@aisin

This comment has been minimized.

Copy link

@aisin aisin commented Dec 15, 2018

Awesome! Thanks.

@alejandroreynagit

This comment has been minimized.

Copy link

@alejandroreynagit alejandroreynagit commented Dec 17, 2018

Uufff, thanks!

@thomashibbard

This comment has been minimized.

Copy link

@thomashibbard thomashibbard commented Dec 18, 2018

Perfect!

@ktajpuri

This comment has been minimized.

Copy link

@ktajpuri ktajpuri commented Dec 28, 2018

Thanks dude

@claytonrm

This comment has been minimized.

Copy link

@claytonrm claytonrm commented Dec 29, 2018

Thanks! '-'

@Mph1399

This comment has been minimized.

Copy link

@Mph1399 Mph1399 commented Jan 5, 2019

Thanks!

@codecapsules

This comment has been minimized.

Copy link

@codecapsules codecapsules commented Jan 12, 2019

Awesome 1000 thanks !

@joeyk16

This comment has been minimized.

Copy link

@joeyk16 joeyk16 commented Jan 14, 2019

Thank you very much!

@vishwakt

This comment has been minimized.

Copy link

@vishwakt vishwakt commented Jan 14, 2019

Awesome! Thanks! :D

@nikita-ahuja

This comment has been minimized.

Copy link

@nikita-ahuja nikita-ahuja commented Jan 14, 2019

Great, thanks!

@glowfinger

This comment has been minimized.

Copy link

@glowfinger glowfinger commented Jan 15, 2019

Thanks!

@avinash2209

This comment has been minimized.

Copy link

@avinash2209 avinash2209 commented Jan 17, 2019

Thank you.

@chadostroff

This comment has been minimized.

Copy link

@chadostroff chadostroff commented Jan 18, 2019

Dope! Thank you!

@sarazafe

This comment has been minimized.

Copy link

@sarazafe sarazafe commented Jan 21, 2019

Thanks a lot!!

@h3nr1ke

This comment has been minimized.

Copy link

@h3nr1ke h3nr1ke commented Jan 23, 2019

thanks =)

@Gilbert1391

This comment has been minimized.

Copy link

@Gilbert1391 Gilbert1391 commented Jan 24, 2019

Thanks!

@gupta-pratik

This comment has been minimized.

Copy link

@gupta-pratik gupta-pratik commented Jan 25, 2019

Awesome.. thanks! 💯

@mghsuchak

This comment has been minimized.

Copy link

@mghsuchak mghsuchak commented Jan 29, 2019

Thanks

@krnblni

This comment has been minimized.

Copy link

@krnblni krnblni commented Jan 31, 2019

Thank you so much! :)

@AndersonLorena

This comment has been minimized.

Copy link

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

@winnosuge winnosuge commented Feb 13, 2019

Thanks!!

@ShawnToubeau

This comment has been minimized.

Copy link

@ShawnToubeau ShawnToubeau commented Feb 22, 2019

You are my hero!

@cristianwork

This comment has been minimized.

Copy link

@cristianwork cristianwork commented Feb 28, 2019

Thanks !

@KenkenIchimura

This comment has been minimized.

Copy link

@KenkenIchimura KenkenIchimura commented Mar 6, 2019

Thanks!

@afed79

This comment has been minimized.

Copy link

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

@roujdami roujdami commented Mar 21, 2019

Nice Thanks

@alisatl

This comment has been minimized.

Copy link

@alisatl alisatl commented Mar 25, 2019

Thanks!!!

@oneEyedSunday

This comment has been minimized.

Copy link

@oneEyedSunday oneEyedSunday commented Apr 2, 2019

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

@jubafourali

This comment has been minimized.

Copy link

@jubafourali jubafourali commented Apr 8, 2019

Awesome thank's

@cudacoder

This comment has been minimized.

Copy link

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

@christopherfarrugia christopherfarrugia commented Apr 10, 2019

Thank you!

@christopherfarrugia

This comment has been minimized.

Copy link

@christopherfarrugia christopherfarrugia commented Apr 10, 2019

Thank you!

@Kawaljit

This comment has been minimized.

Copy link

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

@tessapower tessapower commented Apr 18, 2019

Thank you — super easy!

@ganesh-sapient

This comment has been minimized.

Copy link

@ganesh-sapient ganesh-sapient commented Apr 21, 2019

Awesome

@madansmriti

This comment has been minimized.

Copy link

@madansmriti madansmriti commented Apr 21, 2019

Perfect! Thank you!

@cristofer-dev

This comment has been minimized.

Copy link

@cristofer-dev cristofer-dev commented Apr 22, 2019

Thanks!

@harsh3150

This comment has been minimized.

Copy link

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

@manojpanta manojpanta commented May 8, 2019

Thanks.

@lentty

This comment has been minimized.

Copy link

@lentty lentty commented May 8, 2019

Thanks

@AppManNYC

This comment has been minimized.

Copy link

@AppManNYC AppManNYC commented May 9, 2019

Fantastic!!!

@lucasmathioni

This comment has been minimized.

Copy link

@lucasmathioni lucasmathioni commented May 15, 2019

thanks!

@srikar-kini-mc

This comment has been minimized.

Copy link

@srikar-kini-mc srikar-kini-mc commented May 15, 2019

superb, so useful. Thanks.

@halfnibble

This comment has been minimized.

Copy link

@halfnibble halfnibble commented May 18, 2019

Make the color blue instead?

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented May 20, 2019

thank you!

@soniapatel

This comment has been minimized.

Copy link

@soniapatel soniapatel commented May 24, 2019

thanks!

@andrewh-itison

This comment has been minimized.

Copy link

@andrewh-itison andrewh-itison commented May 29, 2019

Thanks, simple and effective. :-)

@joaquin386

This comment has been minimized.

Copy link

@joaquin386 joaquin386 commented Jun 6, 2019

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

@qufeichen

This comment has been minimized.

Copy link

@qufeichen qufeichen commented Jun 7, 2019

Amazing, thank you!!

@israman30

This comment has been minimized.

Copy link

@israman30 israman30 commented Jun 12, 2019

Awesome..!! thanks!

@kevinkq

This comment has been minimized.

Copy link

@kevinkq kevinkq commented Jun 12, 2019

Nice. Thank you!

@Hussain-AboAlamal

This comment has been minimized.

Copy link

@Hussain-AboAlamal Hussain-AboAlamal 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

@turutosiya turutosiya commented Jun 18, 2019

Super Duper Thanks!

@ulkoart

This comment has been minimized.

Copy link

@ulkoart ulkoart commented Jun 21, 2019

Thanks!

@imalic3

This comment has been minimized.

Copy link

@imalic3 imalic3 commented Jun 24, 2019

awesome work, thank!! <3

@binoopmj

This comment has been minimized.

Copy link

@binoopmj binoopmj commented Jun 24, 2019

Great

@rsmelo92

This comment has been minimized.

Copy link

@rsmelo92 rsmelo92 commented Jun 24, 2019

Nice!!!!

@daddach

This comment has been minimized.

Copy link

@daddach daddach commented Jun 25, 2019

you make my day!!!

@esker

This comment has been minimized.

Copy link

@esker esker commented Jul 10, 2019

THANKS

@Ogala

This comment has been minimized.

Copy link

@Ogala Ogala commented Jul 15, 2019

Awesome!

@mattBrzezinski

This comment has been minimized.

Copy link

@mattBrzezinski mattBrzezinski commented Jul 18, 2019

👍 Thank you!

@efrapp

This comment has been minimized.

Copy link

@efrapp efrapp commented Jul 29, 2019

Thanks man!

@juanmarquezg

This comment has been minimized.

Copy link

@juanmarquezg juanmarquezg commented Aug 3, 2019

nice!

@benoitdemaegdt

This comment has been minimized.

Copy link

@benoitdemaegdt benoitdemaegdt commented Aug 8, 2019

Great ! Thanks !

@Marco-at-D4L

This comment has been minimized.

Copy link

@Marco-at-D4L Marco-at-D4L commented Aug 15, 2019

Cool, that really helps. Thanks :)

@BeijiYang

This comment has been minimized.

Copy link

@BeijiYang BeijiYang commented Aug 20, 2019

Awesome.

@deweydell

This comment has been minimized.

Copy link

@deweydell deweydell commented Aug 21, 2019

THANK YOU

@rivkahcarl

This comment has been minimized.

Copy link

@rivkahcarl rivkahcarl commented Aug 22, 2019

Thank you! this is great!

@sahildorwat

This comment has been minimized.

Copy link

@sahildorwat sahildorwat commented Aug 24, 2019

thanks

@arvind-kumar-avinash

This comment has been minimized.

Copy link

@arvind-kumar-avinash arvind-kumar-avinash 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

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

@patrykho

This comment has been minimized.

Copy link

@patrykho patrykho commented Sep 17, 2019

Thank You!

@fkirc

This comment has been minimized.

Copy link

@fkirc fkirc commented Sep 25, 2019

Thanks

@borisovdenis-kb

This comment has been minimized.

Copy link

@borisovdenis-kb borisovdenis-kb commented Sep 26, 2019

Thank you, sir

@alfdev

This comment has been minimized.

Copy link

@alfdev alfdev commented Oct 5, 2019

Thanks!

@XCarbone

This comment has been minimized.

Copy link

@XCarbone XCarbone commented Oct 9, 2019

Awesome

@d3nisko

This comment has been minimized.

Copy link

@d3nisko d3nisko commented Oct 17, 2019

nice

@Manuel4131

This comment has been minimized.

Copy link

@Manuel4131 Manuel4131 commented Oct 18, 2019

I'll appreciate it if the console can display path.

@mitchellglazier

This comment has been minimized.

Copy link

@mitchellglazier mitchellglazier commented Oct 18, 2019

Very concise solution, thanks

@emiviada

This comment has been minimized.

Copy link

@emiviada emiviada commented Oct 31, 2019

Awesome +1

@him11

This comment has been minimized.

Copy link

@him11 him11 commented Nov 12, 2019

Great. This worked. It would have been awesome, if it also could display the complete path.

@ofuochi

This comment has been minimized.

Copy link

@ofuochi ofuochi commented Nov 18, 2019

Works perfectly. Thank you.

@NimeshJadav

This comment has been minimized.

Copy link

@NimeshJadav NimeshJadav commented Nov 21, 2019

what's for the ubuntu?

@Ogala

This comment has been minimized.

Copy link

@Ogala Ogala commented Nov 22, 2019

what's for the ubuntu?

@NimeshJadav Check this post on StackExchange for directions. https://askubuntu.com/questions/730754/how-do-i-show-the-git-branch-with-colours-in-bash-prompt

@rubenruvalcaba-mac

This comment has been minimized.

Copy link

@rubenruvalcaba-mac rubenruvalcaba-mac commented Nov 24, 2019

awesome, thank you

@gumpen

This comment has been minimized.

Copy link

@gumpen gumpen commented Nov 25, 2019

thank you!

@NimeshJadav

This comment has been minimized.

Copy link

@NimeshJadav NimeshJadav commented Nov 26, 2019

what's for the ubuntu?

@NimeshJadav Check this post on StackExchange for directions. https://askubuntu.com/questions/730754/how-do-i-show-the-git-branch-with-colours-in-bash-prompt

Thank you. @Ogala

@gtludwig

This comment has been minimized.

Copy link

@gtludwig gtludwig commented Nov 29, 2019

Legend!

@Aleyasen

This comment has been minimized.

Copy link

@Aleyasen Aleyasen commented Dec 3, 2019

Terrific!

@sebastiaorealino

This comment has been minimized.

Copy link

@sebastiaorealino sebastiaorealino commented Dec 5, 2019

Nice!!!!

@anitaokoh

This comment has been minimized.

@reinvanoyen

This comment has been minimized.

Copy link

@reinvanoyen reinvanoyen commented Dec 14, 2019

This has been my go-to resource for this kind of thing for a while now. Thank you!

@josepedrodiaz

This comment has been minimized.

Copy link

@josepedrodiaz josepedrodiaz commented Dec 17, 2019

I do this changes but now I get on terminal instead of path:

\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $

Im on Catalina

@reinvanoyen

This comment has been minimized.

Copy link

@reinvanoyen reinvanoyen commented Dec 17, 2019

I do this changes but now I get on terminal instead of path:

\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $

Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html

Here is an example of what I came up with (zsh):

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '
@josepedrodiaz

This comment has been minimized.

Copy link

@josepedrodiaz josepedrodiaz commented Dec 17, 2019

I do this changes but now I get on terminal instead of path:
\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $
Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html

Here is an example of what I came up with (zsh):

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Works like a charm on Catalina, thanks!

@Davizf

This comment has been minimized.

Copy link

@Davizf Davizf commented Dec 19, 2019

I do this changes but now I get on terminal instead of path:
\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $
Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html

Here is an example of what I came up with (zsh):

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Thk a lot!

@alfredobarron

This comment has been minimized.

Copy link

@alfredobarron alfredobarron commented Dec 20, 2019

Thanks bro! ¡Saludos desde México!

@aninchiron

This comment has been minimized.

Copy link

@aninchiron aninchiron commented Dec 24, 2019

Thanks !

@climardo

This comment has been minimized.

Copy link

@climardo climardo commented Jan 3, 2020

Thank you! This is all I was looking for and others had some complicated and ugly looking solutions. This is great.

@bc3

This comment has been minimized.

Copy link

@bc3 bc3 commented Jan 7, 2020

awesome thx

@FrancoGBenedetti

This comment has been minimized.

Copy link

@FrancoGBenedetti FrancoGBenedetti commented Jan 9, 2020

<3

@Hoomanfr

This comment has been minimized.

Copy link

@Hoomanfr Hoomanfr commented Jan 10, 2020

awesome

@vgoklani

This comment has been minimized.

Copy link

@vgoklani vgoklani commented Jan 11, 2020

Hey there @reinvanoyen - is there a way to get the prompt to show a "*" when there are changes on the current branch, say: "master*"

this way you don't forget to push! thanks!

@DungLai

This comment has been minimized.

Copy link

@DungLai DungLai commented Jan 13, 2020

I do this changes but now I get on terminal instead of path:
\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $
Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html

Here is an example of what I came up with (zsh):

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

This cause my terminal to overwrite line when typing long commands

@efpapado

This comment has been minimized.

Copy link

@efpapado efpapado commented Jan 14, 2020

This cause my terminal to overwrite line when typing long commands

+1

@DungLai

This comment has been minimized.

Copy link

@DungLai DungLai commented Jan 20, 2020

I do this changes but now I get on terminal instead of path:
\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $
Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
Here is an example of what I came up with (zsh):

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

This cause my terminal to overwrite line when typing long commands

This might help: try replacing the single-quotes with double-qoutes at this line:

export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Can someone please check it, what I did was switching from zsh to bash

@benmak11

This comment has been minimized.

Copy link

@benmak11 benmak11 commented Jan 24, 2020

Dope! Thank you!

@agustinpfs

This comment has been minimized.

Copy link

@agustinpfs agustinpfs commented Jan 31, 2020

I do this changes but now I get on terminal instead of path:
\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $
Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html

Here is an example of what I came up with (zsh):

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

What is the name of the file? Thank you

@reinvanoyen

This comment has been minimized.

Copy link

@reinvanoyen reinvanoyen commented Jan 31, 2020

I do this changes but now I get on terminal instead of path:
\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $
Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
Here is an example of what I came up with (zsh):

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

What is the name of the file? Thank you

zsh uses .zshrc instead of .bashrc.

@YWHo

This comment has been minimized.

Copy link

@YWHo YWHo commented Feb 2, 2020

I do this changes but now I get on terminal instead of path:
\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $
Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
Here is an example of what I came up with (zsh):

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

This cause my terminal to overwrite line when typing long commands

I'm seeing the same issue but couldn't find a way to resolve the overwrite line issue. I removed the terminal colour like below to prevent the issue:

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

setopt PROMPT_SUBST
export PROMPT='%n %~ $(parse_git_branch) $ '
@brianchou121

This comment has been minimized.

Copy link

@brianchou121 brianchou121 commented Feb 14, 2020

I do this changes but now I get on terminal instead of path:
\u@\h \W\[\033[32m\]$(parse_git_branch)\[\033[00m\] $
Im on Catalina

Catalina switched to zsh instead of bash. For more information on what to use check out this resource: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
Here is an example of what I came up with (zsh):

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

This cause my terminal to overwrite line when typing long commands

I'm seeing the same issue but couldn't find a way to resolve the overwrite line issue. I removed the terminal colour like below to prevent the issue:

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

setopt PROMPT_SUBST
export PROMPT='%n %~ $(parse_git_branch) $ '

I found a fix! Instead of the previous color set up, just use %F{colorname}command%f
so it would look something like this:


function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '
@ckbrewer33

This comment has been minimized.

Copy link

@ckbrewer33 ckbrewer33 commented Mar 11, 2020

Thanks!

@domantasjurkus

This comment has been minimized.

Copy link

@domantasjurkus domantasjurkus commented Mar 23, 2020

Does not work after git checkouting another branch...

@lj3lj3

This comment has been minimized.

Copy link

@lj3lj3 lj3lj3 commented Apr 9, 2020

brianchou121's answer works great, thanks!

@SW3F3

This comment has been minimized.

Copy link

@SW3F3 SW3F3 commented Apr 19, 2020

nice

@gmartinezsan

This comment has been minimized.

Copy link

@gmartinezsan gmartinezsan commented Apr 25, 2020

Thanks a lot! it worked great!

@LuisSevillano

This comment has been minimized.

Copy link

@LuisSevillano LuisSevillano commented May 19, 2020

@brianchou121 Thank you!!! It works like a charm!

@yehee

This comment has been minimized.

Copy link

@yehee yehee commented May 22, 2020

Thanks a lot! Short and sweet!

@chufaca

This comment has been minimized.

Copy link

@chufaca chufaca commented Jun 20, 2020

thanks @brianchou121!!!

@TechStuffBoy

This comment has been minimized.

Copy link

@TechStuffBoy TechStuffBoy commented Jun 25, 2020

Thanks a lot buddy!

@Ba7er

This comment has been minimized.

Copy link

@Ba7er Ba7er commented Jul 14, 2020