How I handle multiple GitHub accounts
I recently took the plunge and created my very own personal GitHub account—check it out at https://github.com/mtdevs28080617! It's separate from my work account because, hey, who doesn’t love a little organization, right? Keeping my work and personal code apart is just another step toward achieving that elusive work-life balance... if only it were that easy everywhere else! 😅
I started looking into how to set up a single machine that can automatically figure out which GitHub account and SSH key to use—sounds like magic, right? There are some great examples out there that follow a similar approach, making the whole process much simpler than I thought!
Step 1 - Update you ~/.ssh/config
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/my-work-key
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/my-personal-key
Step 2 - Download the repository
git clone git@github.com-{your-username}:{owner-user-name}/{the-repo-name}.git
e.g. git clone git@github.com-personal:personal/my-new-personal-repo.git
e.g. git clone git@github.com-work:work/my-new-work-repo.git
Step 3 - Adding our config
Going forward, to make sure that commits and pushes from each repository on the system use the correct GitHub user, we’ll need to configure `user.email` and `user.name` for every repository, whether it's freshly cloned or already existing. You can do this using the following commands:
git config user.email "my-personal-email@email.com"
git config user.name "Matthew Thomas"
Step 4 - Push/Pull
Next, we’ll need to run the following commands to add the remote origin for each repository we set up:
e.g. git remote add origin git@github.com-personal:personal
Now, with everything set up, we’re all set to run `git push` and `git pull` without any issues!
Well, wasn't that a breeze! But hold on—you’re probably thinking, “Is this really the best way? Doing this every time sounds like a recipe for human error.” And guess what? You’re absolutely right. We’ve all heard the term DRY (Don’t Repeat Yourself) thrown around in software engineering—maybe a little too much, right? But hey, why not actually put it to use here?
The way I handle it!
I started thinking, why not borrow an idea from Linux and its folder structure? Just like each user has their own home directory where everything can live neatly, couldn’t Git have a similar setup? Well, it turns out it actually can, and it's pretty neat!
Step 1 - Decide on your folder structure!
I like to think of each GitHub account as its own unique identity within my file system—it’s like giving each account its own little home!
~/workspace
~/personal
As you can probably guess, each of these folders represents a GitHub account or action—'workspace' is for all work-related stuff, and 'personal' is just for my own projects. It’s a simple way to keep everything organized!
Step 2 - Updating my config
Next, I just update my GitHub config—specifically, the global config—and that’s all there is to it!
~/.gitconfig
[user]
name = "Matthew Thomas"
email = "myemail@email.com" #my work default email
[core]
autocrlf = input
sshCommand = "ssh -i ~/.ssh/id_rsa" #my default work key
[url "git@github.com:"]
insteadOf = https://github.com/
[color]
ui = auto
[filter "lfs"]
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
required = true
clean = git-lfs clean -- %f
[includeIf "gitdir:~/personal/"] #my personal folder
path = ~/.mygitconfig
~/.mygitconfig
[user]
email = "mypersonalemail@email.co.uk"
[core]
sshCommand = "ssh -i ~/.ssh/personal.pem"
That’s it—super easy, right? By default, I’m using my work credentials, but if I’m in my personal directory, I switch to my personal account automatically. No more repeating myself or having a hacky bash script lurking in the shadows! You can see how this could make life a whole lot smoother. I hope it helps you on your coding adventure! 🚀