DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Using Git with two or more users
Axel Navarro for Cloud(x);

Posted on

Using Git with two or more users

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'
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ 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
Enter fullscreen mode Exit fullscreen mode

🧠 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
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ 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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 (10)

Collapse
fniro profile image
Facundo Niro • Edited on

Another (not so pretty) alternative would be to add an alias for it:
Example:

alias ssh="eval \"$(ssh-agent -s)\" && ssh-add --apple-use-keychain ~/.ssh/id_1"

alias my_other_ssh="eval \"$(ssh-agent -s)\" && ssh-add --apple-use-keychain ~/.ssh/id_2"
Enter fullscreen mode Exit fullscreen mode
Collapse
ezequielmonteleone profile image
Ezequiel Monteleone

Excellent, it helped me a lot on several occasions

Collapse
citizen428 profile image
Michael Kohl • Edited on

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.

Look at a Github remote (e.g. with git remote -v) and you'll see that all keys are for the user called git: git@github.com:citizen428/dotfiles.git (normal SSH notation, user@server:path).

Which user a commit gets associated to has nothing to do with the SSH key used but only the git config. As illustrated by the fact that I just made a commit with my key but your name and email address (don't worry, I'll remove it in a bit):

github.com/citizen428/dotfiles/com...

So while there may be other reasons to use different keys for different orgs (though none I find particularly compelling), there's also no problem with using the same key and having commits attributed to different email addresses.

Collapse
navarroaxel profile image
Axel Navarro Author

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.

Collapse
citizen428 profile image
Michael Kohl

Sure, the key is associated to a GH user, but GH users have no direct relation to the commits per se. The phrase I quoted may give readers less familiar with git (and specifically Github) the wrong impression, so I figured I'd point it out.

Thread Thread
navarroaxel profile image
Axel Navarro Author

Makes sense! Thanks for that. I'm sure it'll be useful to others as well.

Collapse
hutger profile image
hutger

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.

Host github-1
  HostName github.com
  User git
  IdentityFile <key_path_1>

Host github-2
  HostName github.com
  User git
  IdentityFile <key_path_1>
Enter fullscreen mode Exit fullscreen mode

and then

git clone git@github-1:hutger/my-repo.git
Enter fullscreen mode Exit fullscreen mode
Collapse
ccoveille profile image
Christophe Colombier

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.

Collapse
navarroaxel profile image
Axel Navarro Author

A problem can have multiple solutions, and the scenario defines which solution fits better.

Collapse
ccoveille profile image
Christophe Colombier

Yes of course, and it's what make opensource so magic.

We are all the crafting the solutions to our problems.

Now it's your turn.

Β 
Join DEV and share your story.