Skip to content

Instantly share code, notes, and snippets.

@robandpdx
Created May 7, 2025 20:58
Show Gist options
  • Save robandpdx/68bab0c65d2e2dcf096769dcb33c79f2 to your computer and use it in GitHub Desktop.
Save robandpdx/68bab0c65d2e2dcf096769dcb33c79f2 to your computer and use it in GitHub Desktop.
Managing multiple github accounts for cloning and commit signing

Assuming you already have an ssh key pair for each account and you are using that ssh private key to sign commits, you can manage the different accounts by configuring git to use a different configuration profile based on the directory under which you are cloing the repository.

For each key pair (account) you will create a config file that will apply to all the projects under that account's work directory.

/Users/robandpdx/
    |__.gitconfig
    |__.gitconfig.work
    |__.gitconfig.personal
    |__github-work/                # <--- This is where you will clone all your work repositories
    |__github-personal/            # <--- This is where you will clone all your personal repositories

In your .gitconfig.work config file...

# ~/.gitconfig.work
[user]
    name = Your Name
    email = [email protected]
    signingkey = "~/.ssh/<work_private_key>"

[gpg]
    format = "ssh"

[commit]
    gpgsign = true

[github]
    user = "userhandle_code"
 
[core]
    sshCommand = "ssh -i ~/.ssh/<work_private_key>"

In your .gitconfig.personal config file...

# ~/.gitconfig.personal
[user]
    name = Your Name
    email = [email protected]
    signingkey = "~/.ssh/<personal_private_key>"

[gpg]
    format = "ssh"

[commit]
    gpgsign = true

[github]
    user = "userhandle"
 
[core]
    sshCommand = "ssh -i ~/.ssh/<personal_private_key>"

Each config file tells git what email and signing key to use for commits, as well as the identity key to use for ssh clones. In your global .gitconfig file we configure git to use each config file based on the directory path you are in...

# ~/.gitconfig

[includeIf "gitdir:~/github-work/"] # for all .git projects under ~/github-work/
    path = ~/.gitconfig.work

[includeIf "gitdir:~/github-personal/"] # for all .git projects under ~/github-personal/
    path = ~/.gitconfig.personal
 
[core]
    excludesfile = ~/.gitignore      # for everywhere

With these configurations in place, you can clone work projects to ~/github-work/ and personal projects to ~/github-personal using the ssh clone url, and commits will be signed with the correct key for that account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment