1
0
forked from iicd/git-learner
git-learner/notes/2.9-change_history.md
2024-08-22 20:13:03 +02:00

52 lines
1.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 2.9 改变历史
### 2.9.1 悔棋
`git commit --amend -m` 会将当前的暂存区staging area中的更改与上一次的提交合并为一个新的提交同时允许你修改提交信息。
```bash
git commit -amend -m "message" # 修改上次提交的message
```
添加一个误删的文件
```bash
git checkout HEAD^ -- <filename> # 从上次的提交恢复文件
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 <commit> # 先切换到某次提交
git cherry-pick <commit> # 输入之后的提交
git checkout <branch> # 再切换回来
```
合并两次commit
```bash
git checkout [<tag>|<commit>]
git reset --soft HEAD^^ # 向前移动两次
git commit -C C # 提交重用C提交的提交说明
git cherry-pick E
git cherry-pick F
```