Rebasing is a Git operation that moves a branch to a new base commit. It allows you to maintain a cleaner and more linear project history by integrating changes from one branch into another.
Rebasing rewrites the commit history of a branch by applying its changes on top of another branch. Unlike merging, which creates a new merge commit, rebasing integrates commits into a single, linear sequence.
- Clean History: Rebasing creates a straight-line history, making it easier to understand and review.
- Feature Development: Use rebasing to keep feature branches up-to-date with the
main
branch. - Avoid Merge Commits: Rebasing avoids extra merge commits in the history.
Consider the following branches:
main: A --- B --- C
feature: D --- E
When rebasing feature
onto main
, the commits D
and E
are replayed on top of main
:
main: A --- B --- C --- D' --- E'
-
Switch to the branch you want to rebase:
git checkout feature
-
Rebase it onto the target branch (e.g.,
main
):git rebase main
If conflicts occur:
-
Git pauses the rebase and marks the conflicting files.
-
Resolve the conflicts manually in your editor.
-
Mark conflicts as resolved:
git add resolved_file.txt
-
Continue the rebase:
git rebase --continue
To abort the rebase:
git rebase --abort
Interactive rebasing allows you to edit, squash, or reorder commits.
git rebase -i HEAD~n
- Replace
n
with the number of commits you want to rebase.
pick
: Keep the commit as-is.edit
: Edit the commit message or content.squash
: Combine the commit with the previous one.reword
: Change the commit message.
-
Start an interactive rebase for the last 3 commits:
git rebase -i HEAD~3
-
Change the options in the editor, then save and close.
-
Complete the rebase, resolving any conflicts if needed.
- Rebase Before Pushing: Ensure you haven’t shared the branch with others before rebasing.
- Use for Local Changes: Avoid rebasing shared branches to prevent rewriting history others rely on.
- Keep Backups: Create a backup branch before rebasing to avoid accidental data loss.
Feature | Rebase | Merge |
---|---|---|
Commit History | Linear | Includes merge commits |
Complexity | Rewrites history | Preserves history as-is |
Use Case | Cleaning up commits, small teams | Maintaining complete history, large teams |
Command | Description |
---|---|
git rebase branch_name |
Rebase the current branch onto another branch. |
git rebase --continue |
Continue a paused rebase after resolving conflicts. |
git rebase --abort |
Cancel the rebase and return to the original state. |
git rebase -i HEAD~n |
Start an interactive rebase for the last n commits. |
-
Switch to the feature branch:
git checkout feature
-
Rebase onto
main
:git rebase main
-
Resolve conflicts (if any) and continue:
git rebase --continue
- On Public Branches: Rebasing rewrites history, which can confuse collaborators.
- If Unsure: Use merging instead of rebasing if you're new to Git or uncertain about rewriting history.
Rebasing is a powerful Git feature for maintaining a clean and linear commit history. By using it responsibly and understanding its implications, you can simplify collaboration and code reviews.
Next Steps: Cherry-Picking