git revert
git
用法
git revert [-n] <commit>通过新增一个相反的提交来撤销旧提交;它不删历史,这正是它和移动指针的 reset 的区别,已经推上去的提交要用 revert。
常用选项
| 选项 | 含义 |
|---|---|
| -n | stage the inverse change without committing |
| -m <parent> | which side to keep when reverting a merge commit |
| --no-edit | accept the generated message |
| --continue | finish after resolving conflicts |
| --abort | cancel and go back |
示例
git revert a1b2c3dadds a new commit that undoes that one
git revert --no-edit HEADundoes the last commit without opening an editor
git revert -m 1 <merge-commit>undoes a merge, keeping the first parent
难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. git revert 是做什么的?
通过新增一个相反的提交来撤销旧提交;它不删历史,这正是它和移动指针的 reset 的区别,已经推上去的提交要用 revert。
Q. 怎么写?
git revert [-n] <commit> —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 5 个,完整列表在 man git。这条命令属于git。
相关命令
man 手册: man git-revert