If you’re like most people, you probably have multiple Git accounts set up on your computer. You can use one account to work on projects and another to store your source code. But what if you want to work on two projects at the same time and want to keep track of which project is working on which branch? There’s a way to do this without having to switch between accounts. You can use Git’s branching model, which lets you create different branches for different projects. This way, you can keep track of which project is working on which branch and make changes to the project in one branch while still working on the other project in a separate branch. To use Git’s branching model, first create a new Git account:

  1. open a new terminal window and type git init 2) enter your name (e.g., “joe”) 3) click OK 4) enter your email address (e.g., “joe@example.com”) 5) click OK again 6) type git config –global Branch .gitignore 7) click OK 8) type git config –global push origin master 9) type git push 10) click OK 11) close the terminal window 12) open a new file called .gitignore in your home directory 13) add the following lines 14): #!gitignore #!github-com/github/git-branch-model 15]# cd .. 16]# git add . 17]# git commit -am ‘added branch’ 18]# git push 19]# cd .. 20]# ls -l total 8 -rw-r–r– 1 joe jose 4096 Mar 1 17:12 .gitignore -rw-r–r– 1 joe jose 4096 Apr 2 09:10 Branch

If you’ve tried to use Git with multiple different providers, or with multiple different accounts, you may run into issues with authenticated them both at the same time. Here’s how to manage two accounts without running into conflicts.

How Git Handles Accounts

The first thing to understand is that Git is entirely local—your local Git repository isn’t controlled by the server you use to store code on. It only connects to the remote repository when pushing or pulling updates. When it does this, it uses your Git account details.

The only details it uses when doing so are your email and username. The password for the account is asked every time, or stored in the keychain, or irrelevant if you’re using SSH keys (which you should). If your username is different than the account you have credentials for, you won’t be able to push or pull until you fix it. In some cases, if you use the same email for different accounts, the push may go through but show up with a different username in the commits, which may not be ideal.

These details are defined in git‘s config. You’ve probably had to run the following commands to change your user account name and email:

This will set the global config to the new username and email. However, another issue is the result. Because it sets it globally, if you want to checkout a project on a different account, you’ll break the settings for your other repositories.

The solution is to set a custom config for each repository that uses a different account. Set your global username and email to your most often used account, as it will be the default for all new repositories and repositories without any configuration.

However, for each repository that uses a different account, you’ll want to leave out the –global flag:

This overrides the default config, so even if you change your default account, the local account will still be used for this repository.

Managing Credentials for Multiple Accounts

You’ll want to use SSH keys anyway for Git, so credential management should be a non-issue. It’s perfectly acceptable to use the same SSH key everywhere—your SSH key is tied to your machine, and in turn, your identity. It’s different than a password; there’s no way to crack it without hacking into your personal computer, which is much less likely to happen than a data breach.

However, if you need to use different SSH keys for some reason, it’s possible to do so. First, you’ll want to ensure the secondary SSH key is loaded into your keychain, usually by editing ~/.bashrc to call ssh-add when you load the terminal:

Otherwise, you’ll have to use the -i flag and specify the key path each time.

To make Git use different keys for different accounts, you’ll want to edit ~/.ssh/config and add a Host block for each account:

This will use different keyfiles based on the hostname following git@ in the Git URL. Github appends your username to the Git URL, so it’s easy to differentiate between users.