Skip to content

Instantly share code, notes, and snippets.

@reinvanoyen
Last active May 3, 2026 18:53
Show Gist options
  • Select an option

  • Save reinvanoyen/05bcfe95ca9cb5041a4eafd29309ff29 to your computer and use it in GitHub Desktop.

Select an option

Save reinvanoyen/05bcfe95ca9cb5041a4eafd29309ff29 to your computer and use it in GitHub Desktop.
Add Git Branch Name to Terminal Prompt (MacOS zsh)

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

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} $ '
@devih-21
Copy link
Copy Markdown

helpful, thanks bro

@vasu2912
Copy link
Copy Markdown

Looks Clean !!!

@csarrvas
Copy link
Copy Markdown

Thanks!! 👍

@pychap
Copy link
Copy Markdown

pychap commented Dec 21, 2023

Thank you!

@z-coder-hub
Copy link
Copy Markdown

Thank you!

@emcfarlane
Copy link
Copy Markdown

Thank you!

@gideonokyere
Copy link
Copy Markdown

Thanks

@TrevorPawlewicz
Copy link
Copy Markdown

This is exactly what I was looking for... thank you!

@beitomartinez
Copy link
Copy Markdown

Simple and exactly what I was looking for... thanks!! 💯

@laptopmutia
Copy link
Copy Markdown

laptopmutia commented Mar 26, 2024

but it adds unneccesary space when I opening the non git folder, could we do something about that?

the syntax to get the branch also could be simpler we could do this now

git branch --show-current

@luisf-av1
Copy link
Copy Markdown

It works great, thanks!

@jftuga
Copy link
Copy Markdown

jftuga commented Jun 14, 2024

I have a two line prompt:

  • line 1: current directory [git branch]
  • line 2: HH:MM:SS $
function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF=$'%f'
COLOR_DATE=$'%F{243}'
COLOR_DIR=$'%F{197}'
COLOR_GIT=$'%F{39}'
setopt PROMPT_SUBST
PROMPT_CWD='${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}'
export NEWLINE=$'\n'
export PROMPT="${NEWLINE}${PROMPT_CWD}${NEWLINE}${COLOR_DATE}%D{%H:%M:%S}${COLOR_DEF} $ "

I don't need USER, so I changed COLOR_USER to COLOR_DATE.

Here is what it looks like, with the directory name in red, the branch in blue, and the date in faded gray:

~/go/src/github.com/jftuga/dtdiff [dev]
17:35:28 $ 

What I like about having the time:

  • If I meant to time something, but forgot to, I have a built-in timer of sorts
  • I can easily scroll back to see how long ago I ran a command

@butachi
Copy link
Copy Markdown

butachi commented Jul 16, 2024

thanks!

@NidorPL
Copy link
Copy Markdown

NidorPL commented Aug 21, 2024

Amazing, thanks !

@Anti-Reverb
Copy link
Copy Markdown

Thank you.

@cowley05
Copy link
Copy Markdown

This is great thanks!

Is there a way to make it change as i move directories/branches?
Scenario 1
I am on branch main the prompt shows my branch as main i switch to branch feature-X the prompt still shows I am on branch main

Scenario 2
Lets say I have 2 repositories repository A and repository B If I am on branch main in repository A and then run cd ../repository B and I am on feature-branch-X in that repository, then the prompt still shows I am on main which is not true. Is there a way to change this?

IDK if this is an issue that just I'm having or if its something I'm doing wrong etc.

@alysedunn
Copy link
Copy Markdown

Looks great. Thanks!

@SaraForesti
Copy link
Copy Markdown

That's great, thank you!
Just one issue: I have to run the source ~/.zshrc (in my case source ~/.zprofile) command each time I open a new terminal tab or window. Is there a way to run this automatically?

@reinvanoyen
Copy link
Copy Markdown
Author

That's great, thank you! Just one issue: I have to run the source ~/.zshrc (in my case source ~/.zprofile) command each time I open a new terminal tab or window. Is there a way to run this automatically?

The ~/. zprofile file is only evaluated when you login to your mac user account. The . zshrc (environment for interactive shells) should be used for parameters like $PROMPT, aliases, functions, options, history variables and bind keymappings you would like to have in both login and interactive shells.

What's preventing you from using the .zshrc file?

@SaraForesti
Copy link
Copy Markdown

That's great, thank you! Just one issue: I have to run the source ~/.zshrc (in my case source ~/.zprofile) command each time I open a new terminal tab or window. Is there a way to run this automatically?

The ~/. zprofile file is only evaluated when you login to your mac user account. The . zshrc (environment for interactive shells) should be used for parameters like $PROMPT, aliases, functions, options, history variables and bind keymappings you would like to have in both login and interactive shells.

What's preventing you from using the .zshrc file?

Thanks for the reply.
When I installed nvm via Homebrew, I got these 3 items created
.zprofile

.zsh_history

.zsh__sessions (folder)
I thought .zshrc would be one of them (It's been a while since last time I've done this for a new machine!), but since it wasn't there, I've done some research and found out that .zshrc and .zprofile should be the same: I guess they are not!

I'll try create a .zshrc manually then.

@gtt116
Copy link
Copy Markdown

gtt116 commented Dec 11, 2024

That is awesome

@gabrieljuniorcreditas
Copy link
Copy Markdown

Nice! Worked for me. Thank you!

@ethaizone
Copy link
Copy Markdown

Thank you. I used your version for sometimes until I think I need some adjustment for git status so I created new gist.

Other can use as well or just copy git_status function to your zshrc file and change your exist prompt.
https://gist.github.com/ethaizone/232664a147258fb6a5b91ba2bf790f37

@directlypro
Copy link
Copy Markdown

This is awesome, thanks.

@susantang-nz
Copy link
Copy Markdown

thank you

@ragebiswas
Copy link
Copy Markdown

Nice, clean and fast. Thanks!

@SleepyBrett
Copy link
Copy Markdown

actual git command that doesn't require extra parsing git rev-parse --abbrev-ref HEAD

@taicutsun
Copy link
Copy Markdown

Thx

@rezabaktash
Copy link
Copy Markdown

Thanks. Though there is a problem for me. When I switch to another git directory, the branch remains the old one.

@briemarie
Copy link
Copy Markdown

Perfect! Thank you!

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