实用的git log用法

时间:2023-03-09 14:42:59
实用的git log用法

git log可以很方便地查看日志,可以根据自己需要,将日志按照特定格式显示,或者输出某种格式。

最原始的输出样式:

$ git log

commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date: Mon Mar 17 21:52:11 2008 -0700 changed the version number commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date: Sat Mar 15 16:40:33 2008 -0700 removed unnecessary test commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <schacon@gee-mail.com>
Date: Sat Mar 15 10:31:28 2008 -0700 first commit

这时输出的是最近几个git log,每个log都非常详细,如果想要简洁的显示模式,可以试试下面的:

(先按q键退出 git log)

git log –pretty=online

将log导出,可以用在代码后面加上 >filename.txt,或者其他格式,例如:

git log –pretty=online >log.xml

显示最近两个git文件的差异变化

git log -p -2

-2表示最近两个文件

git log –stat

查看最近提交的状态

git log 按照特定格式输出,例如:

git log –pretty=format:”%h - %an, %ar : %s”

ca82a6d - Scott Chacon, 6 years ago : changed the version number
085bb3b - Scott Chacon, 6 years ago : removed unnecessary test
a11bef0 - Scott Chacon, 6 years ago : first commit

自定义格式:

%H - Commit hash

%h - Abbreviated commit hash

%T - Tree hash

%t - Abbreviated tree hash

%P - Parent hashes

%p - Abbreviated parent hashes

%an - Author name

%ae - Author email

%ad - Author date (format respects the –date=option)

%ar - Author date, relative

%cn - Committer name

%ce - Committer email

%cd - Committer date

%cr - Committer date, relative

%s - Subject

其他git log格式可以参考:

https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History