How to Set Up & Manage Multiple Git Profiles Like a Pro

Table of Contents

Introduction

If you work with Git repositories from multiple sources, organizations, etc, you cannot use the same Git identity (username & email) for commits across all your repos. This article explores ways to address this issue. Some common scenarios include:

  • You have separate personal & work Git accounts
  • You frequently work on & switch between Git repos from multiple sources like GitHub, GitLab, etc, each with a different account & email address
  • You work with Git repos from different organizations with their own dedicated accounts & email addresses

Before we explore solutions, let’s learn how Git config works.

Git Config Hierarchy

Git reads configuration from 3 locations:

  • System-wide: /etc/gitconfig (applies to all users on the system)
  • User-specific: ~/.gitconfig (applies to all repositories for a single user)
  • Repository-specific: .git/config (applies to the specific repository)

Repo config overrides user config & user config overrides system config.

Global Git Config

In a single-profile scenario, you can just set your Git identity globally:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

This updates ~/.gitconfig.

Local Git Config

If you typically work with a handful of Git repos, just set your identity in them once & it will persist unless you delete & reclone the repo:

cd my-git-repo
git config user.name "Your Name"
git config user.email "your.email@example.com"

This updates .git/config of that repo.

Dynamic Git Config

If however, you work with several Git repos from multiple sources & if the repos are mostly short-lived on your system, you need an automatic way of choosing your Git identity when you make a commit in any of the repos.

Consider this setup: You have separate parent directories for Git repos for each of your Git accounts: ~/GitHub & ~/GitLab or ~/Personal & ~/Work or ~/CompanyA & ~/CompanyB. When you clone a repo, you place it one of these directories like ~/CompanyA/RepoA & ~/CompanyB/RepoB. All repos in ~/CompanyA use the same Git account/identity & all repos in ~/CompanyB use another Git account/identity.

If would be nice if we could create ~/CompanyA/.gitconfig & ~/CompanyB/.gitconfig, each with different Git usernames & emails, but Git doesn’t support this. However, if we combine this setup with some clever “conditional include” logic in the global Git config, it works. The config files would look something like this:

[user]
  name = Your Name
  email = your.email@companyA.com
[user]
  name = Your Name
  email = your.email@companyB.com
[includeIf "gitdir:~/CompanyA/"]
  path = ~/CompanyA/.gitconfig

[includeIf "gitdir:~/CompanyB/"]
  path = ~/CompanyB/.gitconfig

Now, whenever Git looks up the config for a repo, this conditional include logic will run & pick the right profile based on which directory you’re currently in.

Test Dynamic Config

To test the setup, create or clone a repo & run git config -l in it:

$ cd ~/CompanyA/RepoA
$ git config -l
user.name = Your Name
user.email = your.email@companyA.com
$ cd ~/CompanyB/RepoB
$ git config -l
user.name = Your Name
user.email = your.email@companyB.com

As expected, this would also reflect in the target Git repo when you commit & push.

Conclusion

This article explored ways to manage multiple Git profiles on a system. This “dynamic” way of selecting Git profiles means you can avoid running git config user.name & git config user.email in every repo you clone & every time you clone it.

By the way, similar functionality is available as VS Code extensions if VS Code is the only IDE you use for Git. See VS Code Marketplace.

About the Author ✍🏻

Harish KM is a Principal DevOps Engineer at QloudX & a top-ranked AWS Ambassador since 2020. 👨🏻‍💻

With over a decade of industry experience as everything from a full-stack engineer to a cloud architect, Harish has built many world-class solutions for clients around the world! 👷🏻‍♂️

With over 20 certifications in cloud (AWS, Azure, GCP), containers (Kubernetes, Docker) & DevOps (Terraform, Ansible, Jenkins), Harish is an expert in a multitude of technologies. 📚

These days, his focus is on the fascinating world of DevOps & how it can transform the way we do things! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *