# 15.从分支中删除提交

本节我们学习如何删除提交。

## 创建一个新提交，并打上标签

在 test 文件中，添加一行 oops，并提交

```powershell
# 查看文件内容
$ cat .\test.txt
abc
oops

# 提交到版本库
$ git commit -a -m "Oops,an error committed"

# 查看提交日志
$ git hist
* 739560c 2023-05-05 | Oops,an error committed (HEAD -> master) [aku]
* 1ca1854 2023-05-05 | Revert "Added 123 to the test.txt" [aku]
* 375f4fe 2023-05-05 | Added 123 to the test.txt [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]

# 打上标签 oops
git tag oops

$ cat .\test.txt
abc
oops
```

## 恢复提交之前的文件

```powershell
$ git reset --hard v1

$ git hist
* d7f681f 2023-05-05 | Added abc to the test.txt (HEAD -> master, tag: v1) [aku]
* 01b8702 2023-05-05 | Add first file (tag: v1-beta) [aku]

$ cat .\test.txt
abc
```

之前我们说过 `HEAD` 是 Git 中一个特殊的指针，它始终指向当前所在分支的最新提交（或者是合并操作中的合并状态）。命令的意思是将您当前分支的 HEAD 指针重置到 v1 标签所指向的提交，同时强制更新工作目录和暂存区以与该提交匹配。这将删除所有未提交的更改和文件，并永久性地回滚您的代码库至 v1 标签所代表的状态。

## 记录其实还在，并且我们还可以引用

```powershell
$ git hist --all
* 2dd1257 2023-05-04 | Oops,an error committed (tag: oops) [aku]
* bcd7c90 2023-05-04 | Added abc (HEAD -> master, tag: v1) [aku]
* 05ade05 2023-05-04 | Add first file (tag: v1-beta) [aku]
```

本地分支的重置通常是安全的。任何“意外”情况通常可以通过使用所需提交再次重置来恢复。但是，如果该分支在远程存储库上共享，则重置可能会使其他共享该分支的用户感到困惑。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alanmpan.gitbook.io/git-learning/git/15.-cong-fen-zhi-zhong-shan-chu-ti-jiao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
