搜索参数为提交的创建者和提交者
git log --author=<pattern>
git log --committer=<pattern>
git log --author=“小明”
git log --author=“小明\|小红”
按时间搜索,支持的有很多类型
git log --since=<date>
git log --after=<date>
git log --until=<date>
git log --before=<date>
```如果你想要一个具体的时间区间的,可以把这个参数组合起来的,例如下面的命令:
```
git log --since="2018.03.12" --until="2018.03.18"
```
Search-提交信息
这个就厉害了,你可以搜索提交信息,也支持正表达式
```
git log --grep=<pattern>
git log --grep='喜欢' --oneline
```
Search-修改内容
一般情况下,我们想找一个提交,大多是为了某个修改去找,这个修改对应要么是具体的文件,要么是具体的修改的内容。放心,这个条件也支持。
文件
```
git log [--] <path>…
git log --oneline -- 11.txt
```
修改内容
这一部分其实是 Diff 的参数部分,有很多参数,这里列出两个。
```
git log -S<string>
git log -G<regex>
git log -S"喜欢你" --oneine
```
Search-合并相关的提交 & 文件
工作中,分支之间的合并,往往不是 fast-forword,而是 recursive strategy merge 策略式合并,所以会在历史中出现很多合并提交。运用下面的命令,你可以选择只看合并提交,或者非合并提交。
```
git log --merges
git log --no-merges
```
不幸发生了合并冲突,还可以用这个命令,可以快速找到冲突的文件。
```
git log --merge
```
Commit History Formatting 你想要什么格式的你说
默认版
这个我们最常用的,显示 commit 的 SHA1 值,创建作者和时间,提交信息。
```
git log
```
一行情书版
使用 --oneline 参数,only one line !只显示提交的 SHA1 值和提交信息,SHA1 还是缩短显示前几位。
这个命令,在你要根据信息去找提交的时候,比 git log 的效率要高点。
```
git log --oneline
```
毫发毕现版
这个版本,就是上面说的,做到加持 git diff-*的效果版本,最常使用的参数是 --stat 和 -p。
```
git log --stat
```
```
git log -p
```
角色版
这个按提交的创建者分类,因为目前只有我一个操作,就显示了我一个人的。
使用这个命令可以容易地看到谁做了什么,毫无保留的。
```
git shortlog
```
图文并茂版
这次命令使用三个参数 --oneline, --decorate 和 --graph 。
–oneline 刚才就是哪个一句话情书的。
–graph 选项会绘制一个 ASCII 图像来展示提交历史的分支结构。
–decorate 是用来可以显示出指向提交的指针的名字,也就是 HEAD 指针, feature/test等分支名称,还有远程分支,标签等。
```
git log --graph --oneline --decorate
```
最强推荐版
这个是在 stormZhang 张哥那里学来的:
```
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
```
效果比较炫酷,这个已经属于自定义格式了,git log 支持自定义样式的,有兴趣的娃子可以自己研究下。而且这个命令比较长,娃子们可以通过给这个命令设置别名解决:
```
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
```
这样以后直接输入 git lg 就行了。
```
```git log -n 显示最近n条提交历史
git log --after="2017-3-1" 只显示3月1号之后的提交,可以使用yesterday等相对时间概念
git log --after="2017-3-1" --before="2017-4-1" 显示3月1号到4月1号之间的提交
git log --author="nj" 只显示某个作者的提交
git log --filename 显示这个文件的提交历史
git log -S "Hello,World" 想知道某一段代码是何时加入的
git log master..feature 显示所有不在feature上的master分支的commit
git log --no-merges 过滤掉所有的merge信息
git log --merges 值显示merge信息
```
```# git log 代码统计
统计项目成员的代码提交量
git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
BreastStroke boxing balance
统计指定人 指定目录的提交量
git log --author="xuhui" --pretty=tformat: --numstat -- . ":(exclude)/d/hdl5/airflow-service/airflow/api_connexion" ":(exclude)/d/hdl5/airflow-service/airflow/compat" ":(exclude)/d/hdl5/airflow-service/airflow/dag_processing" ":(exclude)/d/hdl5/airflow-service/airflow/executors" ":(exclude)/d/hdl5/airflow-service/airflow/kubernetes" ":(exclude)/d/hdl5/airflow-service/airflow/metrics" ":(exclude)/d/hdl5/airflow-service/airflow/serialization" ":(exclude)/d/hdl5/airflow-service/airflow/utils" ":(exclude)/d/hdl5/airflow-service/airflow/api_internal" ":(exclude)/d/hdl5/airflow-service/airflow/config_templates" ":(exclude)/d/hdl5/airflow-service/airflow/datasets" ":(exclude)/d/hdl5/airflow-service/airflow/lineage" ":(exclude)/d/hdl5/airflow-service/airflow/migrations" ":(exclude)/d/hdl5/airflow-service/airflow/secrets" ":(exclude)/d/hdl5/airflow-service/airflow/ti_deps" ":(exclude)/d/hdl5/airflow-service/airflow/_vendor" ":(exclude)/d/hdl5/airflow-service/airflow/auth" ":(exclude)/d/hdl5/airflow-service/airflow/decorators" ":(exclude)/d/hdl5/airflow-service/airflow/hooks" ":(exclude)/d/hdl5/airflow-service/airflow/listeners" ":(exclude)/d/hdl5/airflow-service/airflow/models" ":(exclude)/d/hdl5/airflow-service/airflow/security" ":(exclude)/d/hdl5/airflow-service/airflow/timetables" ":(exclude)/d/hdl5/airflow-service/airflow/www" ":(exclude)/d/hdl5/airflow-service/airflow/callbacks" ":(exclude)/d/hdl5/airflow-service/airflow/contrib" ":(exclude)/d/hdl5/airflow-service/airflow/example_dags" ":(exclude)/d/hdl5/airflow-service/airflow/notifications" ":(exclude)/d/hdl5/airflow-service/airflow/providers" ":(exclude)/d/hdl5/airflow-service/airflow/sensors" ":(exclude)/d/hdl5/airflow-service/airflow/task" ":(exclude)/d/hdl5/airflow-service/airflow/triggers" ":(exclude)/d/hdl5/airflow-service/airflow/api" ":(exclude)/d/hdl5/airflow-service/airflow/cli" ":(exclude)/d/hdl5/airflow-service/airflow/jobs" ":(exclude)/d/hdl5/airflow-service/airflow/macros" ":(exclude)/d/hdl5/airflow-service/airflow/operators" ":(exclude)/d/hdl5/airflow-service/airflow/template" | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
git log --author="xuhui" --pretty=tformat: --numstat -- . ":(exclude) d:/hdl5/airflow-service/airflow/api_connexion" | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
```
```一、统计某项目中历史提交成员数量
printf "\n1. 项目成员数量:"; git log --pretty='%aN' | sort -u | wc -l
结果:
1. 项目成员数量: 39
1
二、按用户名统计代码提交次数
printf "\n\n2. 按用户名统计代码提交次数:\n\n"
printf "%10s %s\n" "次数" "用户名"
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5
printf "\n%10s" "合计";
printf "\n%5s" ""; git log --oneline | wc -l
结果:
次数 用户名
473 wu
345 aj
317 al
237 hq
219 to
合计
三、按用户名统计代码提交行数
printf "\n3. 按用户名统计代码提交行数:\n\n"
printf "%25s +s = +s - %18s\n" "用户名" "总行数" "添加行数" "删除行数"
git log --format='%aN' | sort -u -r | while read name; do printf "%25s" "$name"; \
git log --author="$name" --pretty=tformat: --numstat | \
awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "%15s %15s %15s \n", loc, add, subs }' \
-; done
printf "\n%25s " "总计:"; git log --pretty=tformat: --numstat | \
awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "%15s %15s %15s \n", loc, add, subs }'
```