Git remote repository

Source: Internet
Author: User

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, a computer is also possible, as long as not in the same directory can be, but there is no meaning.

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:

$ 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 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.

Add a remote repository

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, let's run the command under the local repository, based on GitHub's tips learngit :

$ 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.

After adding, the name of the remote library is origin , this is the default for Git, or can be changed to another, but origin this name is known as a remote library.

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

$ git push-u origin mastercounting objects:19, done. Delta compression using up to 4 threads.compressing objects:100% (19/19), done. Writing objects:100% (19/19), 13.73 KiB, done. Total (delta 6), reused 0 (Delta 0) to [email protected]:michaelliao/learngit.git * [New branch] Master-,      Master Branch Master set up to track remote Branch master from Origin.

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!

SSH warning

The first time you use Git clone or push a command to connect to GitHub, you 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 do sure you want to continue connecting (yes/no)?

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 the yes return.

Git will output a warning telling you that you have added GitHub key 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 after the operation.

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.gitcloning into ' gitskills ' ... remote:counting objects:3, Done.remote:Total 3 (Delta 0), reused 0 (Delta 0) receiving objects:100% (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 .

Git 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.