Read Anthropic’s case study about Graphite Reviewer

The difference between git rebase and git pull

Greg Foster
Greg Foster
Graphite software engineer


Note

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


Two common commands used in managing and integrating changes from different branches are git rebase and git pull. This guide will provide an in-depth comparison between these commands, explaining their functions, differences, and when to use each.

Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

Before examining the differences, let's define some key operations:

  • git fetch: This command downloads commits, files, and refs from a remote repository into your local repo. Fetching is a safe way to review changes before integrating them into your local repository.

  • git pull: A git pull operation is essentially a git fetch followed by a git merge. It fetches changes from the remote repository (the branch specified) and then merges them into your current branch.

  • git rebase: Rebase is a way to move or combine a sequence of commits to a new base commit. Git rebase is often used to maintain a cleaner, more linear project history by moving the entire feature branch to sit on top of the main branch.

Now, let's look at how git fetch and rebase differs from git pull:

  1. Workflow clarity and project history

    • Git pull: When you pull changes from a remote branch, Git automatically fetches and merges those changes into your current branch. This merge creates a new "merge commit" in your history, which can sometimes clutter your project history with numerous merge commits, especially in projects with frequent updates.

    Terminal example of git pull:

    Terminal
    git pull origin main

    When you run this command here's what's happening:

When you run the command git pull origin main in the terminal, here's what's happening:

  • origin is the default name Git gives to the remote repository from which you cloned. It's a shorthand alias for the URL of the remote repository.
  • main is the primary branch from which you want to pull the changes. In many Git workflows, main is the central branch where the final source code of your project is kept.

When executed, this command will fetch changes from the main branch of the remote named origin and merge them into the branch that you're currently checked out to in your local repository.

  • Git fetch then rebase: This two-step process gives you more control over the integration of changes. First, git fetch retrieves the updates from the remote without altering your local branch. Then, git rebase repositions your current branch’s commits at the end of the remote branch, creating a linear and cleaner history.

Terminal example of git fetch followed by git rebase:

Terminal
git fetch origin
git rebase origin/main
  1. git fetch origin

    • This command fetches all the changes from the remote repository named origin but doesn't merge any of these changes into your local branches. It updates your local repository's remote tracking branches (like origin/main, origin/feature-branch, etc.). These tracking branches represent the state of the branches in your remote repository at the last fetch.
  2. git rebase origin/main

    • After fetching, this command is used to rebase the current local branch onto origin/main. Rebasing is the process of moving or combining a sequence of commits to a new base commit. It's a way to integrate changes from origin/main into your branch while keeping the history linear and clean.
    • Rebasing re-writes the commit history by creating new commits for each commit in the original branch, which can make the project history easier to understand with fewer merge commits than a typical merge operation would create.
  3. Handling conflicts

    • Git pull: If there are conflicts during a pull, Git pauses the merge and asks you to resolve the conflicts manually. After resolving conflicts, you must commit the changes to complete the merge.

    • Git rebase: During a rebase, if conflicts arise, you also need to resolve each conflict manually. However, the rebase process requires you to use git rebase --continue to proceed after fixing conflicts. This method ensures that your feature branch can be tested as if the changes have been made on top of the base branch (main).

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2
  1. Use cases

    • Git pull is useful when you need a simple update to your current branch from the remote repository and are okay with an additional merge commit.

    • Git rebase is particularly beneficial if you want to create a clean, linear history without unnecessary merge commits. It's ideal for updating a feature branch with the latest changes from the main branch.

For further reading on the differences between git pull and git rebase see the official Git documentation on the pull and rebase commands.

0 min read
0
Share
Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
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