git from installation to multi-account operation is done in one set (two) multi-account use

git from installation to multi-account operation is done in one set (two) multi-account use

Author: conscience still exist

Reprint authorization and onlookers: welcome to add WeChat: Yu Linjun

Overview

GIT is a popular code management technology today, but such a hot system turned out to be a distributed version control system written by the great god Linus in C for two weeks. Github is the world's largest code management platform developed by git technology.

There are many such easy-to-use git tutorials, but because I encountered the need to implement the push of the company's git server and my own private remote warehouse on a computer at work, I read many articles and finally set it up successfully. Some operations require special attention, so I write this article to remind you, hoping to help you, from installation to use in one go.

Following the previous article, continue to share the use of GIT, from installation to multi-account operation with git (1) Getting started

**4. GIT multi-account configuration**

Linking to Github/GitLab via ssh requires a unique public key. If you want to bind two Github/GitLab accounts to the same computer, two conditions are required:

  • Able to generate two pairs of private/public keys

  • When pushing, you can distinguish between two accounts and push to the corresponding warehouse

solution:

1. When generating private/public keys, avoid duplication of key file naming

2. Set different Host to correspond to the same HostName but different keys

3. Cancel the git global username/mailbox setting, and set the username/mailbox independently for each warehouse

The previous article described an example of an account from generating an rsa key to pushing it to a github repository. Follow the previous article git from installation to multi-account operation . But this key needs special configuration.

1>View the existing key

At the command ls ~/.ssh/, see id_rsaand id_rsa_pubthen explained that it had a pair of keys. As shown below

2>Generate a new public key and name it id_rsa_2 (make sure it is different from the previous key file name, the file name should be meaningful, otherwise the file name is wrong when writing the following configuration, my file name is id_rsa_ljy)

ssh-keygen -t rsa -f ~/.ssh/id_rsa_2 -C "yourmail@xxx.com"
 

3>Create a new config file in the .ssh folder and edit it, so that different Hosts are actually mapped to the same HostName, but the key files are different. The Host prefix can be customized:

# default                                                                      Host 192.168.1.xx IP HostName 192.168.1.xx IP User gitIdentityFile ~/.ssh/id_rsa_2# two                                                                          Hostieit.github.comHostName github.comUser gitIdentityFile~/.ssh/id_rsa
 

Reference article:

blog.csdn.net/jifaliwo123...

Points to note :

  • In the configuration file, the IdentityFile file location is the rsa key, not the pub file

  • When submitting the code, you need to modify the git config. You can set a global user.email and user.name, and then you need different configured warehouses, which can be set separately

The previous article set --global user.name --global user.email

This multi-account use needs to be set up. The mailbox and name under a separate folder ensure that we use the files in the folder for git operations. The corresponding account name and mailbox belong to accounts other than the main account.

//Set the user.email and user.name of the warehouse

gitconfig user.email" yeungeek@gmail.com "

gitconfig user.name "yeungeek"

The previous part of setting up the public key on github or private server was also described in detail in the previous article. Next, use a new account for another method of git upload.

Here I am still taking the screenshot on github. Because of the confidentiality used by the company, I will not show it. I just said that I successfully used the above method to use one PC with two accounts.

**5. GIT multi-account use**

The second method is to directly upload remotely by establishing a local warehouse

echo "# Simple-red-black-tree" >> README.md

git init

git add README.md

git commit -m "first commit"

git remote add origingit@github.com :conscience-still/Simple-red-black-tree.git

git push -u origin master

Create a new README.md

$ git add.//Add to the staging warehouse

$ git commit -m "simple red-black tree"//Add upload comment

Set the user name and mailbox under this folder again

After creating the Git warehouse on Github, we can associate with the local warehouse. According to the prompt on the created Git warehouse page, you can enter the command line of the local Simple-red-black-tree warehouse

$ git remote add origin git@github.com :conscience-still/Simple-red-black-tree.git

After the association is complete, we can push all the contents of the local library to the remote warehouse (that is, Github), through:

$ git push -u origin master

Since the newly created remote warehouse is empty, the -u parameter must be added. After the remote warehouse has content, the next time you upload the content from the local library, you only need to do the following:

$ git push origin master

This completes the entire process of uploading the local project to Github.

In addition, there is a pit you need to pay attention to, that is, when you create a remote warehouse in the seventh step above, if you check Initializethis repository with a README (that is, a README file is automatically created for you when the warehouse is created), then it is the first step. 9.Steps When you push the contents of the local warehouse to the remote warehouse, you will report a failed to push some refs to github.com/guyibang/TE...

This is because the README file in the newly created warehouse is not in the local warehouse directory. At this time, we can first merge the contents as follows through the following command:

$ git pull --rebase origin master

At this time, you can push again to succeed.

Reference article:

blog.csdn.net/Lucky_LXG/a...

This is the basic operation of git that I shared. The process in it is practiced. Many places are done while learning. Thank you for the articles of many bloggers. Many of them did not communicate directly. Just express some gratitude here. If you have any better ideas, welcome to share and exchange.