Git Rebase vs Reset vs Revert | When to use What ?
The Scenario: "Oops, I Made a Messy Commit History" Imagine you're working on a feature branch called feature/login-page . You’ve made a few commits, but now : One of the commits has a bug . Another commit has a wrong commit message . Your commit history looks like a jumbled mess compared to main . Time to clean it up! But… should you rebase , reset , or revert ? First, Let’s Understand the Core Idea of Each: Command What it Does Safe for Shared Branches? rebase Rewrites commit history to make it linear and clean No , unless used carefully reset Moves the HEAD and branch pointer to a different commit No , dangerous on shared branches revert Creates a new commit that undoes a previous commit Yes , safe to fix public history 1. Git Rebase : "Let’s Rewrite History Neatly" What is it? git rebase allows you to move or "replay" your commits onto another branch or rearrange them to make a clean, linear history. When to Use? You want to clean up messy commit history before merging. You want to squash commits . You want to change commit messages . Example: You have these commits in feature/login-page : A — B — C — D (feature/login-page) ↑ Buggy commit (C) You want to fix commit C and clean up the message in D . Solution: Interactive Rebase git rebase -i HEAD~3 You’ll see something like this: pick abc123 B pick def456 C pick ghi789 D You can now: Change "pick" to "edit" for C to fix the bug. Change "pick" to "reword" for D to fix the commit message. Squash commits if needed. Important: This rewrites commit hashes. If your branch is pushed and shared , rebasing can mess up others' history. Use with care! 2. Git Reset : "Let’s Go Back in Time (Dangerously Powerful)" What is it? git reset moves your branch pointer ( HEAD ) to a previous commit. It can unstage files , remove commits , or even delete code changes . Types of Reset: Command What happens? git reset --soft Keeps your changes staged. git reset --mixed Unstages changes but keeps them in the working directory. git reset