git rebase
git rebase
: reapply commits from one branch on top of another branch.
The git rebase
command is used to move or combine a sequence of commits to a new base commit. Unlike git merge
, which creates a merge commit to combine two branches, rebasing takes the commits from your current branch and replays them on top of another branch, creating a linear commit history.
Rebasing is particularly useful for maintaining a clean, linear project history. Instead of creating a "commit salad" where commits from different branches are interleaved, rebasing ensures that your feature commits appear as if they were developed directly on top of the latest changes from the target branch.
There are two main types of rebasing:
-
Regular rebase: Takes all commits from your current branch and replays them on top of another branch, maintaining the same commit structure but with new base commits.
-
Interactive rebase (
git rebase -i
): Arguably the most powerful feature of Git, it allows you to interactively edit, reorder, squash, or drop commits during the rebase process.
Rebasing rewrites commit history by creating new commits with the same changes but different commit hashes. This means you should never rebase commits that have been shared with others, as it can cause confusion and conflicts for collaborators.
git merge
vs git rebase
Imagine you're making a sandwich (branch: feature/sandwich
) while your friend reorganizes the kitchen (branch: feature/kitchen-reorg
). Then you try to combine your work.
With git merge
:
HISTORY: YOU-THEM-THEM-YOU-⚠️-THEM-YOU-⚠️-THEM-YOU-⚠️-THEM-YOU-MERGE
With git rebase
:
HISTORY: THEM-THEM-THEM-THEM-⚠️-YOU-YOU-YOU-YOU
The key difference:
- Merge preserves the chaotic reality of parallel work with multiple conflicts.
- Rebase creates a clean timeline with one conflict resolution point, then smooth sailing.
Both end with the same result (sandwich + organized kitchen), but rebase tells a simpler story by pretending you started after the kitchen was already reorganized.
Pulling with rebase
When you want to update your current branch with the latest changes from the remote branch, you can use git pull --rebase
. This command fetches the latest changes and then rebases your local commits on top of the fetched changes, ensuring a clean commit history.
This behavior can be set as the default for your repository by configuring Git:
git config pull.rebase true
Examples
Rebase the current branch onto main
:
git rebase main
Start an interactive rebase for the last 3 commits:
git rebase -i HEAD~3
Continue a rebase after resolving conflicts:
git add .
git rebase --continue
Abort a rebase and return to the original state:
git rebase --abort
Update your current branch with the latest changes from main
(take the main
branch and put all of your commits on top of it):
git fetch origin
git rebase origin/main
Set rebase as the default behavior for git pull
:
git config pull.rebase true