git rebase
Description:
Reapply commits from one branch onto another base. Creates a linear history by moving commits to a new base.
Syntax:
bash
git rebase <branch-name>
git rebase -i <commit>
git rebase --continue
git rebase --abortExamples:
bash
# Rebase current branch onto main
git rebase main
# Interactive rebase (edit last 3 commits)
git rebase -i HEAD~3
# Continue after resolving conflicts
git rebase --continue
# Skip current commit during rebase
git rebase --skip
# Cancel rebase operation
git rebase --abort
# Rebase and preserve merge commits
git rebase --preserve-merges mainDetailed Explanation:
- Rewrites history: Moves commits to a new base, creating new commit hashes
- Linear history: Creates a straight line of commits without merge commits
- Interactive mode:
-iallows editing, squashing, reordering, or dropping commits - Cleaner history: Makes project timeline easier to follow and understand
Common Errors:
CONFLICT (content): Merge conflict in <file>→ Solution: Resolve conflicts,git add <file>, thengit rebase --continuefatal: Needed a single revision→ Solution: Check branch name spellingerror: cannot rebase: You have unstaged changes→ Solution: Commit or stash changes before rebasingfatal: refusing to merge unrelated histories→ Solution: Add--allow-unrelated-historiesor usegit mergeinstead
Pro Tips:
- Golden Rule: Never rebase commits that have been pushed to a shared branch (rewrites history).
- Use interactive rebase to clean up commits before pushing:
git rebase -i HEAD~5 - Squash commits in interactive rebase: change
picktosquashors - Update feature branch with latest main:
git rebase main(cleaner than merging main into feature) - Reword commit messages: Use
rewordorrin interactive rebase - Warning: Rebase changes commit hashes; causes problems if commits are already shared
Related Commands:
git merge– Alternative integration method (preserves history)git cherry-pick– Apply specific commitsgit reflog– Recover from rebase mistakesgit push --force-with-lease– Safely push rebased branches