How to Git rebase origin main

Greg Foster
Greg Foster
Graphite software engineer

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.

  • 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.

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.

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.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2