git remote library connection

Remote warehouse

Reading: 170525450


So far, we have mastered how to time travel a file in the Git repository, you no longer have to worry about file backup or loss.

However, children's shoes that have used the centralized version control system SVN will stand up and say that these functions are already in SVN, and I don't see anything special about Git.

That's right, if you only manage the file history in a warehouse, there is no difference between Git and SVN. In order to ensure that the Git you are learning is value for money, you will never regret it in the future. At the same time, in order to combat children ’s shoes that have unfortunately learned SVN, this chapter begins to introduce one of Git ’s killer features (note that it is one, which is also behind There are two and three ...): Remote warehouse.

Git is a distributed version control system. The same Git repository can be distributed to different machines. How is it distributed? At the earliest, there must be only one machine with an original version library. After that, other machines can "clone" the original version library, and the version library of each machine is actually the same, and there is no distinction between major and minor.

You will definitely think that you need at least two machines to play the remote library, right? But I only have one computer, how to play?

In fact, multiple repositories can be cloned on a computer, as long as they are not in the same directory. However, in real life, no one would be so stupid to play several remote libraries on a computer, because it is completely meaningless to engage in several remote libraries on a computer, and all the libraries will be hung up if the hard disk is hung. So I do n’t tell you how to clone multiple repositories on one computer.

The actual situation is often like this. Find a computer to act as a server and turn it on 24 hours a day. Everyone else clones a copy from this "server" warehouse to their own computer, and pushes their respective submissions to the server warehouse. And also pull other people's submissions from the server warehouse.

It is entirely possible to build a server running Git by yourself, but at this stage, it is definitely a big deal to build a server in order to learn Git. Fortunately, there is a magical website called GitHub in this world. As you can see from the name, this website provides Git repository hosting services, so as long as you register a GitHub account, you can get the Git remote repository for free.

Before continuing to read the follow-up content, please register your GitHub account. Since the transmission between your local Git repository and GitHub repository is encrypted via SSH, a little setup is required:

Step 1: Create SSH Key. In the user's home directory to see if there .ssh directory, and if so, take a look at this directory there id_rsaand id_rsa.pubthese two documents, if you already have, you can jump directly to the next step. If not, open Shell (open Git Bash under Windows) and create SSH Key:

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

 

 

You need to change the email address to your own email address, and then press Enter all the way. Use the default value. Since this Key is not used for military purposes, there is no need to set a password.

If all goes well, can be found in the user's home directory in the .sshdirectory, there are id_rsaand id_rsa.pubtwo documents, both SSH Key is secret key pair, id_rsaa private key can not leak out, id_rsa.pubis the public key, you can safely tell anyone .

Step 2: Log in to GitHub, open the "Account settings", "SSH Keys" page:

Then, click "Add SSH Key", fill in any Title, and paste id_rsa.pubthe contents of the file in the Key text box :

github-addkey-1

Click "Add Key", you should see the added key:

github-addkey-2

Why does GitHub need an SSH Key? Because GitHub needs to recognize that the commit you pushed is indeed pushed by you, not someone posing, and Git supports the SSH protocol, so as long as GitHub knows your public key, you can confirm that only you can push.

Of course, GitHub allows you to add multiple keys. Suppose you have several computers, you submit them at the company later, and at home later, as long as you add the key of each computer to GitHub, you can push to GitHub on each computer.

As a final reminder, anyone can see the Git repository hosted free on GitHub (but only you can change it). Therefore, do not put sensitive information in it.

If you don't want others to see the Git repository, there are two ways. One is to pay a protection fee to make GitHub make the public repository private, so that others can't see it (unreadable and unwriteable). Another way is to do it yourself and build a Git server, because it is your own Git server, so others can't see it. This method, which we will talk about later, is quite simple and necessary for internal company development.

After making sure you have a GitHub account, we will soon start learning about remote repositories.

 

Add remote library

Reading: 209581463


The current situation is that after you have created a Git repository locally, you want to create a Git repository on GitHub, and let the two repositories be synchronized remotely, so that the repository on GitHub can be used as a backup and other People collaborate through this warehouse, which is really a multi-task.

First, log in to GitHub, then find the "Create a new repo" button in the upper right corner to create a new repository:

github-create-repo-1

Fill in the Repository name learngit, keep the other default settings, and click the "Create repository" button to successfully create a new Git repository:

github-create-repo-2

At present, this learngitwarehouse on GitHub is still empty. GitHub tells us that we can clone a new warehouse from this warehouse, or we can associate an existing local warehouse with it, and then push the contents of the local warehouse to the GitHub warehouse. .

Now, according to the GitHub prompt, learngitrun the command in the local repository:

$ git remote add origin [email protected]:michaelliao/learngit.git

Please be careful to michaelliaoreplace the above with your own GitHub account name, otherwise, you are locally linked to my remote library, there is no problem with the link, but you will not be able to push in the future, because your SSH Key public The key is not in my account list.

After adding, the name of the remote library is originthat this is Git's default name, and it can be changed to something else, but originthe name will know that it is the remote library at a glance.

In the next step, you can push all the contents of the local library to the remote library:

$ git push -u origin master
Counting objects: 20, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (20/20), 1.64 KiB | 560.00 KiB/s, done.
Total 20 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), done.
To github.com:michaelliao/learngit.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Push the contents of the local library to the remote, using the git pushcommand, actually masterpush the current branch to the remote.

Because the remote library is empty, when we first pushed the masterbranch, with the added -uparameters, Git will not only masterpush the remote new masterbranch of the local branch content , but also associate the local masterbranch with the remote masterbranch. When you push or pull, you can simplify the command.

 

After the push is successful, you can immediately see on the GitHub page that the content of the remote library is exactly the same as the local one:

github-repo

From now on, as long as the submission is made locally, you can use the command:

$ git push origin master

masterPush the latest changes to the local branch to GitHub, and now you have a truly distributed repository!

SSH warning

When you first connect to GitHub using Git clone(克隆)or pushcommands, you will get a warning:

The authenticity of host 'github.com (xx.xx.xx.xx)' can't be established.
RSA key fingerprint is xx.xx.xx.xx.xx.
Are you sure you want to continue connecting (yes/no)?

This is because Git uses an SSH connection, and when SSH connection verifies the key of the GitHub server for the first time, you need to confirm whether the fingerprint information of the GitHub key really comes from the GitHub server, and yespress Enter.

Git will output a warning telling you that the GitHub Key has been added to a trust list on this machine:

Warning: Permanently added 'github.com' (RSA) to the list of known hosts.

This warning will only appear once, and there will be no warning for subsequent operations.

If you are really worried about someone posing as a GitHub server, yesyou can check whether the fingerprint information of the GitHub RSA Key is the same as that given by the SSH connection before input .

summary

To associate a remote library, use the command git remote add origin git@server-name:path/repo-name.git;

After the association, use the command to git push -u origin masterpush all the contents of the master branch for the first time;

After that, after each local submission, as long as necessary, you can use the command to git push origin masterpush the latest changes;

One of the biggest benefits of the distributed version system is that working locally does not need to consider the existence of remote libraries, that is, it can work normally with or without Internet, and SVN refuses to work when there is no Internet! When there is a network, it is very convenient to push the local submission and then complete the synchronization!

Published 17 original articles · Like1 · Visits 819

Guess you like

Origin blog.csdn.net/weixin_45433031/article/details/105031875