Git Tutorials Remote Repository (9)

Source: Internet
Author: User

There is a magic site called GitHub, hehe, from the name can be seen, this site is to provide a git warehouse hosting services, so as long as the registration of a GitHub account, you can get free git Remote Storage.

Because the transfer between the local git repository and the GitHub repository is encrypted via SSH, a bit of setup is required:
1th step: Create SSH Key. In the user home directory, see if there is no. ssh directory, if there is, then look at this directory there are no Id_rsa and id_rsa.pub these two files, if already have, can jump directly to the next step. If not, open the shell (open git Bash under Windows) and create SSH Key:

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.

2nd step: Login to GitHub, open "Account Settings", "SSH Keys" page:
Then, click "Add SSH Key", fill in any title, paste the contents of the Id_rsa.pub file in the Key text box:

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

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.

Now the scenario is that you've created a git repository locally (a local folder), you want to create a git repository on GitHub , and have the two warehouses remotely synchronized so that the repository on GitHub can be used as a backup It is also possible for others 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:

In the repository name fill in the learngit (the name of the local warehouse), the other remains the default settings, click the "Create Repository" button, the successful creation of a new Git repository:

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.

Once added, the remote library's name is origin, which is the default term for git, or it can be changed to something else, but this name is known as a remote library.

Next, you can push all the contents of the local library to the remote library:

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!

(

To associate a remote library, use the command git remote add Origin git@server-name:path/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

)

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:

Then go to the Gitskills directory to see, there are already readme.md files. Then go to the Gitskills directory to see, there are already 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 git@github.com:winner0715/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.

Git Tutorials Remote Repository (9)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.