git reset
git
用法
git reset [--soft|--mixed|--hard] <commit> | git reset <path>把分支指针移到别的提交:--soft 让改动全留在暂存区,--mixed(默认)连暂存区一起清掉但改动还在文件里,--hard 连文件里的改动也一起丢掉,找不回来。
常用选项
| 选项 | 含义 |
|---|---|
| --soft | move the branch only; index and files keep everything, so the change stays staged |
| --mixed | default: also clear the staging area, leaving edits in your files |
| --hard | also overwrite your files; uncommitted work is destroyed |
| <path> | no commit given: unstage that path |
| --keep | move but refuse if it would overwrite a local change |
| --merge | like --hard but keeps changes that do not collide |
示例
git reset --soft HEAD~1undoes the last commit and leaves everything staged
git reset HEAD~1undoes the last commit; the changes sit in your files, unstaged
git reset src/app.tsunstages one file without changing it
git reset --hard origin/mainthrows away local commits AND uncommitted edits
难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. git reset 是做什么的?
把分支指针移到别的提交:--soft 让改动全留在暂存区,--mixed(默认)连暂存区一起清掉但改动还在文件里,--hard 连文件里的改动也一起丢掉,找不回来。
Q. 怎么写?
git reset [--soft|--mixed|--hard] <commit> | git reset <path> —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 6 个,完整列表在 man git。这条命令属于git。
相关命令
man 手册: man git-reset