## 2.9 改变历史 ### 2.9.1 悔棋 `git commit --amend -m` 会将当前的暂存区(staging area)中的更改与上一次的提交合并为一个新的提交,同时允许你修改提交信息。 ```bash git commit -amend -m "message" # 修改上次提交的message ``` 添加一个误删的文件 ```bash git checkout HEAD^ -- # 从上次的提交恢复文件 git status git commit --amend -m "message" # 将当前的修改添加到上次的提交中 ``` ### 2.9.2 多步悔棋 想要将最近的两个提交压缩为一个,并把提交说明改为“modify hello.h”,可以使用如下方法进行操作。 ```bash git reset --soft HEAD^^ # 重置到两次提交之前 git status git commit -m "modify hello.h" ``` ### 2.9.3 回到未来 git rebase ```bash git cherry-pick # 从众多的提交中挑选出一个提交应用在当前的工作分支中 ``` 去掉某个commit ```bash git checkout # 先切换到某次提交 git cherry-pick # 输入之后的提交 git checkout # 再切换回来 ``` 合并两次commit ```bash git checkout [|] git reset --soft HEAD^^ # 向前移动两次 git commit -C C # 提交,重用C提交的提交说明 git cherry-pick E git cherry-pick F ```