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

How to Git rebase onto another branch

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 that involves moving or combining a sequence of commits to a new base commit. Rebase is useful for integrating changes from one branch into another without creating a merge commit. It's particularly advantageous for cleaning up your commit history before merging a feature branch into the main 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
  • Linear history: Rebasing helps maintain a linear project history, which can simplify both navigation and troubleshooting.
  • Avoiding redundant merges: By rebasing, you can avoid unnecessary merge commits, keeping your project history clean and readable.

Here’s how to rebase one branch onto another, effectively changing the base of your feature branch to incorporate updates from the mainline or another branch.

Always start by fetching the latest changes from your repository to ensure that your local branches are up-to-date.

Terminal
git fetch origin

This command will update your remote-tracking branches.

Switch to the branch you want to rebase. This is typically a feature branch that you want to update with changes from another branch.

Terminal
git checkout feature-branch

Now, perform the rebase operation to change the base of your feature branch to another branch, such as main or develop. The following command rebases the current branch onto the main branch:

Terminal
git rebase main

Alternatively, if you want to specify that the rebase should start from a particular commit on your feature branch and move onto a different branch, you can use:

Terminal
git rebase --onto main start-point feature-branch

In this command, start-point refers to the commit on your feature branch where you want to start the rebase.

During rebase, conflicts may occur. Git will stop at the first conflicted commit and allow you to resolve conflicts. After resolving each conflict, you need to stage the changes with git add and then continue the rebase:

Terminal
git rebase --continue

If at any point you need to abort the rebase process, you can do so with:

Terminal
git rebase --abort

For a more detailed walkthrough on how to resolve these issues see this guide on Git merge conflicts.

After resolving all conflicts and completing the rebase, your branch history will have been rewritten to start from the latest commit of the main branch.

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 your branch has been previously pushed and you've changed its history by rebasing, you'll need to force-push to update the remote branch:

Terminal
git push origin feature-branch --force

When you rebase a branch in Git, you essentially rewrite its commit history, potentially altering the commits' SHA-1 hashes and their relationships. If you attempt to push the rebased branch to the remote repository without force-pushing, Git will reject the push operation because it recognizes that the commit history has diverged from the remote branch's history.

To overcome this, you use the --force flag with the git push command, indicating that you are aware of the history changes and intentionally want to overwrite the remote branch with the rebased version. By force-pushing, you update the remote branch to reflect the new commit history of your local branch.

However, it's crucial to exercise caution when force-pushing, especially in collaborative environments, as it can potentially disrupt other developers' work if they have based their changes on the previous version of the branch. It's generally recommended to communicate with your team before force-pushing to ensure that everyone is aware of the changes and can adjust their work accordingly.

For further reading on rebasing 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