37 Answers
git pull
It performs two functions using a single command.
It fetches all the changes that were made to the remote branch and then merges those changes into your local branch. You can also modify the behaviour of pull by passing --rebase. The difference between merge and rebase can be read here
git fetch
Git fetch does only half the work of git pull. It just brings the remote changes into your local repo but does not apply them onto your branches.You have to explicitly apply those changes. This can be done as follows:
git fetch git rebase origin/master Comments
One must keep in mind the nature of git. You have remotes and your local branches ( not necessarily the same ) . In comparison to other source control systems this can be a bit perplexing.
Usually when you checkout a remote a local copy is created that tracks the remote.
git fetch will work with the remote branch and update your information.
It is actually the case if other SWEs are working one the same branch, and rarely the case in small one dev - one branch - one project scenarios.
Your work on the local branch is still intact. In order to bring the changes to your local branch you have to merge/rebase the changes from the remote branch.
git pull does exactly these two steps ( i.e. --rebase to rebase instead of merge )
If your local history and the remote history have conflicts the you will be forced to do the merge during a git push to publish your changes.
Thus it really depends on the nature of your work environment and experience what to use.
Comments
All branches are stored in .git/refs
All local branches are stored in .git/refs/heads
All remote branches are stored in .git/refs/remotes
The
git fetchcommand downloads commits, files, and refs from a remote repository into your local repo. Fetching is what you do when you want to see what everybody else has been working on.
So when you do git fetch all the files, commits, and refs are downloaded in
this directory .git/refs/remotes
You can switch to these branches to see the changes.
Also, you can merge them if you want.
git pulljust downloads these changes and also merges them to the current branch.
Example
If you want see the work of remote branch dev/jd/feature/auth, you just need to do
git fetch origin dev/jd/feature/auth
to see the changes or work progress do,
git checkout dev/jd/feature/auth
But, If you also want to fetch and merge them in the current branch do,
git pull origin dev/jd/feature/auth
If you do git fetch origin branch_name, it will fetch the branch, now you can switch to this branch you want and see the changes. Your local master or other local branches won't be affected. But git pull origin branch_name will fetch the branch and will also merge to the current branch.
Comments
3 Comments
git merge is a local operation that only affects your working copy.Git Fetch
Helps you to get known about the latest updates from a git repository. Let's say you working in a team using GitFlow, where team working on multiple branches ( features ). With git fetch --all command you can get known about all new branches within repository.
Mostly git fetch is used with git reset. For example you want to revert all your local changes to the current repository state.
git fetch --all // get known about latest updates git reset --hard origin/[branch] // revert to current branch state Git pull
This command update your branch with current repository branch state. Let's continue with GitFlow. Multiple feature branches was merged to develop branch and when you want to develop new features for the project you must go to the develop branch and do a git pull to get the current state of develop branch
Documentation for GitFlow https://gist.github.com/peterdeweese/4251497

git fetch; git reset --hard origin/masteras part of our workflow. It blows away local changes, keeps you up to date with master BUT makes sure you don't just pull in new changes on top on current changes and make a mess. We've used it for a while and it basically feels a lot safer in practice. Just be sure to add/commit/stash any work-in-progress first !