Sometimes we need to use more than one Git user in the same machine. This is simple to configure but we need to know a few things before using this feature.
By default the SSH key is generated in the
~/.ssh/ directory, and it's called
id_ed25519, depending on the signature type. To create a key with a specific filename we can use the following command
ssh-keygen -f filename -t ed25519 -C 'machine_name'
💡 If you're going to use this key for a specific organization, it's very useful to use the format
id_orgname for the filename.
The comment specified by using the
-C argument should be something useful to know why this key was created 🤔. It could be the name of the machine that uses this SSH key or any notation that helps you. This comment is only visible by you.
It depends. The source code hostings, like GitHub, associate the SSH key to a specific user of the platform to know which user is pushing code to a Git repository, check the access, etc. If your company doesn’t use Github, con can also use the same key for a personal email in Github and your company’s email in GitLab.
git clone command uses the SSH protocol to connect to the Git hosting, and the SSH program in your machine uses the default key (usually located at
~/.ssh/id_ed25519) for authentication. You should use a custom SSH command to clone the repository using this:
git clone --config core.sshCommand="ssh -i path/to/private_ssh_key" email@example.com:orgname/repo.git
🧠 Remember that, if the key name ends with
id_orgname.pub, then that is the public key. You should use the key file without this extension because that's your private key.
If you want to use a different email address, configure it with the following commands:
git config user.name "John Doe"
git config user.email firstname.lastname@example.org
💡 Use the
--global argument to set default settings for all the Git repositories in the machine. Otherwise the
git config command only sets configuration values for the repository in the current directory.
If you've cloned a Git repository but you need to use a new SSH key to push code, you can use this command to set a new SSH command:
git config core.sshCommand "ssh -i path/to/private_ssh_key"
Maybe your company has their Git repositories in a custom port 🙈 (
22 is the default port for SSH), you need to clone these repositories with the following option:
git clone --config core.sshCommand="ssh -p 2222" email@example.com:path/to/repo.git
You can use specific Git configurations for each repository, like commit author's information or specific command aliases.
The variations for the SSH commands to connect to a repository using the SSH protocol can be changed anytime.