Committing as a Different User

June 18, 2021

First create a new SSH key pair to use with your alter­nate account.

ssh-keygen -t rsa

Add the public key to your alter­nate account on Github (Set­tings > SSH and GPG keys).

Once that’s done, add a new entry in your SSH config, usu­al­ly locat­ed in ~/.ssh/config:

Host github.com-something
   HostName github.com
   User git
   IdentityFile ~/.ssh/something

github.com-something is a unique place­hold­er that SSH will use to resolve the set­tings to use (in this case, which actual host to con­nect to, using which pri­vate key) — it can be any­thing you like. Point IdentityFile to the loca­tion of the pri­vate key of the SSH key pair.

In the .git folder of the pro­ject you want to commit to using your alter­nate account, put the fol­low­ing set­tings in .git/config:

[user]
        name = My Alternate Account
        email = my_alterate_email@gmail.com
[remote "origin"]
        url = git@github.com-something:githubuser/repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*

Make sure the same github.com-something in the SSH config entry is used as the host in the url field. Tweak the rest of the set­tings to your liking.

That’s it!