How To Easily Manage Multiple Git Profiles

Randall Reed, Jr.
3 min readJul 22, 2023
A pile of lego heads
Keeping work life separate from personal life can sometimes be overwhelming — Photo by Jan Huber on Unsplash

Managing multiple git profiles can be challenging, but it doesn’t have to be.

Too Many git Profiles

Once upon a time, during a consulting engagement, I found myself juggling three different git profiles:

  1. My personal account (r@g.com)
  2. My work account (r@c.com)
  3. My client account (r@x.com)

I would occasionally commit code under each of the three accounts — client work under the “client” account, internal consultancy work under the “work” account, and open source work under my “personal” account. Often, I would realize too late that I had written a commit (or several) under the wrong account, and I would have to reset my branch and rewrite those commits with the correct account.

An elegant solution

Out of this frustration, I came up with two useful strategies:

  1. I started displaying the current git username in my terminal prompt and created a simple command to display the user context
# .zshrc

# Find and set git username var
function git_user_name()
{
user=$(git config user.username 2> /dev/null)
if [[ $user == "" ]];
then
:
else
echo ''$user''
fi
}
# Include git username in prompt
prompt='working as %F{green}$(git_user_name)'$'\n''☆%f '

# Use `me` to show local user, git user, and current active ssh key
alias me='echo "local: $(whoami)"; echo "git: $(git config user.email)"; echo "ssh: $(ssh-add -l)"'

2. I created git swap, to easily manage git profiles

$ me
git: r@g.com

$ git swap work
$ me
git: r@c.com

$ git swap client
$ me
git: r@x.com

git swap

So what is “git swap” you’re asking? It is a command line utility that can be used to easily swap between git profiles! Built with Ruby and distributed as a Homebrew formula, it generates a custom git command, swap, used to easily update your git name, username, email, and active ssh key.

During setup, configuration is stored in ~/.gitswap, in YAML format. Note that you can use whatever naming convention you like for your profiles (personal/work, opensource/professional, etc). Mine looks something like this:

personal:
username: randallreedjr
email: r@g.com
name: Randall Reed, Jr.
ssh: ~/.ssh/id_rsa
work:
username: c-randall
email: r@c.com
name: Randy Reed
ssh: ~/.ssh/work_rsa
client:
username: x-randall
email: r@x.com
name: Randall Reed
ssh: ~/.ssh/client_rsa

Additionally, if you protect your SSH keys with a passphrase (as you should!), git swap will automatically include --apple-use-keychain to apply your saved passphrase. That way, you don’t have to manually type it in every time you switch contexts!

If this is a challenge you face, I encourage you to give git swap a try! I am the author and sole maintainer, and gladly welcome any feedback you may have.

For more information about creating your own custom git commands, check out this excellent tutorial which includes several useful examples!

P.S. — Why not call it git switch?

Great question! git swap was originally created as git switch, but then git released their own switch command and broke my custom one. How dare they!

--

--

Randall Reed, Jr.

Learning Ruby and building stuff. Developer at @degreed, based in Richmond, VA.