What is the difference between 'git pull' and 'git fetch'?

TLDR;

git fetch is similar to pull but doesn't automatically merge; it fetches remote updates, but your local stays the same.

git pull pulls down from a remote and instantly merges with your local.

Fundamentals

If you're new to Git, you're probably confused by the difference between git pull and git fetch. They seem to do similar things but aren't exactly the same.

Git is a distributed version control system. Your local repository is identical to any other remote Git repository, including those found on GitHub and your coworker's machine.

Remote repositories are referenced in your local repository as remote tracking-branches that are linked to there upstream remotes source. To make sure they accurately represent the state of the remote repository, you are not able to modify (move) them directly, Git will do this for you.

To apply changes from a remote repository to your own local working copy, you need first to update your local remote-tracking branch and then apply the changes to your local working branch. To do this, you need to use the git merge, or the git rebase command.

This is particularly important as changes from a remote may break your local working branch, and my need to be applied with your input.

git fetch

git fetch brings your local remote-tracking branches up-to-date with its remote version.

The git fetch operation is safe to run at any time since it never changes any of your local working branches. You will need to use git merge or git rebase to do this.

git pull

git pull brings a local remote-tracking branch up-to-date with its remote version while also attempting to automatically update your own local working branch.

git pull automatically merges the commits without letting you review them first. You may run into frequent conflicts and issues if you are not careful.

Why shouldn't you use git pull?

git pull isn't bad if used properly. The pull command combines two commands, git fetch and git merge. This is safe if your local branch is in sync with the remote branch. If it's not, git pull can modify your local working branch in unpredictable ways and introduces unnecessary nonlinearities in the history.

It's generally preferred to use git fetch followed by git rebase. Git fetch beings down the local changes without applying them to your working branch and git rebase re-writes the history by creating brand new commits for each commit in the local working branch and allows you to apply changes safely.

You've successfully subscribed to Twisted Brackets
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.