Git
Multiple Git Credentials
Users may have multiple accounts on git hosting providers (e.g. GitHub). Unfortunately, GitHub requires different accounts to have different SSH keys. This makes it difficult to interact with each of those accounts on a single user account on the local computer. Luckily, there are ways to solve this using git and SSH configuration files.
Specific .gitconfig Files
Git provides an includeIf field that can be used to specify a unique set of git configuration attributes. This can be used to specify unique attributes for specific parent paths.
There are several caveats to this method. One, the directories will be the only place these unique attributes will apply. Second, the attributes only apply to git directories within the specified paths. This means git clone will not work since the command itself might not occur within a git directory.
First, create a new custom .gitconfig file with custom attributes. Make sure to include a custom sshCommand attribute to ensure the right secret key file is used.
[core]
sshCommand = ssh -i /path/to/ssh/secret_key -F /dev/nullFinally, modify the main ~/.gitconfig. Specify the parent directory in the includeIf field and set the path attribute to the custom .gitconfig file.
[includeIf "gitdir:/path/to/parent/dir"]
path = /path/to/custom/.gitconfigMore information can be found at these links:
How to Use Multiple Git Configs on One Computer on FreeCodeCamp
git: includeIf not working with git clone on stack overflow
SSH Host Config
A better way is to set up an SSH config and a URL override in git config. This allows running git anywhere on the system without limiting the user to a specific parent path.
First, update the ~/.ssh/config file with a host URL label and a custom ssh private key.
Finally, modify the ~/.gitconfig file with a host URL override. This modification causes git to replace the user's github URL path with the host URL label defined in the SSH config file. The label between the two files must match.
More information can be found at the following link:
git: includeIf not working with git clone on stack overflow
Last updated
Was this helpful?