skip to content
Posts · November 2026

Maintaining a clean workspace for multi-account git setups


If you have many accounts in hosts like GitHub, GitLab, etc., you may have found it troublesome to manage credentials on your machine (though if you’ve got it figured out, this article is probably old news for you).

Below is the approach I take to keep it clean and organized.

1. Directory structure

.gitconfig
Code/
├── github/
│ ├── github-user-1/
│ │ ├── .gitconfig
│ │ ├── project-1/
│ │ └── project-2/
│ └── github-user-2/
│ ├── .gitconfig
│ └── project-1/
├── gitlab/
│ ├── gitlab-user-1/
│ │ ├── .gitconfig
│ │ └── project-1/
│ └── gitlab-user-2/
│ └── .gitconfig
└── other-git-host/
└── ...

Here’s how it works:

  • The global .gitconfig tells git what .gitconfig to use to override defaults based on the working path.
  • The local .gitconfigs specify user info (name, email) as well as the ssh command to use when interacting with the origin. The private SSH key is added by this command, so any further requests to git are authenticated.

2. SSH Keys Directory

I keep my .ssh directory like this:

.ssh/
└── git/
├── github/
│ ├── github-user-1/
│ │ ├── id_ed25519
│ │ └── id_ed25519.pub
│ └── github-user-2/
│ ├── id_ed25519
│ └── id_ed25519.pub
├── gitlab/
│ ├── gitlab-user-1/
│ │ ├── id_ed25519
│ │ └── id_ed25519.pub
│ └── gitlab-user-2/
│ ├── id_ed25519
│ └── id_ed25519.pub
└── other-git-host/
└── github-user-2/
├── id_ed25519
└── id_ed25519.pub

3. .gitconfig Structure

a. Global ~/.gitconfig

This file (probably included by default the moment you installed git) exists in your $HOME. Use includeIf to load another file if the path matches

~/.gitconfig
[user]
name = Stavros Tsioulis
[init]
defaultBranch = main
[includeIf "gitdir:~/Code/github/stavros-tsioulis/"]
path = ~/Code/github/stavros-tsioulis/.gitconfig
[includeIf "gitdir:~/Code/github/github-user-2/"]
path = ~/Code/github/github-user-2/.gitconfig
[includeIf "gitdir:~/Code/gitlab/gitlab-user-2/"]
path = ~/Code/gitlab/gitlab-user-2/.gitconfig

I will explain what each of them does:

  • gitdir: matches the location of the repository, not the directory you happen to run git from, so git -C, your editor’s git integration, and any script still pick the right account.
  • The trailing / means “everything under this directory”. Git appends ** automatically, so a single [includeIf] covers all the projects inside.
  • Git reads the file top to bottom, so the included file overrides whatever comes before it. That is why the [user] defaults are at the top.
  • On a case-insensitive filesystem (like macOS by default), use gitdir/i: if the case of the path in your config doesn’t match reality. Though I recommend keeping all your paths under your code directory in kebab-case.

b. Per-account .gitconfig

Each account directory gets a file with the identity that should be used inside it, plus the SSH key git has to authenticate with.

~/Code/github/stavros-tsioulis/.gitconfig
[user]
name = Stavros Tsioulis
[core]
sshCommand = ssh -i ~/.ssh/git/github/stavros-tsioulis/id_ed25519 -o IdentitiesOnly=yes
  • user.name and user.email end up in every commit made from a repository under this directory, so no more commits with the wrong author.
  • core.sshCommand replaces plain ssh for every fetch, pull, push, and clone git performs here, which pins the key to the account.
  • IdentitiesOnly=yes stops ssh from offering every key your agent is holding. Without it, the server may pick another identity you happen to have and authenticate you as the wrong user.

You can add your own overrides as well, such as a signing key (user.signingkey), commit.gpgsign, or a url.<base>.insteadOf rewrite.

4. Create your ssh keys

For each of the accounts you created, you need to create those SSH keys in their corresponding directories. Here’s a quick one-liner that creates the directory, generates the key, and prints the public key so you can register it on the corresponding host:

Terminal window
HOST=github USERNAME=stavros-tsioulis \
&& mkdir -p ~/.ssh/git/$HOST/$USERNAME \
&& ssh-keygen -t ed25519 -C "$USERNAME@$HOST" -f ~/.ssh/git/$HOST/$USERNAME/id_ed25519 -N "" \
&& cat ~/.ssh/git/$HOST/$USERNAME/id_ed25519.pub

Don’t move your private keys without updating the relevant .gitconfig or you’ll fail to authenticate.

5. Next steps

On an upcoming article I will write down how to better secure those keys so they don’t sit on your machine in plaintext, especially now that our agents are running with --dangerously-skip-permissions.