TLDR: Do you use different names/emails for git version control (work vs personal)? Then add your user info to the command prompt as a visual reminder of who you're committing as.
A few months ago I ran into a dirty situation, I committed and pushed a lot of code using the wrong git config user.name
and git config user.email
(I was using personal info instead of work info).
Fixing it was possible, but I had to rewrite git history (ouch!)
And then it happened again last week. And almost again an hour ago.
I decided I wanted a visual indicator in my command prompt of which email I was using to commit to git with. In essence, to display my git config user.email
in my command prompt. I use "oh-my-zsh" but it doesn’t have support for this by default so I had to write two small oh-my-zsh functions:
git_current_user_name()
git_current_user_email()
After that, I was able to edit my theme ~/.oh-my-zsh/themes/robbyrussel.zsh-theme
and use $(git_current_user_email)
anywhere I wanted.
Here’s an example:
# ~/.oh-my-zsh/themes/robbyrussel.zsh-theme
local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT='${ret_status} %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}$(git_current_user_email):(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"
Eventually I got my custom oh-my-zsh functions merged into the public repo and now anyone can use them.
- Update oh-my-zsh via
upgrade_oh_my_zsh
- Use
$(git_current_user_email)
in your theme file - Reload your shell (open a new tab or
source ~/.zshrc
)
Technically, you can stop there. But I also setup an easy way to switch between git 'user accounts' too.
- Edit
~/.gitconfig
using the example below - Run
git work
orgit home
, etc. from the command line - Your shell wont update to display your new git user account unless you reload your shell (open a new tab or
source ~/.zshrc
)
# ~/.gitconfig
[alias]
work = !git config --local user.email \"work@example.com\" && git config --local user.name \"Work Name\"
home = !git config --local user.email \"home@example.com\" && git config --local user.name \"Home Name\"
Again, you can stop here too, but there's one last thing I do with my workflow. I remove my global git user.name and user.email just in case, and also enforce git to make you set a local user config
Run:
$ git config --global --unset user.name
$ git config --global --unset user.emai
$ git config --global user.useConfigOnly true
This way, the next time you commit something and you forgot to run git work
or git home
you’ll get a warning message and be force to set your user config locally in the repo.
So all you have to do is run git work
& source ~/.zshrc
, and then re-run your commit.
My git configuration is a little weird and opinionated, but it works for me because I tend to switch between several different name/email combos and often times I forgot to make the switch until I’ve already pushed code to the server.