
Git rebase is a powerful command that allows developers to modify commit history, integrate changes from one branch to another, and keep a clean, linear project timeline. This article explains how to rebase branch commits in Git.
The Short Answer Version
If you're already familiar with Git and need quick reference commands to rebase branches and commits, they include:
# Reapply your branch commits on top of the latest main
$ git switch feature ## Switch to your feature branch
$ git rebase main ## Rebase the commits on top of the main branch
# Interactively rebase the last 3 commits (edit, squash, reorder)
$ git rebase -i HEAD~3
# Rebase local commits while pulling remote changes (cleaner history)
$ git pull --rebase origin main
# After rebase, push rewritten history to remote
$ git push --force-with-lease ## Safer than --force
Git Rebase
Rebasing takes commits from a source branch and reapplies them on top of a target branch. This results in a streamlined history without unnecessary merge commits. The moved commits are the same as the old ones, but the commit ID is new, meaning that Git created a new commit using the previous ones.
Command Syntax
git rebase [OPTIONS] [UPSTREAM [BRANCH]]
Where,
[OPTIONS]
control how the command works. These are covered in the later section of this article.UPSTREAM
specifies the branch onto which the commits are to be placed.[BRANCH]
specifies the branch whose commits you want to rebase. If not specified, it defaults to the current branch.
Command usage
Switch to the branch you want to rebase.
console$ git switch feature-branch
Rebase, or move your feature-branch commits to the top of the main branch.
console$ git rebase main
You can also clean up your commits in interactive mode before rebasing.
console$ git rebase -i
Push your branch (if you haven't pushed before):
console$ git push
If you've already pushed the branch before rebasing, push with force (because the history has changed):
console$ git push --force-with-lease
Note--force-with-lease
instead of--force
ensures you don’t overwrite others' work on the branch.
Git Rebase vs Git Merge
git rebase
rewrites history by moving your commits between different branches. git merge
, on the other hand, creates a new commit by merging commits of two branches. It preserves the history as it does not move or edit any commits and their positions.
Use merging when working with shared branches, and use rebasing to clean up local or feature branch history before merging.
Use rebasing when:
- You want a cleaner, linear history.
- You want to avoid unnecessary merge commits.
- You're collaborating and want to reapply your changes on top of the latest main branch before pushing.
Git Merge
Git Merge combines changes from one branch into another by creating a new merge commit, which keeps the history of both branches intact. This approach helps you to keep a clear record of how development progressed.
Switch into your main branch.
console$ git switch main
Merges the changes from the
feature-branch
into the main and creates a merge commit if needed.console$ git merge feature-branch
Types of Git Rebase
Interactive Rebase
Interactive rebasing allows you to edit, delete, squash, or reorder commits. It’s ideal for cleaning up a messy commit history before pushing to a shared repo.
Run the below command to start an interactive rebase of the last 3 commits.
console$ git rebase -i HEAD~3
You’ll see a list like this in your editor:
pick a1b2c3d Add login functionality pick b2c3d4e Fix login bug pick c3d4e5f Update login style
You can replace
pick
with:reword
: Edit commit messageedit
: Modify the commitsquash
: Merge into previous commitdrop
: Remove the commit
Non-Interactive Rebase
This is the default mode when you run git rebase
without any flags. It automatically reapplies your commits on top of another branch in the same order, without opening an editor or letting you modify the commits interactively.
Rebase your commits on top of the main
branch:
$ git rebase main
This updates your current branch by reapplying its commits on top of the latest commits from main
. It's useful for keeping your feature branch up to date with the base branch.
Auto-Merge Rebase
When you use --autostash
, Git will automatically stash your local changes before rebasing and reapply them after the rebase. This prevents errors due to uncommitted changes.
Stashes your local changes before rebasing onto main, then reapplies them after the rebase completes.
console$ git rebase main --autostash
Common Configuration Options
Common configuration options for git rebase
are shown below:
--interactive
(-i
): Opens an editor to manually edit, reorder, squash, or drop commits during the rebase process.--skip
: Skips the current commit (usually one that caused a conflict) and continues the rebase.--continue
: Continues the rebase after you've resolved conflicts and staged the changes.--abort
: Cancels the rebase and restores the branch to its original state before the rebase began.--autostash
: Automatically stashes any local, uncommitted changes before the rebase starts, and reapplies them afterward.--onto <newbase>
: Rebase a range of commits onto a new base commit (used for advanced rebasing scenarios).
Conclusion
In this article, you learned how to rebase your Git commits using interactive and non-interactive methods and understand the difference between rebase and merge. You can keep a cleaner commit history, avoid unnecessary merge commits, and streamline collaboration. For more information, check out the official Git documentation.
No comments yet.