Skip to content

Reset

一句话解释

Reset moves current 分支指针到 不同提交, effectively rewinding history. three 模式 control 什么 happens 到 staged and 工作 files.

用大白话说

git reset moves the current branch pointer to a specified commit, rewinding history. It has three modes that control what happens to the staging area and working directory:

  • --soft: moves the branch pointer only; staged changes and working files are untouched. The undone commit's changes appear as staged.
  • --mixed (default): moves the pointer and unstages changes; working files are untouched. Changes appear as unstaged.
  • --hard: moves the pointer, unstages, AND discards all working directory changes. Everything is lost.

Reset rewrites history — the undone commits become unreachable. This is fine for local branches but dangerous for shared branches. Use git revert instead for shared branches.

示例

bash
# Undo last commit, keep changes staged
git reset --soft HEAD~1

# Undo last commit, unstage changes
git reset HEAD~1

# Undo last commit, discard ALL changes
git reset --hard HEAD~1

reset moves 分支指针. --soft keeps staged, --mixed unstages, --困难 discards 所有.

常见混淆

  • 容易混淆的对象: revert

    关键区别: reset rewinds 分支指针 (rewrites history); revert 添加新 inverse 提交 (preserves history). Use reset locally, revert on shared 分支.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表