In the process of getting started learning Git, this confusion arises:
On the computer, if we want to use Git, we need to configure the user name and email through the command
git config --global user.name "myname"and
git config --global user.email "firstname.lastname@example.org"to indicate “who” is operating Git, to do a series of operations such as submitting code.
In addition, if we need to use a code hosting service, such as [GitHub] , GitHub will also ask us to provide email and username to register to register, and username and email are also required.
Furthermore, if we choose to use ssh to access GitHub (more convenient than using https, https needs to enter the username and password every time), we need to generate a key pair. The method recommended by many people to generate a key pair is to use commands
ssh-keygen -t rsa -C "email@example.com", and then To provide an email.
What is the relationship between the emails of these three places and how to set them up? The following is my understanding, it may be biased, judge officials to take the essence, get rid of the dross, and don’t speak ill of each other.
basic premise construction
1. Personal computer situation
|HoPC||Desktop computer used at home, Mac system|
|CoPC||Desktop computer used by the company, Windows system|
|Book||Own laptop, Windows system|
|VM||Development virtual machine, Linux system|
2. Code hosting website account status
|code hosting site||username||email address||illustrate|
Assume the situation is this, need to do development on 4 computers (3 real physical machines, 1 virtual machine), we assume that part of the personal interest work, the code is hosted on GitHub, the other part is the company’s work, the code is hosted On Gitee, in reality, it may be a private server built by the company, and Gitee is used here to assume.
how to set
1. How to generate and set SSH keys
When setting the SSH key, you must understand that the SSH key pair is essentially for convenience and is used to replace the authentication method of username and password. From the point of view of the code hosting website, when we registered, we provided our email address, user name, and set a password, so the code hosting website can only verify whether it is really the owner of our account through the username and password. in operation. Therefore, if the https method is used, there is no need to generate any key pair, but it is inconvenient to enter the password every time.
In the SSH method, a key pair is generated locally, and then the public key is added to the secret key allow list of the code hosting website. The step of adding the public key to the allow list of the website requires password authorization. In doing so, the site acknowledges the fact that accessing me with this key pair is equivalent to providing a password. The key pair is stored on a certain machine, so that accessing the website from this machine does not require a password, because the key can be found on this machine to replace the password for authentication.
Therefore, on many blogs, when the key pair is generated, the command is:
ssh-keygen -t rsa -C "firstname.lastname@example.org", the email address with the -C parameter in it is of no practical use. From the command itself, -C just adds a comment to the generated key pair. Open id_rsa.pub with notepad++, you can see that there is this email address at the end, so that when you get the key pair in the future, you may be able to recall what the key pair was originally generated for.
Conclusion 1: When the ssh key pair is generated,
ssh-keygen -t rsa -C "email@example.com"the email entered in it and the username set by Git have nothing to do with the username of code hosting websites such as GitHub.
The recommended practice is: when a key pair is generated on each computer, the content of the commented information is mainly related to this machine, and when the key is added to the list of the code hosting website, use this closely related to a certain computer. The name. In the future, if this computer is no longer used, it is convenient to delete this key from the website.
|computer abbreviation||Key pair generation annotation||Key title on code hosting site|
Four key pairs are generated, and the public keys of these four key pairs are added to the SSH public key lists of GitHub and Gitee respectively. For a certain computer, only one key pair is needed to access multiple code hosting websites. Here, when accessing GitHub, the secret key replaces the password of the firstname.lastname@example.org account, while accessing Gitee , it represents the account email@example.com .
2. How to set git local username and email
What needs to be noted here is how the code hosting website handles the locally set account. The code hosting website mainly looks at email, and uses the email address to match the email address of its own account name. If the same, the code hosting website considers this operation to be the operation of the account owner. for example:
If the locally set user.email value is: firstname.lastname@example.org , since the email address of the account on GitHub is also email@example.com , if you push from this computer, GitHub will consider this push as the account. What the owner does by himself is the same as logging in directly to GitHub and modifying from the website. The same person who modifies is the account owner.
In addition, it should be noted that user.name and user.email can set their own user.name and user.email values in specific code directories in addition to setting global values (–gloabl).
The setting strategy of user.name and user.email is: when setting the global value, consider whether this computer does more of the company’s work or more of its own. For example, the company’s computer obviously does more of the company’s work. We’d better set it as: user.email = firstname.lastname@example.org , user.name = company. When we need to do some private work on the company computer, in the specific project directory,
git config user.name "personal",
git config user.email "email@example.com", change the value to meet our own requirements.
One pair of key pairs generated by ssh-keygen is enough for each computer. Some blogs instruct one computer to configure multiple key pairs, which is not necessary.
On the code hosting website, it is recommended to use information about the computer itself as the title of the key pair. It is also recommended to use this information as a key pair annotation when generating an ssh key pair locally.
When configuring the local git username, the global configuration (–global) considers which server the work on the computer is mainly pushed to, and it is best to configure the username and email address consistent with that server.
If the work on this computer needs to be pushed to other servers besides the main push server, then in the specific project directory, configure the user name and email address to be the same as the specific server.