Undo operations (git reset, git checkout, git revert)
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:
- Undoing local unpushed commits:
# Roll back to the previous two commits (preserves file modifications)
git reset HEAD~2
- 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
-
Recovering with Reflog:
After accidental operations, recover lost commits usingreflog
:git reflog git reset --hard HEAD@{2}
-
Partial File Reset:
Restore specific files from another commit:git checkout 2a1b3c4 -- package.json
-
Batch Undo:
Userevert
to combine undos for multiple commits:git revert -n 1a2b3c4..5d6e7f8 git commit -m "Batch undo for feature X"
-
Hook Protection:
Prevent erroneous commits withpre-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
上一篇:数据冗余与高可用设计
下一篇:远程仓库操作概览