用 git reset 模式选择撤销方式

用 --soft、--mixed、--hard 控制重置后保留什么。

macOSdiff 文件对比Git命令行低风险
运行 git reset --soft HEAD~1,回退分支指针同时保留暂存改动。

01命令

命令
$ git reset --soft HEAD~1

02作用说明

reset 会移动当前分支,并可选更新索引与工作区:soft 保留暂存,mixed(默认)取消暂存,hard 全部丢弃。理解这些模式才能在 shell 中精确重塑本地历史。

03风险

风险低风险
使用条件Git 2.20+ (install with Homebrew or Xcode Command Line Tools)
系统兼容性macOS with Git installed (Homebrew or Xcode Command Line Tools)
!注意: --hard 会永久丢弃未提交改动;若工作重要请先提交或 stash。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • git-reset documentation
    Git · vendor-doc · 2026-09-20
    依据: Documents soft, mixed and hard reset and their effect on index and working tree.

最后验证: 2026-10-13 · 分类: 开发者