Source: http://blog.tsnrose.com/2012-04-18-git-fetch.html (Thanks to the author for sharing)
Sometimes we need to get other people's code repositories, to merge others (not push to the remote repository), to incorporate their own modifications, or to look at the code under a branch of someone else (without actually switching to someone else's branch), this article describes how to do it.
Git remote
Git remote is used to manage the local working directory corresponding to the long-range code warehouse, in the normal working directory, the implementation of GIT remote results are as follows:
> git remoteorigin> git remote -vorigin [email protected] (fetch)origin [email protected] (push)
We can use the GIT remote add command to add a remote repository that can be an SSH address (such as the one above), a local directory, or an address to the GIT protocol or HTTP protocol.
For example, I'm going to use Liming's repository as one of my remote repositories, and I can execute git remote add to add a repository, for example:
> git remote add liming /home/liming/repo> git remotelimingorigin> git remote -vliming /home/liming/repo (fetch)liming /home/liming/repo (push)origin [email protected] (fetch)origin [email protected] (push)
This will/home/liming/repo as one of my remote repositories.
As appropriate, you can use git remote rm or git remote Rename to modify the name of the repository (local, without affecting the other's directory)
git fetch
Git fetch is used to get content that is not available locally in the remote code repository, and Git fetch, for example:
20, done.remote: Compressing objects: 100% (9/9), done.remote: Total 11 (delta 7), reused 4 (delta 2)Unpacking objects: 100% (11/11), done.From /home/liming/repo * [new branch] card -> liming/card * [new branch] master -> liming/master * [new branch] test1 -> liming/test1 * [new branch] ziti -> liming/ziti
The code that is being developed is then crawled to the local.
Git checkout
You can use git checkout to switch to other people's code branches, git checkout/, for example:
> git checkout liming/cardNote: checking out ‘liming/card‘.You are in ‘detached HEAD‘ state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_nameHEAD is now at 20831f6... 自动充值
This will allow you to see the code that has been commit.
Git checkout-b
Checkout to someone else's branch, in detached head state, that is, the commit will be discarded. To make changes based on someone else's code, you can create a new local branch, for example:
# (在liming/card分支中)> git checkout -b newcardSwitched to a new branch ‘newcard‘
This creates a local branch called Newcard.
Modify the Gitconfig file
With the local branch, you can modify and commit on the branch, and for other people's changes, you can use:
# (在newcard分支中)git fetch liminggit merge liming/master
Merge with local code, but this will run two commands at a time. In fact, often run git pull is the same as the above two commands, as long as in the configuration file set up, you can let Git get help us:
In. Gitconfig [branch "Newcard"] segment (add it Yourself):
remote = limingmerge = refs/heads/master
This way, you can use git pull to get remote code after each remote update.
git push
After the local branch has been modified and committed, these changes can be committed to the remote branch, in the form of git push:, SRC stands for the local branch, and DST represents the remote branch, for example:
$git push liming newcard:card
This allows the local newcard changes to be submitted to the card branch under Liming. Note: Only the local merge remote branch has the latest code, and the other party is not under this branch to operate successfully.
git Fetch pull Others branch (GO)