Multiple Git Accounts on One Machine with Git Config Files

 

This blog entry shows how to use Git Config files to support multiple Git accounts on the same machine. It focuses on what are Git Config files and how one can store multiple Git configurations in the Git Config files.

What are Git Config files?

A Git config file is a configuration file used by Git, a distributed version control system, to store settings that control the behavior of Git. These settings can be related to a specific repository, a user’s repositories, or the system as a whole.

There are three main levels of Git configuration files:

  1. Local configuration:
    • in the .git/config within a specific git repo
    • affect only the specific repo
    • typical settings:
      • remote URL,
      • branch info, and
      • repo-specific preferences
  2. Global configuration:
    • in the user’s home directory (~/.gitconfig or ~/.config/git/config)
    • affect all repositories in this user’s account
    • common settings include:
      • user identity and
      • default editor
  3. System configuration:
    • in the /etc/gitconfig
    • affects all repositories on this system (machine)
    • common settings:
      • system-wide hooks
      • other system-level attributse

Steps to Setup Access of Multiple Git Accounts

  1. Create a separate Git configuration file for each Git account
  2. Git Clone using –config option

Step 1 Create a separate Git configuration file for each Git account.

You can do this by running the following command in your terminal, replacing username with your actual username and email with your actual email:

# Personal account
$ git config --global --file ~/.gitconfig_personal user.name "Personal Name"
$ git config --global --file ~/.gitconfig_personal user.email "personal_email@example.com"

# Work account
$ git config --global --file ~/.gitconfig_work user.name "Work Name"
$ git config --global --file ~/.gitconfig_work user.email "work_email@example.com"

Step 2: Git Clone using –config option

Clone your Git repository using the --config option to specify the appropriate configuration file. For example:

# Personal account
$ git clone --config ~/.gitconfig_personal https://github.com/<personal_username>/repo.git

# Work account
$ git clone --config ~/.gitconfig_work https://github.com/<work_username>/repo.git