Data report"State of code review 2024" is now liveRead the full report

Understanding the differences between git pull and git fetch

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Both Git commands git pull and git fetch are used to update local code from a remote repository, but they serve different purposes and affect your local repository in different ways. This guide explores the differences between git pull and git fetch, explaining when to use each command and highlighting their impacts on your workflow.

git fetch is a command used to download commits, files, and refs from a remote repository into your local repo. Fetching is great for seeing what others have done without merging those changes into your own branch.

The command:

  • Updates remote-tracking branches: git fetch updates your remote-tracking branches (e.g., origin/main). These branches do not affect your local branches and provide a history of what has happened in the repository.
  • Keeps local changes safe: Fetching does not change your current branch or modify your working directory. This makes it safe to see other branches' updates without altering your local development.

To fetch the latest updates from your remote repository, run:

Terminal
git fetch origin

This command fetches all the new data from the remote repository pointed to origin (the default name for your remote repository) but doesn't merge any of this new data into your working files.

Git pull, in contrast, not only fetches updates from the remote repository but also automatically merges them into your current branch.

The git pull command:

  • Combines fetch and merge: Pulling changes will fetch the latest data and then immediately attempt to merge those changes into the branch you are currently working on.
  • Can potentially lead to merge conflicts: If the changes fetched conflict with the changes you've made locally, git pull will pause and ask you to resolve those conflicts manually.

To update your current branch with the latest commits from the remote repository, use:

Terminal
git pull origin main

This command fetches and merges the remote main branch into your local main branch.

  • Use git fetch when:

    • You want to see the changes made to the remote repository without merging them into your local branch.
    • You need to review or compare the changes before integrating them into your branch.
    • You are working in a sensitive or unstable branch and want to avoid unintended disruptions.
  • Use git pull when:

    • You are ready to merge remote changes and are confident it won't disrupt your local development.
    • You want to quickly synchronize your branch with the latest updates from the remote.

One effective workflow involves using git fetch to review changes before performing a git pull. This can be particularly useful in a team environment where understanding changes before merging can prevent conflicts and misunderstandings.

  1. First, fetch the changes:

    Terminal
    git fetch origin
  2. Compare the fetched changes to your local branch:

    Terminal
    git log --oneline --graph --decorate --all
  3. If you decide the changes are safe to merge, then execute a pull:

    Terminal
    git pull origin main

For further reading, see the official Git documentation on git pull and git fetch.

Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2