Quick Guide: Getting Git set up for home and work

Let’s imagine a hypothetical situation. You’ve used Git with your personal GitHub account for the longest time, but once you got a job in computer science or entered university, it turns out they have their own private Git remote! Sound familiar?

There are numerous ways to get private repositories cloned on your system — some of the most popular are through generating an SSH key or using Git GCM. However, once you are able to set this up, you would still be making commits to your work/school Git remote under the email and name of your private account (or what you set Git up with initially)! Nobody wants to see an unverified commit from a nonexistent user called “coder700″…1

The fix for this is actually really simple. Let’s say we have two folders; one where we clone our GitHub repositories to, and one where the repositories on the private remote are cloned to. We’ll call these ./GitHub and ./GitWork, respectively. First, let’s type a Git command to see what our global username and email are:

git config --global --editCode language: PHP (php)

This tells you a bit more than that, actually — it’ll open your Git configuration file in an external editor. Notice how we have a block for our username and email (and potentially a signing key if you have that set up). What we want to do is define a custom username/email/signing key for repositories in ./GitWork.

First, copy everything under [user]. Then, add this to the bottom of the configuration file:

[includeIf "gitdir:GitWork/"]
    path = $PATH/.gitconfig

In this case, replace $PATH with the file path to another git configuration file — with the filename .gitconfig. This can be anywhere, but I recommend placing it in ./GitWork. Then, you can just copy the absolute file path and paste it instead of $PATH.

After creating this second .gitconfig, we want to edit it with a text editor. Paste the block you copied earlier, and swap the name and email values for the username and email of your work account. (If you have a signing key, now would be a good time to get it set up with your private Git remote).

Now, when you clone a repository to ./GitWork and make a commit, it should use a specific name and email for your work/school account, but making commits in ./GitHub will still use the username and email for your personal account!

Hope this helped, and I’ll catch you later with hopefully a normal blog post soon!

  1. The way some programs like GitLab and GitHub work is that for each commit, they’ll look up the username/email attached to it on their own servers to try and bring relevant information on their web interfaces (like a profile picture, or loading a user profile). However, there’s a good chance that there’s no user on your private GitLab server with the same username and email as your personal account on GitHub. ↩︎

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *