Git remote repository Add and Clone 1.Git remote repository
With a few previous introductions to git, we've learned how to make a time shuttle to a file in a git repository, and you don't have to worry about file backups or missing issues anymore. 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.
Indeed, git and svn are no different if you're just managing file history in a warehouse. In order to ensure that you now learn git value, will not regret in the future, and in order to combat the unfortunate learning of SVN's children's shoes, from this chapter began to introduce the killer of Git one of the features (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 yourself before continuing to read the following GitHub account number. There's a lot of information on how to sign up for GitHub online, so here's not the introduction.
Because the transfer between your local git repository and the GitHub repository is encrypted via SSH, SSH Key needs to be created.
1.1 Creating an SSH Key
In the current Git 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 you 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. As shown in the Git command,
If all goes well, you can find the . SSH directory in the directory you created, with Id_rsa and id_rsa.pub two files,
These two are the key pair of SSH key,Id_rsa is the private key, can not be leaked out,id_rsa.pub is the public key, it is safe to tell anyone.
1.2 Landing on 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. "With a remote repository, there's no need to worry about hard drives anymore."
2. 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 "Createa new Repo" button to create a new warehouse:
After Repositoryname fills in the mygittest, synchronizes with the local, other remains the default setting, clicks the "Create Repository" button, has succeeded in creating a new Git repository:
At the moment, this mygittest 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 Mygittest repository, based on GitHub's prompt:
$ git Remote add origin [email protected]/kevinfa2016/mygittest.git
Please be careful to replace the above Kevinfa2016 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/f/git/mygittest
Pushing the contents of the local library to the remote, with the git push command, actually pushes the current branch master (referred to as f/git/mygittest, below) to the remote.
Since the remote library is empty, the first time we push the master branch, with the- u parameter, git will not only push the local master branch content to the remote new master branch, It also associates the local Master branch with the remote Master branch and simplifies 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/f/gitmywarehouse Master
By pushing the latest changes to the local master branch to GitHub, you now have a truly distributed repository!
When you first connect to GitHub using Git's clone or push command, 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.
Is you sure 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 Yes .
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.
If you're really worried about someone impersonating a github server, enter Yes before you can control whether the fingerprint information of GitHub's rsakey is consistent with the SSH connection.
Summary
To associate a remote library, use the command git remoteadd origin [email protected]:p ath/repo-name.git;
Once associated, use the command git push-uorigin 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 initializethis repository with a READMEso 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]:kevinfa2016/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 useHttps://github.com/michaelliao/gitskills.gitSuch an address. In fact, GIT supports multiple protocols, the defaultgit://Use SSH, but you can also use theHTTPSand other agreements. UseHTTPSIn addition to the slow speed, the biggest trouble is that each push must enter a password, but in some only open the HTTP port of the company can not useSSHProtocol and can only be usedHTTPS。
Summary
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 remote repository additions and clones