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.

Latest comments (8)

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
 
navarroaxel profile image
Axel Navarro

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

Collapse
 
navarroaxel profile image
Axel Navarro

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
 
fniro profile image
Facundo Niro • Edited

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
 
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

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.

Collapse
 
ezequielmonteleone profile image
Ezequiel Monteleone

Excellent, it helped me a lot on several occasions