Git Rebase
Rebase is an alternative to merge in Git. This applies your local commits on top of the incoming changes, rewriting the commit history. It’s often used to keep a cleaner, more linear history, especially when working on branches.
This page uses branching terminology. We recommend reading our page on branches to get familiar with these terms.
Introduction
When working on a project with others, your local branch may fall behind the main branch on the remote repository. git rebase lets you take the changes from your branch and apply them on top of the latest commits from another branch. This helps create a cleaner commit history by avoiding unnecessary merge commits.
Rebase vs Merge
There are some key differences between merge and rebase for Git:
- Merge keeps all commit history, including a new merge commit.
- Preserves the exact history and collaboration
- Rebase rewrites history to make it seem like your work was built off the latest main branch, creating a linear timeline.
- Avoids unnecessary merge commits.
- Cleans the history timeline.
- May result in more conflicts if any others are using the previous commit history and try to pull/push.
In group projects, merge is typically preferred in order to maintain the true history of changes made and prevent issues for other collaborators. Rebase is used on more personal branches, such as to clean up your branch history before merging into main.
How to Rebase
For our example, you’ve been working on a branch called feature-1 while changes were made to the main branch.
To rebase your feature-1 branch onto the latest version of main, use git checkout [branchname] to switch to the appropriate branch. Once on the correct branch, run the rebase command using:
$ git checkout feature-1
$ git pull origin main --rebase
If you already have the latest commits from main, you add your commits to the top of the commit hisotry. To do this, use:
$ git rebase main
Due to the rewriting of the commit history, a rebase needs to be followed by a force push. This can be done with the following terminal command:
$ git push --force
--forceoverwrites history on the remote repository and should be used with caution. Improper use of this argument may lead to loss of data.
Rebase with git pull
You can use a --rebase flag to automatically rebase when pulling. This is done using the following command:
$ git pull --rebase