Pulling is analogous to pushing but the data flow is the other way around. This is where you would take changes that have happened on the remote repo and introduce them to your local repo. The command to do this is
git pull. It is actually an aggregate command in that it calls several other commands in sequence but it is a handy and reliable tool. Essentially git pull will do two things:
- Update all of the remote tracking branches for the remote repo you are pulling from
- Examine the branch you currently have checked out and if it has a remote tracking branch, merge in the remote tracking branch
The second point is fairly obvious and the resultant behaviour is what you might expect from
svn update, the first part however isn’t necessarily obvious and happens behind the scenes. You will often notice that git is updating a lot of remote tracking branches that you aren’t working on when you do a git pull. This is fine, dont be alarmed! It sometimes looks like git is updating the wrong branch or has merged a different branch into yours, but it is just updating the tracking branches in order that the repo stay as up to date as possible.
As we’ve learned, if you check out a branch called ‘bigfeature’ then git will create a local branch based off the remote-tracking branch. If you then leave this branch for several weeks and work on something else then when you come to next work on bigfeature you might notice git telling you that you are ‘behind’ by several commits. All it is doing is comparing your local branch with the remote tracking branch. If there have been a lot of commits to the bigfeature branch by other people during the last few weeks then git wont have updated your local branch because you havent had it checked out. The graph would look like this:
If git is telling you you are behind then a
git pull is all that is required to fix it. After a git pull in the above case your local branch pointer would just be fast-forwarded along to meet the remote tracking branch.
So the in summary your general git workflow will look something like this:
- clone a repo from a remote source (usually github)
- create a new branch or checkout an existing one
- do some work
- make one or many commits
- during your work you can pull updates from the remote repo
- when your work is ready, push it to the remote repo
http://eagain.net/articles/git-for-computer-scientists/ – Excellent references that explains how git works under the hood.
http://www.eecs.harvard.edu/~cduan/technical/git/ – Decent tutorial that takes you through most of the concepts involved with git.
http://gitref.org/ – Has quite a comprehensive guide to all the git commands and how to use them in practice