banner
keney

keney

remain optimistic
twitter

Git Multiple Account Configuration

1. Background#

On the same computer, sometimes you need to use multiple GitHub, gitee, and gitlab accounts, or you have multiple accounts and don't want to generate public keys back and forth. In this case, you need to use git to configure multiple accounts.

2. Preparation#

Environment: Windows 10, git

Before using it, you need to install git. Installing git is as simple as clicking "Next" all the way. You also need to have a certain understanding of git commands.

Prerequisites: Need the C:\Users\nxg.ssh directory (folder), open git bash in it

As shown in the figure below:

image-20221031222311849

After opening it, use this interface:

image-20221031222421017

Note: For those with a certain foundation, the path C:\Users\nxg.ssh can be understood based on the above figure, so it will not be repeated here.

Why do we need to perform this step first? The reason is that when you create the config file and generate the key, they all need to be created in this .ssh directory (folder). If you open it in another directory, then you need to consider the path issue when creating the file. In order to be convenient and clear, use the above method to operate, saving time and effort.

3. Configuration#

3.1 Create the config file#

Create the config file in the C:\Users\nxg.ssh directory (folder) and configure the routing strategy for the ssh key.

Create the config file:

touch config

Configure in the config file:

# GitHub
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
    User git
    
# Github2
Host github2
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github2
    User git    

# gitee
Host gitee.com # Your own gitee address
    HostName gitee.com  
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_gitee
    User git
    

The effect is shown in the figure below:

image-20221031224144979

"#" indicates a comment
Host is followed by a name, which can be written arbitrarily, which is equivalent to giving an alias

Each Host represents a repository, and the repository parameters start on a new line with 4 spaces in front
HostName: the actual server address to connect to

ssh host, the content after "git@" and before the colon

User: custom username, usually "git"
PreferredAuthentications: specifies which authentication method to use, supporting password and key authentication. Use "publickey" for all git repositories

Authentication methods can be set as publickey, password publickey, keyboard-interactive, etc.
IdentityFile: specifies the absolute path of the private key corresponding to the public key configured in the background of this host
Port: The default port number for SSH is 22. Some privately deployed git repositories may change the port number

3.2 Global Configuration#

Taking GitHub as an example:


# Global configuration, using global configuration here
# Set username:
git config --global user.name 'abc'  
# Set email:
git config --global user.email  "[email protected]"

# Add the key to the local, that is, generate the key
ssh-keygen -t rsa -C "[email protected]"

# The same applies to gitee accounts.

Note: Here, abc is the GitHub username, and [email protected] is the email address bound to your GitHub account.

The effect is shown in the figure below:

image-20221031232651471

The effect of configuring gitee is shown in the figure below:

image-20221031232222217

Note: The generated key file needs to be consistent with this.

image-20221031233525370

Test if the connection is successful#

Test if the connection to GitHub is successful:

ssh -T git@github2

Successful result:

Hi abc! You've successfully authenticated, but GitHub does not provide shell access.

Test if the connection to Gitee is successful:

Successful result:

Hi nxg! You've successfully authenticated, but GITEE.COM does not provide shell access.

Note:

image-20221031234333269

4. Associate with GitHub#

Add the contents of the id_rsa_gitee.pub file to the SSH public key in the gitee settings.

Add the contents of the id_rsa_github2.pub file to the SSH public key in the SSH and GPG keys section of the GitHub settings.

Note: The files mentioned above are in

image-20221101162514113

The effect is shown in the figure below:

image-20221101162133360

Note#

There are multiple ways to configure multiple accounts. You can modify the files directly or use commands.

Here, we use the method of modifying files.

First, you need to find the .gitconfig file in the C:\Users\nxg path and open this file.

image-20221031235250843

Note: Don't double-click to open it directly. You need to choose the open method, open it as a text file, or open it with other editing tools.

Content of the .gitconfig file:

[user]
	name = xiaoli
	email = [email protected]
[core]
	autocrlf = true
[http]
	sslVerify = false
[user]
	name = abc
	email = [email protected]
[core]
	autocrlf = true
[http]
	sslVerify = false

The effect is shown in the figure below:

image-20221031235759564

View the account list:#

git config --global --list

The effect is shown in the figure below:

image-20221101000219730

Adding keys#

Method 1:

ssh-keygen -t rsa -C "[email protected]"

Then execute id_rsa

image-20221031232651471

Method 2:

ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa

The effect is shown in the figure below:

image-20221101000544601

Explanation of commonly used parameters for ssh-keygen:

-t: Key type, you can choose dsa | ecdsa | ed25519 | rsa;

-f: Key directory location, default is the .ssh hidden directory under the current user's home path, that is, ~/.ssh/, and the default key file name starts with id_rsa. If it is the root user, it will be in /root/.ssh/id_rsa, if it is another user, it will be in /home/username/.ssh/id_rsa;

-C: Specify the comment information for this key, it is recommended to carry it when configuring multiple passwordless logins;

-N: Specify the password for this key pair. If this parameter is specified, there will be no interactive confirmation of the password during the command execution.

Example: Specify the directory location, password, and comment information at the same time, so you don't need to press the Enter key to complete the creation:

Attachment#

Git Single Account Usage Tutorial

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.