惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Vercel News
Vercel News
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
H
Help Net Security
罗磊的独立博客
The Cloudflare Blog
J
Java Code Geeks
博客园 - 叶小钗
I
InfoQ
B
Blog
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
月光博客
月光博客
博客园_首页
雷峰网
雷峰网
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
美团技术团队
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Git Rebase vs Reset vs Revert | When to use What ?
Ishan Jarwal · 2026-05-29 · via DEV Community
Cover image for Git Rebase vs Reset vs Revert | When to use What ?

Ishan Jarwal

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 :

  1. One of the commits has a bug.
  2. Another commit has a wrong commit message.
  3. 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) 

Enter fullscreen mode Exit fullscreen mode

You want to fix commit C and clean up the message in D.

Solution: Interactive Rebase

git rebase -i HEAD~3 

Enter fullscreen mode Exit fullscreen mode

You’ll see something like this:

pick abc123 B pick def456 C pick ghi789 D 

Enter fullscreen mode Exit fullscreen mode

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 --hard Deletes your changes completely. Dangerous!

When to Use?

  • You made local commits you don’t want anymore.
  • You want to uncommit but keep the changes.

Example:

You made two useless commits on top of main and want to remove them.

A — B — C — D  (main)              
            ↑            
            Your branch (HEAD) 

Enter fullscreen mode Exit fullscreen mode

To undo D and C:

git reset --soft HEAD~2 

Enter fullscreen mode Exit fullscreen mode

  • Your changes are still there but unstaged (due to --soft flag).
  • If you want to kill the changes, do
git reset --hard HEAD~2 

Enter fullscreen mode Exit fullscreen mode

After that your git timeline will look like this :

A — B  (main)
    ↑            
    Your branch (HEAD) 

Enter fullscreen mode Exit fullscreen mode

Important:

  • Never reset a branch that is already pushed and shared with others.
  • Resetting can permanently lose work (especially --hard).

3. Git Revert : "The Safe Undo Button"

What is it?

git revert creates a new commit that undoes the changes of a previous commit. It preserves history.

When to Use?

  • You want to undo a bad commit in a shared branch.
  • You want to keep history intact but fix a mistake.

Example:

Commit C introduced a bug. But you’ve already pushed it to origin/main.

A — B — C — D  (main)           
             ↑            
            Bug lives here 

Enter fullscreen mode Exit fullscreen mode

You can revert C safely:

git revert <commit-hash-of-C> 

Enter fullscreen mode Exit fullscreen mode

Git will create a new commit :

A — B — C — D — E  (main)                    
                ↑                 
              "Revert C" 

Enter fullscreen mode Exit fullscreen mode

Important:

  • Safe for shared/public branches.
  • Doesn’t remove the commit but inverts its changes.

Quick Summary: When to Use What?

Command Use When…
git rebase You want to rewrite and clean history (local-only).
git reset You want to remove commits or unstage changes locally.
git revert You need to undo commits safely in shared branches.

Visual Cheat Sheet

"I messed up a commit and want to fix it before pushing" → Rebase / Reset (local only)
"I pushed a bad commit, now I need to undo it on remote" → Revert
"I made several ugly small commits, want to squash them" → Rebase -i


Final Tips

  • Never rebase or reset public history unless you know what you’re doing.
  • Use git log --oneline --graph to visualize the history before making changes.
  • Always backup your branch with git branch backup/my-branch before risky operations.

Further Reading