DEV Community

Cover image for Display Current Git Branch on iTerm2
Ahmet
Ahmet

Posted on

Display Current Git Branch on iTerm2

As a Windows user, I generally use Conemu Console for my console and it has a sweet future in which you can see the current git branch on the terminal. After using Mac I wanted to see the same future on iTerm2. I found the way and I want to share it with you 🙂

.zshrc file

Firstly create and edit a .zshrc file that will use for terminal configuration.



touch ~/.zshrc; open ~/.zshrc


Enter fullscreen mode Exit fullscreen mode

parse_git_branch

Customize your terminal whatever you want with this file. You can use the following bash code to display the git branch.



parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF='%f'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{39}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '


Enter fullscreen mode Exit fullscreen mode

And that is it! Restart your terminal. You will see the current branch on your terminal 🙂

Image description

More customization

If you want more customization you can edit the PROMPT variable in the .zshrc file. For example, you can add your current computer user.



parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF='%f'
COLOR_USR='%F{243}'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{39}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n@%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '


Enter fullscreen mode Exit fullscreen mode

Image description

More color!

If you want to change color, just edit the color code.
Change COLOR_USR='%F{243}' to COLOR_USR='%F{229}'

You can find the color code schema in the following image.

Image description

Image Source

Top comments (4)

Collapse
 
michelamin47 profile image
Michel Lalmohamed

Worked like the charm! Thanx for the colour overview!

Collapse
 
rodimendes profile image
Rodrigo Mendes

Perfect!!
That kind of things are super handy!
Thank you!!

Collapse
 
bughunter profile image
burak duman

Awesome man, thanks!!

Collapse
 
catherinepope profile image
Catherine Pope

Thanks so much for your helpful post, Ahmet! That was exactly what I needed.