For basic git usage please read the previous post, "versioning" git Learning Notes (i).
1. Remote repository git is a distributed version control system, and the same git repository can be distributed across different machines. How to distribute it? At first, there must be only one machine with an original repository, and since then, other machines can "clone" the original repository, and each machine's repository is actually the same, and there is no primary and secondary points.
You'd think that at least two machines would be needed to play the remote library. But I only have one computer, how to play?
In fact, you can clone multiple repositories on a single computer, as long as they are not in the same directory. However, in real life is not so silly to make a few remote library on a computer to play, because a computer to make a few remote library completely meaningless, and the hard drive hangs will cause all the library hangs, so I do not tell you how to clone multiple warehouses on a computer.
This is often the case, find a computer to act as a server role, 24 hours a day to power on, everyone else from the "server" to clone a copy to their own computer, and their respective submissions to the server warehouse, but also from the server warehouse to pull someone else's submission.
You can build a server that runs git on your own, but at this stage, it's definitely a fuss to learn to git a server first. Fortunately, this world has a fantastic website called GitHub, from the name can be seen, this site is to provide git warehouse hosting services, so as long as you register a github account, you can get free git Remote Storage.
Please register your GitHub account before continuing to read the following. Because the transfer between your local git repository and the GitHub repository is encrypted via SSH, a bit of setup is required:
Create an SSH Key. In the user's home directory, see if there is a. SSH directory, if there is, then look at this directory there are no Id_rsa and id_rsa.pub these two files.
If you already have one, you can jump right to the next step. If not, open the 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 return to the default value, as this key is not used for military purposes, so there is no need to set a password.
If all goes well, you can find the. SSH directory in the user's home directory, there are Id_rsa and id_rsa.pub two files, these two are SSH key key pair, Id_rsa is the private key, can not be leaked out, Id_rsa.pub is the public key, can be assured to tell anyone.
Log on to GitHub and open the "Account Settings", "SSH Keys" page:
Show SSH options:
Click New SSH Key:
Enter title and
Finally click on Add to do it!
Why does GitHub need SSH key? Because GitHub needs to recognize that your push submission is actually pushed by you, not someone else impersonating it, and Git supports the SSH protocol, so if GitHub knows your public key, you can confirm that only you can push it.
Of course, GitHub allows you to add multiple keys. Assuming you have a number of computers, you can submit them at the company in a moment, and at home, just add each key to GitHub and push it to GitHub on every computer.
Finally, friends can see the Git repository, which is hosted on GitHub for free (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 to do it, one for the intersection protection fee, and for GitHub to turn the public repository into private so that others can't see it (unreadable and not writable). Another way is to do it yourself, take a git server, because it is your own git server, so others are invisible. This method we will talk about later, quite simple, the company internal development necessary.
Make sure you have a GitHub account and we're about to start learning from remote repositories. 2. Add a remote Library
Now, after you've created a git repository locally, you want to create a git repository on GitHub and synchronize the two warehouses remotely, so that the repository on GitHub can be used as a backup and other people to collaborate through the warehouse.
First, log on to GitHub, and then, in the upper right corner, find the "Create a new Repo" button to create a new warehouse:
At the moment, this learngit warehouse on GitHub is still empty, and GitHub tells us that we can clone a new repository from this repository, or associate an existing local repository with it, and then push the contents of the local repository to the GitHub repository. Now, let's run the command under the local Learngit repository, based on GitHub's prompt:
$ git Remote add origin [email protected]:hanxiaofeng/learngit.git
Note: Learngit is the name of the remote library you created
Please be careful to replace the above Hanxiaofeng with your own GitHub account name, otherwise, you are locally associated with my Remote library, the association is not a problem, but you later push is not pushed up, because your SSH key public key is not in my account list.
Once added, the name of the remote Library is origin, which is the default term for git, or it can be changed to something else, but the name of origin is known as the remote repository.
Next, you can push all the contents of the local library to the remote library:
$ Git push-u Origin Master
Pushing the contents of the local library to the remote, using the git push command, is actually pushing the current branch master to the remote.
Since the remote library is empty, when we first push the master branch, with the-u parameter, git will not only push the local master branch content to the remote new Master branch, but also associate the local master branch with the remote Master branch. You can simplify the command at a later push or pull.
Once the push is successful, you can see the remote library's content on the GitHub page immediately as if it were local:
From now on, as long as the local commits, you can pass the command:
$ GIT push origin master
By pushing the latest changes to the local master branch to GitHub, you now have a truly distributed repository!
SSH warning
When you first connect to GitHub using Git's clone or push command, you get a warning:
This is because Git uses SSH connection, and SSH connection is the first time to verify the GitHub server key, you need to confirm that GitHub key fingerprint information is really from the GitHub server, enter Yes.
Git will output a warning telling you that you have added GitHub key to a trust list on this machine:
This warning will only appear once, and there will be no warning after the operation.
If you are really worried about someone impersonating a github server, enter Yes before you can control whether the fingerprint information of GitHub's RSA key is consistent with the SSH connection.
To associate a remote library, use the command git remote add origin [email protected]:p ath/repo-name.git;
Once associated, use the command Git push-u Origin master to push all the contents of the master branch for the first time;
Thereafter, after each local commit, whenever necessary, you can use the command GIT push Origin master to push the latest changes;
One of the biggest benefits of distributed version systems is that working locally does not have to take into account the existence of remote libraries, which means that there is no Internet connection that works, and SVN refuses to work when it is not connected! When there is a network, and then the local submission push a bit to complete the synchronization, it is very convenient!
3. Cloning from a remote library
The last time we talked about a local library, and then a remote library, how to associate the remote library.
Now, assuming that we are developing from scratch, the best way is to first create a remote library and then clone from the remote library.
First, log on to GitHub and create a new repository named Gitskills:
We tick initialize this repository with a README so GitHub will automatically create a readme.md file for us. Once created, you can see the readme.md file:
Now that the remote library is ready, the next step is to clone a local library with the command git clone:
$ git clone [email protected]:hanxiaofeng/gitskills.git
Be careful to replace the GIT repository's address with your own, then go to the Gitskills directory and see that there are readme.md files.
If there is more than one person collaborating on the development, then each individual from the remote clone a copy can be.
You may also notice that GitHub gives more than one address and can also use an address such as Https://github.com/hanxiaofeng/gitskills.git. In fact, GIT supports multiple protocols, and the default git://uses SSH, but other protocols such as HTTPS can also be used.
In addition to the slow use of HTTPS, the biggest trouble is that each push must enter a password, but in some only open the HTTP port of the company can not use the SSH protocol and can only use HTTPS.
To clone a warehouse, you must first know the address of the warehouse and then clone it using the git clone command.
GIT supports multiple protocols, including HTTPS, but the native GIT protocol supported by SSH is the fastest.
Reference link: GitHub remote repository, add remote library, clone from remote library
Version control Git learning notes (ii)