Previous operations We just took git as a repository, a local version management, which is overkill for Git. Git as a distributed version control system, distribution is its characteristics, how to distribute it. Must have a machine to act as the original repository, the other machine "clone" the original version of the library, in fact, each machine version is the same, no primary and secondary points, the reason for the original version of the library, is to facilitate the collaboration of many people have a benchmark, so that the version of the machine is consistent. In general, the original repository will not be with the computer we work on, it is remote for us, so we can call it a remote repository.
Since it is remote, it must also need a machine to act as a server (original repository), and this machine needs to be guaranteed to boot 24 hours, so that other users can clone the original version at any time, push to submit to the server, pull someone else's submission. We are just to learn git, deliberately find a server can not be, fortunately, big git status detached, many websites have provided git warehouse hosting services, such as the famous github.com at home and abroad, the domestic famous git.oschina.net. While both are able to provide hosting services, GitHub is free to build only Gangcan (visible to all users), private (only for their own visibility) is to pay, and Oschina public and private positions do not need to pay, and is the domestic, it should be supported, so the small part of the choice of Oschina to establish a remote warehouse.
About the Oschina application account is not repeated here, assuming you already have an account and login, then we can build a remote repository. Creating a remote repository on Oschina is simple, we click on the new project, we must fill in the project name, others can choose according to your needs, there is a point to be prompted, if you already have a git project you want to push directly to the remote repository, do not let the remote warehouse initialization, That is, do not tick "initialize this project with a Readme file", if you do not want others to see your project, tick "private project".
We succeeded in building an empty remote repository with nothing in it, so we gave our friendly tips and told us what to do.
Although they are really very attentive, but not all-encompassing, it is OK, from the small part to supplement their shortcomings. Different between the two computers need to carry out data transmission, definitely need a bridge, Oschina for us to provide two options, one is HTTPS, one is ssh. If you use HTTPS, you do not have to do anything special, the above tutorial is enough to allow you to successfully push local projects to the remote warehouse, but each push will need to enter the account number and password (Oschina account password), very troublesome, so we can choose another way, SSH.
SSH is required to create SSH Keys, many people do not know how to create, it's okay, Oschina thought of this, specifically to the tutorial.
Linux followed the tutorial to walk on the line, below we look at windows. We use the GIT bash terminal and then enter the same
Ssh-keygen-t rsa-c "Zhangsan@gmial.com"
Then return to the car. In "C:\Documents and Settings\administrator\.ssh" There is the file id_rsa.pub we need. Then open Oschina add SSH Public key place, click here, if you have already landed. Then enter a title, copy the contents of the id_rsa.pub you just generated to the public key, then click OK, so the preparation is done.
Then we can communicate the local and remote positions via SSH. Add a remote repository
git remote add osc-git git@git.oschina.net:xxxxxxx/git.git
where Git remote is to tell git to execute remote repository related commands, add is added, Osc-git is the remote warehouse name (many tutorials use origin as the remote repository name, not necessary, can be modified as appropriate), to simplify the writing of remote warehouse address, In the future remote related operations only need to use the remote warehouse name, git@ ... is the actual SSH address of our remote repository, so we have successfully added a remote repository.
View Remote repositories
git remote //output repository name
git remote-v// output remote repository name and address
Delete a remote repository
Git remote RM osc-git
push to remote warehouseThe git push command is used to push updates from the local branch to the remote branch.
git push < remote repository name > < local branch name >:< Remote Branch name >
For example, we push the local master branch to the master branch of the remote repository to write
git push osc-git Master:master
Precautions1. When we push, we must pay attention to the local branch and the location of the remote branch, do not be mistaken. 2. The local branch for push must exist, otherwise it will error, the remote branch can not exist, when the remote branch name does not exist, it will be new.
Of course, we can also shorthand for
git push osc-git Master
Where master represents the local branch, it also achieves the effect of pushing the local master branch to the remote Master branch. With: When we can push branches with different names, such as Git push osc-git master:dev, we push the local master branch to the remote dev branch. Without: We can only use to push the local master branch to the remote Master branch. We recommend pushing between branches with the same name, so we often use shorthand.
Sometimes we'll see in the tutorial that there is a-u parameter in front of the remote warehouse name, for example
git push-u osc-git Master
Plus-U with no-u what's the difference?
Difference One:Plus-U, we find one more line in git status, "Your branch is up-to-date with ' osc-git/master '", what's the use of this. To illustrate its role, we use git push-u osc-git master push after creating a new file, then git add and git commit, when the local warehouse is more than the remote warehouse a commit. And then we git status again, what you see, what you see, right is the hint, git hints that the branch of our local warehouse is a more advanced submission than ' Osc-git/master ', which is to tell us that the local and remote silos are out of sync and we should git push. Without-u, you don't get any hints when you use git status. You can see that the role of-U is to associate the local branch with the remote branch, of course, this operation only once, and then git push does not have to add-u, because git already know the relationship between the two branches. Do you think git is too friendly, of course, this kind of friendliness is still a bit of a flaw, because it is one-way, only if the local warehouse submission is beyond the remote warehouse submission, git status is prompt, but the remote warehouse submission beyond the local warehouse submission, git status does not have any hint, This must be noted.
Difference Two:In a moment we'll talk about the git pull command, which is the command for pulling the contents of the remote branch, before using-u, we have to use the full command git pull Osc-git master to fetch the remote branch content, the direct use of Git pulls will be error, but after using-u, We can pull directly from the remote master branch with Git pull, and of course only the remote Master branch (just take the master branch for example, the actual branch that can be pulled is the remote branch that you are bound to when you use-u).
The first time you push, you get a warning because git uses SSH connections, and when SSH connects to the Oschina server key for the first time, it needs to make sure that the Oschina key is really from Oschina and enter Yes. Git then outputs a warning that you've added the Oschina key to the native trust list, and the next time you connect, there's no warning.
The authenticity of host ' git.oschina.net (xxx.xxx.xxx.xxx) ' can ' t be established.
ECDSA key fingerprint is sha256:xxxxxxxxx.
Is you sure want to continue connecting (yes/no)? Yes
warning:permanently added ' git.oschina.net,xxx.xxx.xxx.xxx ' (ECDSA) to the list of known hosts.
At this point we look at the remote repository, the original empty warehouse is not already have data, and with our local repository directory structure is consistent, so we completed the first git push.
cloning from a remote repositoryJust now we are assuming that the remote warehouse is empty, the local repository via git push to the remote repository, if the remote repository has content, we need to pull to the local how to do it. In fact, it's easy to do it with Git clone.
git clone git@git.oschina.net:xxxxxxxx/git.git
One thing to note is that using Git clone pulls a folder with the project name, such as the Git folder, which is not directly pulling the files and folders under the project, so you need to be careful when choosing a local warehouse location.
pull from remote repositoryWhen a remote repository branch exceeds the local branch's commit, git push cannot be pushed, and we need to pull the remote branch content first and then merge with the local branch to git push.
method One: git fetch+git merge
git fetch osc-git master
git merge Osc-git/master
Git fetch is used to get the branch content of the remote repository locally, and the git merge is used to merge the remote branch content into the local branch, when the remote branch synchronizes with the local branch.
method Two: Git pull
Git pull Osc-git Master
Git pull is a combination of git fetch and git merge two commands, just one command to complete the above two steps, if you use Git push to bind a branch, such as Git push-u osc-git master, At this point, even the Osc-git master can be omitted, we only need git pull can be pulled to the remote repository Osc-git Master Branch, of course, the notation can only pull the default associated branch, the above example can only pull the master branch of Osc-git. Although convenient, but there are drawbacks, especially in the case of multi-branch. For example, there are now two branches locally, a master branch, a dev branch, and the Osc-git/master and Osc-git/dev for the remote repository, and we wanted to pull Osc-git/dev to the Dev branch, but we didn't notice that we're using the master branch. Directly using git pull osc-git Dev, the cup has, git directly merges master and Osc-git/dev, which is not what we want, found the bug in time to make up, git reset--hard xxxxxx, Well, master finally returned to its original state. While there is a remedy, it also tells us about the potential pitfalls of git pull, so we should avoid using git pull and try to use git fetch and git merge, although this may still make the mistake, but the odds will be greatly reduced.