(Git v2.13+) Easily Use Work & Personal Git Accounts On The Same ComputersteemCreated with Sketch.

in #programming7 years ago (edited)

octobiwan.jpg

Using 2+ git accounts on the same computer

Do you use a personal git account on your home computer, but also want to occasionally use it to make some commits to your work git repos?

The easiest solution I've found goes like this:

Make sure you're using git version 2.13 or newer.

Let's say your current .gitconfig file is located at ~/.gitconfig and it looks like this:

[user]
  email = catwarrior01@aohell.com
  name = "Teh OSS Kitteh W4rrior"
[alias]
  co = checkout

That's cool. But you've just been hired by google and now you'd like to use your real name and google email when authoring commits when you're working from home and sipping on a pina colada.

Git v2.13 gives us the option of using conditional configuration includes. This lets us apply settings from a specific .gitconfig to a directory (and all its subdirectories).

Here's how it works:

Let's update our ~/.gitconfig to look like this:

[includeIf "gitdir:~/source/personal/"]
  path = ~/.gitconfig-personal
[includeIf "gitdir:~/source/work/"]
  path = ~/.gitconfig-work

This tells git to pay attention to the directory we're currently in when we issue git commands.

If the directory we're currently in starts with ~/source/personal, it will use the config file ~/.gitconfig-personal for all its configuration.

Likewise, if the current directory we're in starts with ~/source/work, it will use the config file ~/.gitconfig-work for all its configuration.

Now let's create this ~/.gitconfig-personal file. It should look like this:

[user]
  email = catwarrior01@aohell.com
  name = "Teh OSS Kitteh W4rrior"
[alias]
  co = checkout

And let's create the ~/.gitconfig-work file. It should look like this:

[user]
  email = leeroyjenkins@google.com
  name = "Leeroy  A. Jenkins"
[alias]
  co = checkout

Tada! You're done.

Now your commits will be tagged with the appropriate name and email address depending on whether the repo you're working in is inside of the personal or work directories on your home computer.

For pulling down your work repos, I'd suggest just using https instead of ssh as it will be easier than managing multiple ssh keys.

Hope this helped!

If you enjoyed this article, please upvote and resteem! Thanks!

Sort:  

More information can be found here: https://github.com/blog/2360-git-2-13-has-been-released under the "conditional configuration" sub-section. Enjoy!

Congratulations @golgaroth! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @golgaroth! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!