# 16.修改提交内容

## 修改文件，并提交版本库

在 test 文件中添加一行 123，并保存。使用 `git commit -a -m "Added 123 to the test.txt"` 命令提交至版本库。

```bash
# 查看文件内容
$ cat .\test.txt
abc
123

# 提交到版本库
$ git commit -a -m "Added 123 to the test.txt"
[master ed5fe0d] Added 123
 1 file changed, 2 insertions(+), 1 deletion(-)
 
# 查看提交日志
$ git hist
* fc49e5f 2023-05-05 | Added 123 to the test.txt (HEAD -> master) [aku]
* d7f681f 2023-05-05 | Added abc to the test.txt (tag: v1) [aku]
* 01b8702 2023-05-05 | Add first file (tag: v1-beta) [aku]
```

## 再次修改文件，并覆盖上一次提交的内容

```bash
# 查看文件内容
$ cat .\test.txt
abc
123456

# 提交到版本库
$ git commit -a -m "Added 123456 to the test.txt" --amend
[master 929f644] Added 123456 to the test.txt
 Date: Fri May 5 19:25:32 2023 -0700
 1 file changed, 2 insertions(+), 1 deletion(-)
 
# 查看提交日志
$  git hist
* 929f644 2023-05-05 | Added 123456 to the test.txt (HEAD -> master) [aku]
* d7f681f 2023-05-05 | Added abc to the test.txt (tag: v1) [aku]
* 01b8702 2023-05-05 | Add first file (tag: v1-beta) [aku]
```

## 解释

`--amend` 是一个 Git 命令选项，用于修改最新的提交（或当前分支上的指定提交）而无需创建新的提交记录。它可以用于更改引导提交或添加/删除文件等操作。

该选项主要用于以下两个情况：

1. 修改最新的提交：如果您忘记将某个文件提交到最新的提交中，或者需要修改提交消息，则可以使用 `git commit --amend` 命令来修改最新的提交。这将会打开编辑器并允许您修改提交消息和暂存区中的文件版本。完成修改后，Git 将更新最新的提交记录而无需创建新的提交记录。
2. 添加/删除文件：如果您想将文件添加到最新的提交中，或者从最新的提交中删除文件，则也可以使用 `git commit --amend` 命令完成此操作。首先，使用 `git add` 命令将更改的文件添加到暂存区中。接下来，运行 `git commit --amend` 命令，并使用 `--no-edit` 选项以保留现有的提交消息不变。Git 将会使用暂存区中的文件替换最新的提交记录，而无需创建新的提交记录。

请注意，在使用 `--amend` 选项时，请确保仅更改了最新的提交并且没有共享该提交，否则可能会破坏团队成员的工作进程。如果您已经共享了提交，则应使用 `git revert` 命令来逆转该提交，而不是修改它。
