# 08.查看提交历史

在程序开发过程中，了解项目的演变历史非常重要，因为它可以帮助您更好地理解代码库，并跟踪文件的变化。 Git 为我们提供了 `git log` 工具，用于查看 Git 代码库中的提交历史记录。首先我们先打开终端，进入项目目录，然后输入 `git log` 命令

```powershell
$ git log
commit d7f681fa3086bfa0222388775c827ab650e00a16 (HEAD -> master)
Author: aku <aku@example.com>
Date:   Fri May 5 19:05:08 2023 -0700

    Added abc to the test.txt

commit 01b8702bd46d5f4fcb18fc2d8523808b131a3ab6
Author: aku <aku@example.com>
Date:   Fri May 5 19:04:04 2023 -0700

    Add first file
```

在默认情况下，它会按时间顺序列出了所有提交，并提供有关每个提交的详细信息。输出格式包括提交哈希值、作者、提交日期、提交信息。

> 哈希值是一种唯一且固定长度的字符串，由哈希函数根据输入数据计算而来。它通常用于检测数据完整性和安全性因为哈希函数是一种单向函数，它将输入数据转换为一个固定长度的哈希值，并且该过程不可逆，还可以用于数据索引或加密等领域。`git log` 还提供很多选项参数，帮助我们更好的查看信息，例如 `--pretty`，它用于指定在输出中显示提交信息的格式。允许用户自定义输出的样式。`--pretty=oneline` 可以把提交标记和信息显示在一行内。

```powershell
$ git log --pretty=oneline
d7f681fa3086bfa0222388775c827ab650e00a16 (HEAD -> master) Added abc to the test.txt
01b8702bd46d5f4fcb18fc2d8523808b131a3ab6 Add first file
```

我们还可以用多种方式来控制输出结果，比如，只显示某个作者的提交。

```powershell
$ git log --pretty=oneline --author="aku"
d7f681fa3086bfa0222388775c827ab650e00a16 (HEAD -> master) Added abc to the test.txt
01b8702bd46d5f4fcb18fc2d8523808b131a3ab6 Add first file
```

## 还有一些常用的命令

```powershell
# 显示最近的两个提交记录
$ git log --pretty=oneline --max-count=2
# 显示五分钟前的所有提交记录
$ git log --pretty=oneline --since='5 minutes ago'
# 显示五分钟内的所有提交记录
$ git log --pretty=oneline --until='5 minutes ago'
# 显示当前分支上所有的提交历史
$ git log --pretty=oneline --all
```

还有非常多的选项，我们可以使用 `git log --help` 命令去查看详细帮助文档。

## 终极命令

随着提交越来越多，我们可以使用下面这条命令，它可以整洁的显示我们所需要的信息。

```powershell
$ git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short

* d7f681f 2023-05-05 | Added abc to the test.txt (HEAD -> master) [aku]
* 01b8702 2023-05-05 | Add first file [aku]
```

* `--pretty="..."`  定义输出的格式。
* `%h` :提交哈希值的缩写
* `%d` :提交所在的分支或标签等引用的名称
* `%ad` :提交的作者修订日期
* `%s` :提交的说明
* `%an`:作者姓名
* `--graph`:通知 git 以 ASCII 图形布局显示提交树（前面的 `*`）
* `--date=short`:设置为短日期格式（YYYY-MM-DD）

Git 还集成了一个让我们查看日志的图形化界面 gitk，我们在仓库目录输入 gitk 就会弹出这个工具。 ![1](https://225190396-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxZvzbTU1xnfUIQi6IZI0%2Fuploads%2Fgit-blob-0484d515cdd66055afa92fab118595e336b62725%2F8-1.png?alt=media)


---

# 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/08.-cha-kan-ti-jiao-li-shi.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.
