Managing Git Identities
If you'd like to keep your real-life and modding identities separate, Git and SSH let you manage the identities on a per-folder basis.
In other words, you can automate the switch of identities. The final result is that you need to run a single command once for each cloned repository, so it applies the correct identity later on.
This manual is applicable to Linux and Windows. The guide may work on MacOS, but it was not tested.
As a git console for Windows, you can use Git Bash.
Configuring name and email per folder
The manual will use "ChangeMe" as a dev identity. Replace it with your own before applying the commands.
Create a ~/.gitconfig
file and populate with the paths you want to store your different identities in.
When you are in the includeIf
folder or any of the children folders, it will use the path you specify to populate the user.name and user.email fields:
# Path to the config of the dev identity
[IncludeIf "gitdir:~/git/greg/**"]
path = ~/.gitconfig-changeme
# Path to the config of the IRL identity
[IncludeIf "gitdir:~/git/work/**"]
path = ~/.gitconfig-irl
The next step is to fill the paths you specified with the desired names and emails:
# In file ~/.gitconfig-changeme
[user]
email = changeme@example.com
name = ChangeMe
The result is that git will use the ChangeMe identity for all commit-signing actions in those the folder ~/git/greg/
and its child folders.
You can check it by doing the following:
- Clone any repository into your dev-identity folder.
- Commit anything into the repository.
- Check the credentials of the commit with
git log -1
. - You should see your dev credentials in the shown commit.
Managing multiple SSH keys
When pushing to Git, it will require SSH keys associated with your account.
In our case, we have multiple identities, so if we don't tell SSH to use different keys for different identities, it will send a wrong key and the push will fail.
We need to generate a new SSH key-pair. The following command will create a new public and private key in the ~/.ssh
folder:
# -t is algorithm type, -C is the comment to remember what this key is for, -f is the save path
ssh-keygen -t ed25519 -C "changeme@example.com" -f ~/.ssh/changeme_ed25519
# The resulting keys will be named changeme_ed25519 and changeme_ed25519.pub
Next open ~/.ssh/config
and define aliases for when to use which keys:
Host changeme
User git
Hostname github.com
IdentityFile ~/.ssh/changeme_ed25519
Host github.com
User git
Hostname github.com
IdentityFile ~/.ssh/your_irl_ssh_key_goes_here
Now if you push to git@github.com
, the default URL you paste from Github, it will use the public identity, and if you push to git@changeme
, it will use the dev identity.
The last and recurring step
You need to do this step once for each repository where you want to use your dev identity. Every time you clone or re-clone a repository, you need to do this step.
For each dev repository, run the following command to point to the new remote:
git remote set-url origin git@changeme:<remote details from the clone button, eg GTNewHorizons/GT-New-Horizons-Modpack.git>
You can check that you set it correctly by
- Checking the results of
git remote -v
, - Committing locally and checking the credentials,
- Pushing to remote successfully.
You are now using a unique name, email, and SSH key for your dev identity, and it is automatically applied without any manual switching of credentials.
However, please remember that it works only for the path you set up in gitconfig, so you need to update these files if you move your dev folder.