git log 常用命令
更新时间:2023-02-22 15:57:53标签:git
示例
遍历src/pages下的所有文件, 将每个文件的mtime修改为git log 中最后一次提交的时间
1#!/bin/sh23git ls-tree -r --name-only HEAD src/pages | while read filename; do45touch -d "$(git log -1 --format="%ai" -- $filename)" $filename67done
1# 执行以上命令时注意修改git config core.quotepath,否则包含中文名称的路径将会被转移成\xx\xx从而引发报错2git config --global core.quotepath false
应用场景
我们可以在编译期间,获取每个文件的最后提交时间,比如本项目,在执行git actions时,遍历每篇文章的最后提交时间,作为文章的最后更新时间。
--format 参数详解
作者
-
%an
%aN
作者名称1git log -1 --format="%an" -- filepath2# output: ChenJiYuan -
%ae
%aE
作者邮箱1git log -1 --format="%ae" -- filepath2# output: chenjiyuan.super@gmail.com -
%aD
提交时间(rfc2882)1git log -1 --format="%aD" -- filepath2# output: Wed Aug 24 12:33:50 2022 +0800 -
%ar
提交时间(相对时间)1git log -1 --format="%ar" -- filepath2# output: 3 hours ago -
%at
提交时间(timestamp)1git log -1 --format="%at" -- filepath2# output: 1661315630 -
%ai
提交时间(iso8601)1git log -1 --format="%ai" -- filepath2# output: 2022-08-24 12:33:50 +0800
提交者
%c 和 %a 开头的写法,一个是作者信息,一个是提交者信息,测试的输出结果一致, 不知道有什么区别
-
%cn
%cN
提交者的名字1git log -1 --format="%cn" -- filepath2# output: ChenJiYuan -
%ce
%cE
提交者的邮箱1git log -1 --format="%ce" -- filepath2# output: chenjiyuan.super@gmail.com -
%cD
提交时间(rfc2882)1git log -1 --format="%cD" -- filepath2# output: Wed Aug 24 12:33:50 2022 +0800 -
%cr
提交时间(相对时间)1git log -1 --format="%cr" -- filepath2# output: 3 hours ago -
%ct
提交时间(timestamp)1git log -1 --format="%ct" -- filepath2# output: 1661315630 -
%ci
提交时间(iso8601)1git log -1 --format="%ci" -- filepath2# output: 2022-08-24 12:33:50 +0800
Hash相关
-
%H
提交的hash1git log -1 --format="%H" -- filepath2# output: c6ededc02c13877d62f7a1b1b004ca8db7d1dd1d -
%h
提交的hash(缩写,前七位)1git log -1 --format="%h" -- filepath2# output: c6ededc -
%T
树hash(tree hash)1git log -1 --format="%T" -- filepath2# output: 74c55360530c7f38e6f459b5d4d32a5ce7a973ac -
%t
树hash(简写)1git log -1 --format="%t" -- filepath2# output: 74c5536 -
%P
parent hash1git log -1 --format="%P" -- filepath2# output: e224982135575c4cf4c7bbdc9cf70c228444f10c -
%p
parent hash(简写)1git log -1 --format="%p" -- filepath2# output: e224982
Commit 相关
-
%s
提交信息的标题1git log -1 --format="%s" -- filepath2# output: Fix some bugs -
%f
提交信息的标题(格式化后)1git log -1 --format="%f" -- filepath2# output: Fix-some-bugs -
%b
提交信息的body1git log -1 --format="%b" -- filepath2# output: 1. improve xxxx3# 2. fix xxx -
%d
ref name1git log -1 --format="%d" -- filepath2# output: (HEAD -> master, origin/master) -
%e
encoding(不知道是啥)