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

How to Git rebase origin main

Greg Foster
Greg Foster
Graphite software engineer


Note

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


Rebasing is a process in Git used to apply changes from one branch onto another. It's primarily used to maintain a cleaner, linear project history by rearranging commits. Instead of creating a merge commit, rebasing re-writes the project history by placing commits from one branch to the top of another branch.

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.

Learn more
  • Rewriting history: Normally, when you create a new branch from a particular commit, that branch starts with the same commit history as the branch it was created from. However, when you rebase a branch onto another commit, Git effectively "replays" each commit on the branch as if it were created from the new base commit, thus modifying the branch's commit history.
  • Avoiding merge commits: Rebase is often used to avoid merge commits and keep a linear project history which can simplify debugging and code review.

Rebasing onto the main branch (or the primary development branch in your repository) is commonly done in several scenarios to maintain a cleaner commit history and streamline the development process:

  1. Integration of feature branches: When you have finished working on a feature branch and are ready to merge it into the main branch, rebasing onto main allows you to incorporate any changes made to main since you branched off. This helps to resolve any potential conflicts early and ensures that your feature branch is based on the most recent codebase.

  2. Avoiding merge commits: Rebasing allows you to incorporate changes from the main branch by replaying your commits on top of the latest main commit. This results in a linear commit history without additional merge commits, which can make the commit history cleaner and easier to understand.

  3. Syncing with upstream changes: If changes have been made to the main branch by other contributors since you created your feature branch, rebasing onto main allows you to incorporate those changes into your branch. This helps to keep your branch up to date with the latest developments in the project.

  4. Maintaining a Linear History: Some development workflows prefer to maintain a linear commit history, where each commit builds upon the previous one without unnecessary merge commits. Rebasing onto main helps to achieve this by incorporating changes from main into your branch in a linear fashion.

While Git is an incredibly useful tool, it has many shortcomings, particularly with rebasing, and managing stacked pull requests.

The Graphite CLI simplifies git, handles rebasing automatically, and allows you to create, submit, and stack pull requests right from the command line.

Under the hood, the CLI runs Git to create branches, commits, and metadata, which means you can still use Git in your scripts, tooling, or whenever you feel like it. Read more about installing the Graphite CLI in our docs.

screenshot of the Graphite CLI

Here's how you would typically perform a rebase in Git:

Before rebasing, you need to fetch the latest changes from the remote repository to ensure you're working with the most up-to-date data.

Terminal
git fetch origin

This command updates your remote-tracking branches under refs/remotes/.

To rebase your current branch onto the latest commit from origin/main (or any other branch), you use the git rebase command. This is what it looks like if you're rebasing to the latest main:

Terminal
git rebase origin/main

If there are conflicts during the rebase, Git will stop and allow you to fix the conflicts manually. After resolving each conflict, you need to add the changes with git add, and then continue the rebase process using:

Terminal
git rebase --continue

Repeat this process until all conflicts are resolved and the rebase is complete.

For more detailed instructions, see this guide on conflict resolution in Git.

The best engineers use Graphite to simplify Git

Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.

Git started for free

If you have already pushed your branch before rebasing and the rebase modifies the branch history, you'll need to force push your changes:

Terminal
git push origin <your-branch-name> --force

Using --force is necessary here because you've altered the commit history, and Git needs to overwrite the previous commits on the remote server.

For further reading see the official Git documentation.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2