Update gitconfig to Work with Multiple Accounts and SSH Keys

Ryan Harris
InstructorRyan Harris
Share this video with your friends

Social Share Links

Send Tweet

When working with multiple GitHub accounts on the same system, you want to ensure that your commits have the proper author information.

To do this, we'll create a global git configuration on our system that uses our personal account's information. Then, in our work-specific repositories, we'll add a local git configuration to override our global values.

We will be using the following commands in this lesson:

  • code
  • git config

Ryan Harris: [0:00] Here we have two projects associated with two different github accounts. The personal-repo lives in our personal github account whereas the work-repo lives in our work github account.

[0:09] While we've configured our system to properly connect to these two accounts using ssh and their respective keys, we have yet to update the git info for each of our commits. To ensure commits are associated with a proper account, we'll want to update our gitconfig. First, let's check to see if we have a global configuration set up on our system by running git config --global -l.

[0:34] As you can see here, we're unable to read the file, meaning we don't have one. We'll need to create our own. We're going to use our personal information for our global configuration since we work on projects in that account more.

[0:45] Let's add our user name and email. First, we'll run git config --global user.name. Here I'll use my name, Ryan Harris. Then I'll do the same thing, git config --global user.email. Here I'll define my email address. Now if we run the git config --global -l command again, you can see that our values are properly defined.

[1:14] While these values will take effect for all get repos on our system, we actually don't want to use that information for our work-repo, since it's part of a different account. To set our git configuration values here for this repo alone, we can run git config without the --global and user.name, Ryan Harris. We'll also do git config user.email, harris.ryan1@Gmail.com and hit Enter.

[1:44] Now if we run git config -l, you can see our global values are defined at the top of this file. However, down below, they are overridden with the values we just entered for this repository specifically. Let's see this in action.

[1:59] If we do git status, we can see that our vscode directory has changed. That's because I've defined these peacock colors you see down here. We actually don't want to commit that, so we're going to open our gitignore file, code gitignore.

[2:14] At the bottom, we'll just add .vscode. We'll save that. We'll add git add .gitignore. Then we'll git commit -m, update gitignore. Then we'll git push origin master.

[2:34] You can see the same thing in our work-repo by running git status. Again, our vscode subdirectory has some changes to it. What we're going to do is we're actually going to ignore those by opening the git ignore file, code .gitignore. And again, we'll do the same thing we did in our personal-repo. We'll add .vscode, save that.

[2:54] Now we can git add .gitignore and we'll add a commit as well, git commit -m update work repo .gitignore, hit Enter, and then we'll run git push origin master.

[3:15] If we check out the personal-repo on GitHub and look at the latest commits, you can see the latest commit update .gitignore is associated with my personal account, which is what we expect.

[3:26] To confirm our git config is working properly for both accounts, let's look at the work-repo in my work account on GitHub. Check out the two commits that we have. You can see the latest commit update work repo .gitignore is associated with the proper account, ryan-egghead.

[3:42] To review, by using a global and local git configuration file for your different repositories, you can ensure that the git commit information will be accurate when you push work up to GitHub.