阿里云主机折上折
  • 微信号
Current Site:Index > Undo operations (git reset, git checkout, git revert)

Undo operations (git reset, git checkout, git revert)

Author:Chuan Chen 阅读数:22706人阅读 分类: 开发工具

In the Git version control system, undo operations are a frequent requirement in daily development. git reset, git checkout, and git revert are three core undo commands, each suited for different scenarios. Understanding their differences can effectively prevent code loss or history confusion.

git reset: Rolling Back Commit History

git reset modifies commit history by moving the HEAD pointer and is divided into three modes:

# Soft reset (preserves working directory)
git reset --soft HEAD~1

# Mixed reset (default, preserves working directory but resets staging area)
git reset HEAD~1

# Hard reset (completely discards changes)
git reset --hard HEAD~1

Typical Scenarios:

  1. Undoing local unpushed commits:
# Roll back to the previous two commits (preserves file modifications)
git reset HEAD~2
  1. Fixing incorrect staging:
# Unstage all changes (preserves working directory modifications)
git reset

Risk Warning:
Hard reset permanently deletes uncommitted changes. It is recommended to use git stash to save changes first:

// Example: Recovering mistakenly deleted code
function importantFunction() {
  console.log("Critical feature");
}

git checkout: Switching Branches and Discarding Working Directory Changes

This command has dual purposes, distinguished by parameters:

Switching Branches

# Switch to the feature branch
git checkout feature

Discarding Working Directory Changes

# Discard changes to a specific file
git checkout -- src/index.js

# Discard all unstaged changes
git checkout -- .

Special Usage:
Creating a temporary branch pointing to a specific commit:

git checkout -b hotfix 2a1b3c4

git revert: Safely Undoing Public Commits

Unlike reset, revert undoes historical commits by adding new commits:

# Undo a specific commit (generates a new commit)
git revert 3f4e5d6

# Undo the most recent commit (opens editor automatically)
git revert HEAD

Conflict Resolution:
When conflicts occur, resolve them manually and continue:

git revert --continue
# Or abort the undo
git revert --abort

Team Collaboration Guidelines:
For already pushed commits, always use revert instead of reset:

# Wrong approach: Force-pushing reset history
git push -f origin main

# Correct approach: Adding a reversal commit
git revert 5a6b7c8
git push origin main

Comprehensive Comparison and Selection Strategy

Command Scope Use Case Modifies History?
reset Local repository Undoing unpushed commits Yes
checkout Working directory/branch Discarding changes or switching branches No
revert Public history Safely undoing pushed commits No

Complex Scenario Example:
When needing to modify a historical commit:

# Interactive rebase (dangerous operation)
git rebase -i HEAD~3
# Mark the commit to edit as "edit"
# After modifications, continue rebasing
git commit --amend
git rebase --continue

Advanced Techniques and Pitfall Avoidance

  1. Recovering with Reflog:
    After accidental operations, recover lost commits using reflog:

    git reflog
    git reset --hard HEAD@{2}
    
  2. Partial File Reset:
    Restore specific files from another commit:

    git checkout 2a1b3c4 -- package.json
    
  3. Batch Undo:
    Use revert to combine undos for multiple commits:

    git revert -n 1a2b3c4..5d6e7f8
    git commit -m "Batch undo for feature X"
    
  4. Hook Protection:
    Prevent erroneous commits with pre-commit hooks:

    // .git/hooks/pre-commit
    #!/bin/sh
    if git diff --cached | grep 'TODO'; then
      echo "Commit contains unfinished TODO markers"
      exit 1
    fi
    

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.