Setup different gitconfig based on Folder with SSH Config

August 07, 2021

Recently, one of my PR got merged in React Core. I was over the moon to see it merged and I took a screenshot and shared it on social media. I was quite happy thinking my name would now show up in the contributors’ section in the GitHub repository of facebook/react. But here is the twist - it was not!

Normally the name shows up in GitHub if you have even one single PR. It got me thinking why wasn't my name on that list. I had configured the SSH correctly for work and private GitHub; then why would this happen?

I had to go back and figure out how GitHub shows the contribution chart.

My findings:

  1. When you first install the git in your system, you have to provide two values to the local git - your name and your commit email. Then, when you commit anything in any git repository, it associates those two values with the commit. So, when I set up the git first time in the current system it was my work email. This meant all of the commits I did during the last six months were signed by my work email. And I thought I knew enough git to be dangerous!

  2. Next thing I did was to search if we can use different emails for different projects. One solution was to setup an email for every folder, which is not practical if you clone git repos every now and then. Instead, I needed a generic workflow that could help me use different emails for different folders. I had already segregated folders for work and private so that would have been a more natural solution for the way I organize git repos in my system.

  3. I have found a solution to this problem. Conditional Configuration was released to git back in 2017. For future me, I am writing this blog listing all the configurations I need when setting up git in a new machine.

Setting up SSH

  1. Create a key from terminal
ssh-keygen -t ed25519 -C "[email protected]"
  1. Add eval "$(ssh-agent -s)" to .zshrc
  2. Adding Key to agent ssh-add -K ~/.ssh/id_ed25519
  3. Create SSH config in ~/.ssh folder (example)
Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/personal
Host bitbucket.org
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/workkey
  1. Now add the keys to their respective git providers GitHub, bitbucket etc.

Setting up different git config based on Folder

  1. In home folder you can add .gitconfig for different situations
user]
    name = Shubham Pandey
    email = [email protected]       # default email if no config provided
    useConfigOnly = true
[includeIf "gitdir:~/mysites/work/"]       # For Work folder
    path = ~/mysites/work/.gitconfig         # use this config file, you can define them in any folder
[includeIf "gitdir:~/mysites/experiment/"] # For Personal folder
    path = ~/mysites/experiment/.gitconfig   # use this config file
  1. Now create those two configs in their paths. (example for personal folder)
[user]
    email = [email protected]
    name = Shubham pandey

That's it. Now you have configured two different emails for two different folders, now I can commit in peace. Hopefully, my next PR to react comes sooner.

Thanks for reading if you have any comments or feedback you can reach out to me on twitter.

Up Next