controlling source control sources

controlling source control sources

·

2 min read

Wanna know how to setup your work💻 and personal🤓 github accounts on the same machine like a pro?? 🔥🚀

First we are going to configure ssh such that it uses different keys for different domains. Open your ssh config file located at ~/.ssh/config and add these lines -

# work account
Host myworkplace
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_myworkplace

# personal account
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_personal

now its as simple as simply changing the remotes of your work repos to point togit@myworkplace instead of git@github.com like this -

git@github.com:git-account-name/repo-name.git
# to ->
git@myworkplace:git-account-name/repo-name.git

That's it! You can now keep using everything else like before and ssh will automatically switch the Host with HostName and use that relevant key assuming you've already generated and uploaded the public keys. You can check the documentation of your respective hosting provider for further details on that.

Now as you might have noticed, while pushing the changes and generally working with different remotes seems to work fine.. all the commits have your personal credentials 😱. This is because you've set that up in your global git config the first time you tried to commit! This may or may not be a problem but I'll tell you how to fix it nonetheless.. and its easy! see git allows you to nest different git configs throughout your file system and we can simply provide different ones for different repos.

Go to your global git config located at ~/.gitconfig and add these lines -

# work specific folders -
[includeIf "gitdir:~/work/"]
  path = ~/work/.gitconfig

Now lets make that new gitconfig we just told git to use on our work folder like this -

# ~/work/.gitconfig
[user]
  email = work-mail
  name = work-name

Congratulations! and happy commiting!🎉🎉

Â