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.
Creating an additional SSH key
By default the SSH key is generated in the ~/.ssh/
directory, and it's called id_rsa
or 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.
Can we use the same key for all the users?
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.
Cloning a repository with a specific SSH key
The 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" git@github.com:orgname/repo.git
🧠 Remember that, if the key name ends with .pub
, e.g. 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 john@example.com
💡 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.
Changing the key to an already cloned repository
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"
Extra SSH tips
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" git@git.example.com:path/to/repo.git
Conclusion
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.
Top comments (8)
Another (not so pretty) alternative would be to add an alias for it:
Example:
Yeah! You're right, you can set anyone's email in the commits that you make. That's why it's a good idea to sign your contributions with a GPG key like I said here dev.to/cloudx/sign-your-code-31oo.
But your SSH key is associated with a specific GitHub user because it's the way GitHub verifies your access to a private repository or a write (
git push
) access to a repository, public or private.Excellent, it helped me a lot on several occasions
Makes sense! Thanks for that. I'm sure it'll be useful to others as well.
Hi there, an alternative approach in cases when you have multiple different users and multiple repos, ssh config file (~/.ssh/config) can be also very handy.
e.g.
and then
I might be wrong, but you are trying to solve the same issue that @rizkyzhang faced
He posted it yesterday dev.to/rizkyzhang/setup-multiple-s...
I like your idea, but I found @rizkyzhang cleaner.
A problem can have multiple solutions, and the scenario defines which solution fits better.
Yes of course, and it's what make opensource so magic.
We are all the crafting the solutions to our problems.