Remote warehouse $ git remote add origin https://github.com/wu347771769/learngit.git

Original link : https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001374385852170d9c7adf13c30429b9660d0eb689dd43a000

So far, we have mastered how to time-travel a file in the Git repository, and you don't have to worry about file backup or loss anymore.

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

That's right, if you just manage the file history in a warehouse, Git and SVN are really the same. In order to ensure that the Git you are learning now is worth the price, you will never regret it in the future. At the same time, in order to combat children who have unfortunately learned SVN, this chapter begins to introduce one of the killer features of Git (note that it is one of the There are two, three......): remote warehouse.

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

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 version libraries can be cloned on one computer, as long as they are not in the same directory. However, in real life, no one would be so stupid to play with several remote libraries on a computer, because it is completely meaningless to have several remote libraries on a computer, and a hard disk hung up will cause all libraries to hang up. So I won't tell you how to clone multiple warehouses 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. , Also pull other people's submissions from the server warehouse.

You can build a server running Git by yourself, but at this stage, setting up a server first in order to learn Git is definitely a fuss. Fortunately, there is a magical website called GitHub in this world. It can be seen from the name that this website provides Git warehouse hosting services. Therefore, as long as you register a GitHub account, you can get a Git remote warehouse for free.

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

Step 1: Create SSH Key. In the user's home directory, see if there is a .ssh directory, if so, then see if there are id_rsaand id_rsa.pubthese two files in this directory. If there are already, you can skip 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, then press Enter all the way and 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 content of the file in the Key text box :

Click "Add Key" and you should see the added Key:

 

Why does GitHub need SSH Key? Because GitHub needs to recognize that the commit you pushed was indeed pushed by you, not by someone else, 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. Assuming that you have several computers, you submit it at the company for a while, and submit it at home for a while. As long as you add the Key of each computer to GitHub, you can push to GitHub on each computer.

Finally, a friendly reminder, the Git repository hosted for free on GitHub can be viewed by anyone (but only you can change it). So, don't 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 and let GitHub make the public repository private, so that others will not see it (unreadable and unwritable). Another way is to do it yourself and set up a Git server. Because it is your own Git server, it is invisible to others. This method, which we will talk about later, is quite simple and is a must for internal company development.

After ensuring that you have a GitHub account, we are about to start learning remote warehouses.

The current situation is that after you have created a Git repository locally, you want to create a Git repository on GitHub and synchronize the two repositories remotely. In this way, the repository on GitHub can be used as a backup or other People collaborate through this warehouse, and it's a multitude of things in one fell swoop.

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

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

At present, the learngitwarehouse on GitHub is still empty. GitHub told us that we can clone a new warehouse from this warehouse, or 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 under the local warehouse:

$ git remote add origin https://github.com/wu347771769/learngit.git

Please be sure to wu347771769replace the above with your own GitHub account name, otherwise, you will associate my remote library locally, there is no problem with the association, but you will not be able to push it up 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 origin. This is the default name of Git, and it can be changed to something else, but originthe name is a remote library at first glance.

In the next step, you can push all the contents of the local library to the remote library. Pay attention to input the username and password of github:

$ git push -u origin master
Username for 'https://github.com': wu347771769
Password for 'https://[email protected]': 
Counting objects: 23, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (23/23), 2.14 KiB | 1.07 MiB/s, done.
Total 23 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), done.
To https://github.com/wu347771769/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 git pushcommands, actually masterpush the current branch to the remote.

Since the remote library is empty, when we push the masterbranch for the first time, we add -uparameters. Git will not only push the local masterbranch content to the remote new masterbranch, but also associate the local masterbranch with the remote masterbranch. In the future You can simplify the command when you push or pull

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

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

$ git push origin master

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

SSH warning

When you connect to GitHub using Git cloneor pushcommands for the first time , 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 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 just press yesEnter.

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 warnings for subsequent operations.

If you are really worried about someone pretending to be a GitHub server, yesyou can check whether the fingerprint information of GitHub's RSA Key is consistent with the SSH connection before entering it.

 

Clone from remote repository:

Last time we talked about how to associate the remote library when there is a local library first and then a remote library.

Now, assuming we develop from scratch, the best way is to create a remote library first, and then clone from the remote library.

First, log in to GitHub and create a new repository named gitskills:

We check Initialize this repository with a READMEit so that GitHub will automatically create a README.mdfile for us . After creation, you can see the README.mdfile:

Now that the remote library is ready, the next step is to git cloneclone a local library with the command :

$ git clone https://github.com/wu347771769/gitskills.git
Cloning into 'gitskills'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.

Pay attention to replace the address of the Git library with your own, and then enter the gitskillsdirectory to see, there are already README.mdfiles:

$ cd gitskills
$ ls
README.md

If there are multiple people developing collaboratively, then each person can clone a copy from a remote location.

You may also notice that there is more than one address given by GitHub, and https://github.com/wu347771769/gitskills.gitsuch an address can also be used . In fact, Git supports multiple protocols, git://ssh is used by default , but httpsother protocols can also be used .

Use httpsin addition to slow, there is the biggest trouble is that each push must enter a password, but some within the company is only open http port will not be able to use sshprotocol and can only use https.

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 commands to git push origin masterpush the latest changes;

To clone a warehouse, you must first know the address of the warehouse, and then use the git clonecommand to clone.

Git supports a variety of protocols, including https, but the fastest through sshthe native gitprotocol supported .

 

 

Guess you like

Origin blog.csdn.net/wu347771769/article/details/88999943