Skip to content

Instantly share code, notes, and snippets.

@aesopwolf
Last active July 20, 2022 22:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aesopwolf/2f899068f1fb85976265ad21ffb59858 to your computer and use it in GitHub Desktop.
Save aesopwolf/2f899068f1fb85976265ad21ffb59858 to your computer and use it in GitHub Desktop.
Add git email to command prompt

Add git email to command prompt

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.

The problem

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.

The solution

image

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:

  1. git_current_user_name()
  2. 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.

Instructions for using

  1. Update oh-my-zsh via upgrade_oh_my_zsh
  2. Use $(git_current_user_email) in your theme file
  3. 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.

  1. Edit ~/.gitconfig using the example below
  2. Run git work or git home, etc. from the command line
  3. 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.

In summary

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.

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