Vasu Patel logoVasu Patel
← Git Mastery
All chapters in this track
  1. 011. The Mental Model
  2. 022. Essential Setup
  3. 033. Core Daily Workflow
  4. 044. Branching
  5. 055. Merging & Conflict Resolution
  6. 066. Rebasing
  7. 077. Remote Repositories
  8. 088. Stashing
  9. 099. Undoing Things
  10. 1010. Tagging & Versioning
  11. 1111. Cherry-Pick
  12. 1212. Git Bisect & Blame
  13. 1313. Git Hooks
  14. 1414. Submodules
  15. 1515. Team Workflows
  16. 1616. Power Commands & Aliases
  17. 1717. Reflog: The Safety Net
  18. 1818. Practice Tasks
  19. 19Quick Reference Cheatsheet

Chapter 6 of 19

6. Rebasing

Rebase replays your commits on top of another branch. Result: linear history.

# Basic rebase
git switch feature/login
git rebase main              # replay feature commits on top of main

# After rebase, go back to main and fast-forward merge
git switch main
git merge feature/login      # fast-forward, no merge commit

Interactive rebase (clean up history)

git rebase -i HEAD~4         # edit last 4 commits

Editor opens with a list of commits. Change the action keyword:

ActionEffect
pickkeep as-is
rewordchange commit message
editpause to amend
squashmeld into previous commit, combine messages
fixupmeld, discard this commit's message
dropdelete this commit entirely

Example: squash 3 WIP commits into one before merge

pick a1b2c3 feat: scaffold login form
squash d4e5f6 wip: add validation
squash g7h8i9 fix: edge case null email

Result: one clean commit with a combined message.

Golden rule

Never rebase commits that have been pushed to a shared branch.
Rebase rewrites history. If others have pulled those commits, everyone's history diverges.

Safe rebase uses:

  • Local-only branches
  • Your own remote branch (before others pull it)
  • Feature branches not yet merged