Skip to content

Instantly share code, notes, and snippets.

@danielalvarenga
Last active March 27, 2024 17:34
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\$ "
@Guilheeeerme
Copy link

Valeu pai

@BryanFonseca
Copy link

gracias lokiiis

@mohammad-hassani
Copy link

oh it worked. thanks a lot

@vishalsanwar
Copy link

Excellent! That's work.
Need to restart Terminal

@narbigcito
Copy link

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOoo Amazing

@abdobouna
Copy link

abdobouna commented Sep 3, 2021

Thanks for this snippet, but can you please tell me how to add a space before (branch)?

Edit: NVM I figured out how to do it :)

# Add in ~/.bashrc or ~/.bash_profile
function parse_git_branch () {
  # The line below is changed.
  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\$ "

@JoseCigarro
Copy link

Excellent! Many thanks!

@ferrazmichel
Copy link

Excellent man!! Tranks a lot!

@TatevAvoyan
Copy link

ooooo Amazing, Thanks a lot!

@timeuz
Copy link

timeuz commented Jan 23, 2022

thanks man! ⭐

@jackyli-work
Copy link

thanks a lot!

@islahh
Copy link

islahh commented May 24, 2022

Awesomeee

@VirajInsharp
Copy link

Thanks a lot!. Worked on Pop os 22.04

@kaareloun
Copy link

🙏

@alete89
Copy link

alete89 commented Oct 5, 2022

Please don't ever remove this. I have it starred and I use it everytime I use a new system 😅

@ozehken
Copy link

ozehken commented Oct 8, 2022

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

@nezahualcoyotl
Copy link

nice!

@vitaliisili
Copy link

nice

@Scyzemic
Copy link

Scyzemic commented Jun 5, 2023

💯

@lia2790
Copy link

lia2790 commented Jun 26, 2023

thanks! works with ubuntu 22.04

@gilbertouk
Copy link

thanks, works with Ubuntu WSL

@alexxfreitag
Copy link

Thanks!

@MaKTaiL
Copy link

MaKTaiL commented Sep 20, 2023

This is not working for me.

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

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