+11 votes
1 view
in Devops and Agile by (20k points)
edited by

What are the differences between git pull and git fetch?

2 Answers

+11 votes
by (28.4k points)
edited by

The command 'git pull' pulls any new commits from a branch from the central repository and then updates the target branch in the local repository.

But 'git fetch' is used for slightly different form 'git pull'. Unlike 'git pull', it pulls all new commits from the desired branch and then stores them in a new branch in the local repository. In order to reflect these changes in your target branch, 'git fetch' must be followed with a 'git merge'. The target branch will only be updated after merging with the fetched branch (where we performed 'git fetch'). We can also interpret the whole thing into an equation like this,

git pull = git fetch + git merge

To know more about git here is a tutorial  of  git that helps you:

0 votes
by (32.2k points)

Simply, we can say that git pull does a git fetch followed by a git merge.

You can freely perform git fetch at any time to update your remote-tracking branches under refs/remotes/<remote>/.

This operation never changes any of your local branches under refs/heads and is safe to do without changing your working copy. I have even heard of people running git fetch periodically in a cron job in the background (although I wouldn't recommend doing this).

In order to bring a local branch up-to-date with its remote version, you need to perform a git pull. Simultaneously, it will also update your other remote-tracking branches.

Git documentation – git pull:

P.S. - In its default mode, git pull is shorthand for git fetches followed by git merge FETCH_HEAD.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !