Git in your prompt
Posted by Corey on December 19, 2007 at 10:59 AM
I like to know what git branch I have checked out. A constant, in-my-face acknowledgment that I am not committing changes to some obscure branch I checked out the night before. It’s easy enough to merge these changes into the correct branch, but I need more.
The solution? Put it in your prompt.
When I’m in a git repo with the master branch checked-out I want my prompt to look like this my_git_project(master) %
otherwise leave out the git stuff ~ %
setopt prompt_subst
parse_git_branch() {
git branch —no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PROMPT='%c$(parse_git_branch) %% '
How this works
setopt prompt_subst: Tells zsh that I want it to evaluate my prompt string every time it is printed.
parse_git_branch: Lists all the branches in the current git repo. Sed removes everything but the checked-out branch from the git output. If the current directory isn’t in a git repo, then it just redirects the errors to /dev/null
PROMPT=’c$(parse_git_branch) %% ’: Prints out the current directory name and the git branch if it exists. Remember to you ’ instead of ” because otherwise zsh won’t evaluate the parse_git_branch function.
function parse_git_branch {
git branch —no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'
}
PS1="\w\$(parse_git_branch) $ "
update
Geoffrey Grosenbach (aka TopFunky) created a version for tcsh as well. The gist of it is…
setenv GIT_BRANCH_CMD "sh -c 'git branch —no-color 2> /dev/null' | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'"
set prompt="[$HOST %~ `$GIT_BRANCH_CMD`]"
But you can get a more detailed example here.
Comments
There are 0 comments on this post. Post yours →
Post a comment
Required fields in bold.