14020

What are the differences between git pull and git fetch?

7
  • 448
    I found this well written article about git fetch and git pull it's worth the reading: longair.net/blog/2009/04/16/git-fetch-and-merge Commented Sep 16, 2010 at 6:57
  • 75
    Our alternative approach has become git fetch; git reset --hard origin/master as 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 ! Commented May 4, 2014 at 14:32
  • 43
    Make sure you know how to use git stash correctly. If you're asking about 'pull' and 'fetch' then maybe 'stash' will also need explaining... Commented Dec 9, 2014 at 20:09
  • 52
    Lots of folks coming from Mercurial keep using "git pull", thinking it's an equivalent for "hg pull". Which it's not. Git's equivalent of "hg pull" is "git fetch". Commented Jun 29, 2015 at 10:15
  • 2
    Slightly off question, but I have developed the habit of entering "git fetch && git status" instead of "git status". By itself "git status" reports the last remote repository status seen. This can report stale information about the remote repository. For example, "git status" will report "Your branch is up to date" despite a new commit having been pushed. Commented Jan 16, 2024 at 19:04

37 Answers 37

1
2
33
git pull = git fetch + git merge 
Sign up to request clarification or add additional context in comments.

Comments

28

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

25

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

21

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 fetch command 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 pull just 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

11

Simple explanation:

git fetch 

fetches the metadata. If you want to check out a recently created branch you may want to do a fetch before checkout.

git pull 

Fetches the metadata from remote and also moved the files from remote and merge on to the branch

Comments

8

This graphic could be of help. git pull is essentially equivalent to git fetch then git merge

This graphic could be of help. git pull is essentially equivalent to git fetch then git merge

3 Comments

git merge does not only affect your working copy...
@masterxilo Yes it does. git merge is a local operation that only affects your working copy.
no, in general it makes commits and thus affects your repository not just the working copy; from git-merge docs: "if you want to ensure your branch is not changed or updated by the merge command, use --no-ff with --no-commit"
7

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

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.