View commit history
After committing several updates, or cloning a project, how to view the commit history
git log
official chestnut
Run the following command to get the project:
git clone https://github.com/scha
Run the git log command
available information
By default, without passing in any parameters, git log will list all commits in chronological order , with the most recent update at the top
Lists each commit's SHA-1 checksum, author's name and email address, commit time, and commit description
git log common command line parameters
-p, --patch: It will display the differences introduced by each submission (output in the format of patch ).
-n: n is the number, limit the number of logs displayed, such as -2, output two
-p In addition to displaying the basic information, it also includes the changes of each submission. The advantage: when performing code review, you can quickly see the differences in the code submitted by others
--stat
See abbreviated statistics for each commit
Below each commit is a list of all modified files, how many files were modified, and which lines of the modified files were removed or added
--pretty
Commit history can be displayed in different formats, there are some built-in sub-options for you to use.
--pretty=oneline
Displays each commit on a single line, useful when browsing a large number of commits
There are also options short , full and fuller , which display information in basically the same format, but with varying degrees of detail
--pretty=format
The display format of records can be customized
Such output is especially useful for post-extraction analysis, because the format of the output will not change with Git updates
git log --pretty=format commonly used options
列出了 format 接受的常用格式占位符的写法及其代表的意义
选项 |
说明 |
%H |
提交的完整哈希值 |
%h |
提交的简写哈希值 |
%T |
树的完整哈希值 |
%t |
树的简写哈希值 |
%P |
父提交的完整哈希值 |
%p |
父提交的简写哈希值 |
%an |
作者名字 |
%ae |
作者的电子邮件地址 |
%ad |
作者修订日期(可以用 --date=选项 来定制格式) |
%ar |
作者修订日期,按多久以前的方式显示 |
%cn |
提交者的名字 |
%ce |
提交者的电子邮件地址 |
%cd |
提交日期 |
%cr |
提交日期(距今多长时间) |
%s |
提交说明 |
当 oneline 或 format 与另一个 log 选项 --graph 结合使用时尤其有用,展示你的分支、合并历史:
git log --pretty=format:"%h %s" --graph
* 2d3acf9 ignore errors from SIGCHLD on trap
* 5e3ee11 Merge branch 'master' of git://github.com/dustin/grit
|\
| * 420eac9 Added a method for getting the current branch.
* | 30e367c timeout code and tests
* | 5a09431 add timeout protection to grit
* | e1193f8 support for heads with slashes in them
|/
* d6016bc require time for xmlschema
* 11d191e Merge branch 'defunkt' into local
git log 输出格式的常用选项
选项 |
说明 |
-p |
按补丁格式显示每个提交引入的差异。 |
--stat |
显示每次提交的文件修改统计信息。 |
--shortstat |
只显示 --stat 中最后的行数修改添加移除统计。 |
--name-only |
仅在提交信息后显示已修改的文件清单。 |
--name-status |
显示新增、修改、删除的文件清单。 |
--abbrev-commit |
仅显示 SHA-1 校验和所有 40 个字符中的前几个字符。 |
--relative-date |
使用较短的相对时间而不是完整格式显示日期(比如“2 weeks ago”)。 |
--graph |
在日志旁以 ASCII 图形显示分支与合并历史。 |
--pretty |
使用其他格式显示历史提交信息。可用的选项包括 oneline、short、full、fuller 和 format(用来定义自己的格式)。 |
--oneline |
--pretty=oneline --abbrev-commit 合用的简写。 |
限制输出长度
-n:限制输出提交历史的数量
--since、--until:按照时间限制
下面的命令会列出最近两周的所有提交
git log --since=2.weeks
该命令可用的格式十分丰富
可以是类似 "2008-01-15" 的具体的某一天,也可以是类似 "2 years 1 day 3 minutes ago" 的相对日期。
git lot 限制输出的选项
选项 |
说明 |
-<n> |
仅显示最近的 n 条提交。 |
--since, --after |
仅显示指定时间之后的提交。 |
--until, --before |
仅显示指定时间之前的提交。 |
--author |
仅显示作者匹配指定字符串的提交。 |
--committer |
仅显示提交者匹配指定字符串的提交。 |
--grep |
仅显示提交说明中包含指定字符串的提交。 |
-S |
仅显示添加或删除内容匹配指定字符串的提交。 |
-- |
仅显示某些文件或目录的历史提交,-- 文件名/目录名 |
--no-merges |
不显示合并提交的历史记录 |
来看一个实际的例子,如果要在 Git 源码库中查看 Junio Hamano 在 2008 年 10 月其间, 除了合并提交之外的哪一个提交修改了测试文件,可以使用下面的命令:
$ git log --pretty="%h - %s" --author='Junio C Hamano' --since="2008-10-01" \
--before="2008-11-01" --no-merges -- t/
5610e3b - Fix testcase failure when extended attributes are in use
acd3b9e - Enhance hold_lock_file_for_{update,append}() API
f563754 - demonstrate breakage of detached checkout with symbolic link HEAD
d1a43f2 - reset --hard/read-tree --reset -u: remove unmerged new paths
51a94af - Fix "checkout --track -b newbranch" on detached HEAD
b0ad11e - pull: allow "git pull origin $something:$current_branch" into an unborn branch
git commit 没反应
git commit 没有反应的原因可能有很多,下面是一些常见的原因:
你没有将文件添加到暂存区,因此 git commit 无法提交。你需要在 git commit 之前运行 git add 将文件添加到暂存区。
你的 git 配置文件中缺少用户名和电子邮件地址。git commit 需要这些信息来记录提交的作者和邮箱。
你的 git 客户端版本过旧,不兼容当前的 git 服务器版本。
你正在进行的操作在 git 分支上是不安全的,如:正在合并分支或正在执行 Gitflow 工作流程中的操作。
你没有权限提交到远程仓库。
你的网络连接中断导致无法连接到远程仓库。
如果你无法解决问题,可以尝试查看 git 的日志文件(通常在 .git/logs 目录中)或检查远程仓库的日志以获取更多信息。