Skip to content

Instantly share code, notes, and snippets.

@nilesh-akhade
Created October 29, 2023 08:34
Show Gist options
  • Save nilesh-akhade/6cee427e1ba0b72334d9d3c35491c3e0 to your computer and use it in GitHub Desktop.
Save nilesh-akhade/6cee427e1ba0b72334d9d3c35491c3e0 to your computer and use it in GitHub Desktop.
How to work with multiple GitHub configuration

How to Work with Multiple GitHub Configurations

GitHub is a popular platform for hosting and collaborating on software projects, and it's common for developers to have multiple GitHub accounts, such as personal and work accounts. Managing multiple GitHub accounts on the same machine can be challenging, but it's entirely possible with the right configuration. In this article, we'll explore how to work with multiple GitHub configurations on your local machine.

Prerequisites

Before we dive into the setup, make sure you have the following:

  1. GitHub Accounts: Ensure you have the GitHub accounts you want to use (e.g., personal and work accounts).

  2. SSH Keys: Generate SSH keys for each GitHub account you plan to use. You can follow GitHub's guide on generating SSH keys for detailed instructions.

  3. Git: You should have Git installed on your local machine. If it's not installed, you can download it from the official Git website (https://git-scm.com/).

Configuring Multiple GitHub Accounts

By default git commands uses git config stored in the file ~/.gitconfig, global config. To work with multiple GitHub configurations, you need to configure Git to recognize each account and associate the correct SSH key and username with each. Here's how to do it:

1. Configure SSH Config File

On your local machine, you can use the ~/.ssh/config file to create SSH host aliases for each GitHub account. Here's an example of how you can set up SSH aliases for two GitHub accounts, "personal" and "work":

# Personal GitHub account
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/personal_key

# Work GitHub account
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/work_key

Make sure to replace personal_key and work_key with the actual file paths to your SSH keys for each GitHub account.

2. Git Global Configuration

Set your global Git configuration for your username and email, which will be the default values used for repositories that don't have specific configurations.

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

3. Specific Repository Configuration

For each repository, you can set repository-specific configurations. Navigate to the root directory of the repository, and use the following commands:

  • Set the SSH alias for the repository (e.g., for a repository associated with your "personal" GitHub account):

    git remote set-url origin git@github.com-personal:personal_username/repository_name.git
  • Set the SSH alias for a repository associated with your "work" GitHub account:

    git remote set-url origin git@github.com-work:work_username/repository_name.git

Remember to replace personal_username and work_username with your actual GitHub usernames and repository_name with the repository name.

Working with Multiple GitHub Accounts

With these configurations in place, you can now work with multiple GitHub accounts on the same machine. Here are some common scenarios:

Cloning a Repository

When you clone a repository, Git will use the remote URL configured for that repository, which should include the appropriate SSH alias. For example:

git clone git@github.com-personal:personal_username/repository_name.git

Committing

Should not forget setting local github config, before making a commit.

git config --local user.email personal-email@gmail.com

Review git log to check for the commit owners.

Pushing and Pulling

When you push or pull changes, Git will use the remote URL from the repository's configuration. This means that the correct GitHub account will be used for each repository, eliminating conflicts.

Setting Up a New Repository

When creating a new repository, you can configure it to use the desired GitHub account by setting the remote URL as you did when cloning a repository.

Conclusion

Managing multiple GitHub accounts on a single machine may seem complex, but with proper configuration, it's straightforward. By setting up SSH host aliases, global Git configurations, and repository-specific configurations, you can work seamlessly with your personal and work GitHub accounts. This flexibility allows you to switch between accounts and collaborate on different projects without any conflicts.

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