Git Easy guide-remote repository

Source: Internet
Author: User

Introduction to remote Warehouses

So far, we've mastered how to navigate a file in a git repository, and you'll never have to worry about file backups or missing issues.

But there's a centralized version control system. SVN's children's shoes will stand out and say that these features have been in SVN for a long time, and do not see anything special about Git.

Yes, git and svn are no different if you're just managing file history in a warehouse. In order to ensure that you are now learning git value, will never regret in the future, and in order to combat the unfortunate learning of SVN's children's shoes, this chapter began to introduce one of the killer features of Git (note is one, that is, there are two later, three ...). ): Remote warehouse.

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:

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, you can skip to the next step. If not, open the shell (open git Bash under Windows) and create SSH Key:

" [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 directory in the user's home directory .ssh , there are id_rsa and id_rsa.pub two files, these two are SSH key key pair, is the private key, can id_rsa not be leaked out, id_rsa.pub is the public key, it is safe 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 file in the Key text box id_rsa.pub :

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.

To 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:

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

At the moment, this warehouse on GitHub learngit 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, we associate the local repository with this GitHub remote repository and take an alias origin for this remote repository. Run the command in the local learngit warehouse:

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

Please be careful to replace the above with michaelliao 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.

Add, later use this remote library, you can not write the specific address, the direct use of origin can be replaced, in fact, this is a very long connection to take a good remember name. This is the default name for Git, but it can be changed to something else, but origin this is a long-range repository.

You can use the following instructions to view all the remote libraries that are already associated (a local library can be associated with a lot of remote libraries), and you can see the aliases for each remote library:

$git remote-v

Note: A local library can be associated with many remote libraries, and for simplicity, aliases are used instead of that long link. However, this alias and the remote Library is one by one corresponding, can not be an alias for multiple remote libraries.

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

$ git push-u Origin mastercounting objects: +, done. Delta CompressionusingUp to4threads.compressing objects: -% ( +/ +), done. Writing objects: -% ( +/ +),13.73KiB, done. Total at(Delta6), reused0(Delta0) to [email Protected]:michaelliao/Learngit.git* [NewBranch] Master,Masterbranch MasterSetUp-to-track remote branch master fromOrigin.

Pushing the contents of the local library to the remote, with git push the command, is actually pushing the current branch master to the remote.

Since the remote library is empty, the first time we push master a branch, with -u parameters, Git will not only master push the local branch content of the remote new master branch, but also the local master branch and the Remote master Branch Association, 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

Push the master latest changes from your local branch to GitHub, and now you have a truly distributed repository!

Summary

To associate a remote library, use the command git remote add origin [email protected]:path/repo-name.git ;

Once associated, use the command to git push -u origin master push all the contents of the master branch for the first time;

Thereafter, after each local submission, whenever necessary, you can use the command to git push origin master 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 file for us README.md . Once created, you can see the README.md file:

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

$ git clone [email protected]:michaelliao/'gitskills'3   3000(3/3), done . $ cd gitskills$ lsreadme.md

Be careful to replace the GIT repository's address with your own, then go to the gitskills directory and look at the README.md file.

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 such an https://github.com/michaelliao/gitskills.git address. In fact, GIT supports a variety of protocols, git:// using SSH by default, but can also use https other protocols.

With the https exception of slow speed, one of the biggest problems is that each push must enter a password, but in some companies that only open HTTP ports, the protocol cannot be used ssh https .

Summary

To clone a warehouse, you must first know the address of the warehouse and then use the git clone command clone. Cloning This behavior can be done for anyone's remote library, and then after you modify it, push it to the remote library of your own association.

GIT supports a variety of protocols, including https , but with the fastest ssh supported native git protocols.

Learning Corner: http://www.liaoxuefeng.com/

Git Easy guide-remote repository

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.