Switch a Git User with One Command

Git Logo

After initializing a git repository for a new project, the next step would be to set a git user name and email. You can do this by typing the git config command each time. But when we have multiple git profiles, it would be nice if we could manage them in a JSON format.

Therefore, I decided to write a simple script to set up and/or switch a git user config with one command.

Git Config

Git config has two scopes; “global” and “project (repository).”

Global Config

A global config file exists on a user home directory as .gitconfig, and your default user name and email are defined here.

Project (Repository) Config

When you are creating a repository with the git init command, it generates .git/config. This is the file that contains configuration data such as remote, branch, and user information.

Git User Setting

The git config command is the one you can use to set a user name and email. For your git global configuration, passing the --global option would do.

Global git configuration is the one you would use as a fallback, but this is the only config you need if you have only one git profile. If that is not the case, you would need to make sure to configure a user profile for each repo so that your commits will be under the right profile, even if it’s the same as your current global one.

Global Config Command

$ git config --global user.name <USER NAME>
$ git config --global user.email <USER EMAIL>

Project (Repository) Config Command

$ git config user.name <USER NAME>
$ git config user.email <USER EMAIL>

Set and/or Switch a Git User for a Repository

Let’s get to the actual implementation of setting and/or switching a git user profile.

Strategy

  • Create a JSON file that holds git user profile data
  • Write a script that pulls out a profile data specified with arguments, and run git config
  • Make a convenient alias that runs the script

Dependencies/Prerequisites

To use JSON on the ShellScript, we need a library called jq. This one is also quite useful for parsing response JSON string on curl, etc.

For Mac, installation could be done with Homebrew:

$ brew install jq

git_users.json

Now, let’s create a JSON file for the user profile data in a use case where we switch the profile on a work/personal basis:

set_git_user.sh

Here’s the script that loads the JSON data and passes it into git config:

CONFIG_PATH represents the path to the JSON file. So, you can change that based on where you locate it. Usually, I would use set -e to make it exit right after any error is thrown. However, I chose to use jq’s exit code option this time to see if the JSON parsing was successful, or if it returned null. This option is enabled by passing -e to jq.

Execution

As we have everything we need to make it work now, let’s try setting the work profile. The console should print the contents of .git/config on success:

$ sh PATH_TO/set_git_user.sh work

Add an Alias

When we place the script somewhere like $HOME/scripts, it would be a bit tedious to type the absolute path all the time. So let’s just make an alias for it:

$ alias guser="sh PATH_TO/set_git_user.sh"

This allows us to set and/or switch the git user config by just running guser work from anywhere. It’s a little huge difference.

TL;DR

This whole thing started with the idea of storing multiple git user profiles on a file since it would be used often. It seems not a few people want to switch their git user profiles easily. I hope this article helps.

For setting different ssh keys based on a git account, the article below describes the tip:

Switch SSH Keys by Host

References

COPYRIGHT © 2023 Kohei Ando