1

Using Git with two or more users

 1 year ago
source link: https://dev.to/cloudx/using-git-with-two-or-more-users-5437
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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" [email protected]: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 [email protected]

💡 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" [email protected]: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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK