【译】10 个最有用的 git log 技巧

image.png
原作者:Srebalaji 原文地址: Ten Useful Git Log Tricks | Hacker Noon[1] 译者: KIWI 的碎碎念[2]
If you are using Git for a while you should have come across git log. As everyone knows, the git log is a simple command that helps us to view the changes or project history.
(如果你在使用 Git 一段时间后,应该会遇到访问 git 日志的场景。众所周知,git log 是一个帮助我们查看项目变更或项目历史的简单命令。)
Even with that simplicity, it is a very powerful tool and it comes with numerous options that help us to view the project changes and its structure. We will see some of the most used options in this article.
(尽管它是非常简单的,但是它是一个非常强大的命令工具,可以通过它提供的数量众多的选项来帮助我们去查看项目的变更与结构。在这篇文章里,我们将看到一些它最常使用的选项)
git log —oneline
This command helps you to view the commits in a cleaner way. It condenses each commit to a line and has only minimal information like shorter commit hash, commit message.
(这条命令可以帮助你以简洁的方式查看提交记录。它把提交记录浓缩在了一行,而且只保留类似较短的提交哈希值和提交信息)
git log --oneline

image.png
Filter commits by time period
(根据时间段筛选提交信息)
These commands will filter out the commits by the given time period. For example, — after will only filter commits after the given time period and — before will only filter commits before the given time period.
(这些命令可以筛选出指定时间段的提交记录,例如, --after 会筛选出指定时间之后的提交记录,**--before ** 会筛选出指定时间之前的提交记录。)
git log --after="2020-05-15"
// 译者注:注意原文这里的日期格式是 2020-15-05 ,年日月?我本地测试时间不对,不知道是不是跟原作者配置或环境不一样,大家可以试一下自己本地可以支持哪种格式,后文的日期格式都是如此,不再赘述。
The above command will show only commits after May 15th, 2020
(上面这条命令将只显示 2020 年 5 月 15 日 之后的提交记录)
git log --after="2020-05-15" --before="2020-05-25"
The above command will show only commits from May 15 to May 25
(上面这条命令将只显示 5 月 15 号到 5 月 25 日之间的提交记录)
You can also use the following date formats
(你也可以使用如下的日期格式)
git log --after="yesterday" // shows only commits from yeserday
(只显示昨天之后的提交记录)
git log --after="today" // shows only today commits
(只显示今天的提交记录)
git log --before="10 day ago" // omits last 10 days commits
(只显示 10 天前的提交记录)
git log --after="1 week ago" //show only commits from last week
(只显示最近一周的提交记录)
git log --after="2 week ago"
(只显示最近两周的提交记录)
git log --after="2 month ago" // shows only last 2 months commits
(只显示最近两个月的提交记录)
git log with diff changes
(带变更差异信息的 git 日志)
git log -p
This command will show the log with the diff changes. So that you can know the changes done in each commit.
(这条命令将显示带变更差异信息的提交记录。所以你可以很清楚的知道每次提交都有哪些变更。)

image.png
In the above image, you can see the git diff changes.
(在上面的图片里,你可以看到 git 的变更差异信息)
Filter commits by author
(根据作者筛选提交记录)
git log --author="Srebalaji"
The above command will filter out the commits done by the particular author. Note that the Git filters out by regex pattern. So don’t worry about the exact name match or case sensitivity.
(上面这条命令可以通过指定的作者名称过滤提交信息。注意 git 是通过正则表达式进行过滤的。所以不用担心精确的名称匹配或大小写敏感的匹配)
Git log can take multiple options so you can combine options for your need. For example,
(Git 日志是支持多选项的,所以你可以按你的需要自由组合选项。例如:)
git log --after="1 week ago" --author="srebalji" -p
The above command will filter commits for the past week by the respective author and also shows the diff changes.
(上面这条命令将过滤出最近一周相应作者的提交记录并且同时显示变更差异信息)
Filter commits by log messages
(根据日志提交信息过滤提交记录)
Sometimes, you need to filter commits by log messages. Git accepts a regex pattern to search for the log messages and displays all the matched commits.
(有时候,你需要根据日志信息过滤提交记录。Git 支持通过正则表达式去查询日志消息并且显示所有匹配的提交记录)
git log --grep="ISSUE-43560"
The above command will filter commits by the respective pattern. And remember by default it’s case sensitive.
(上面这条命令将根据相应的正则表达式过滤提交记录。需要知道的是,它默认是大小写敏感的。)
To make the search case insensitive, you can pass -i parameter
(如果想使用大小写不敏感查询,你可以通过 -i 参数)
git log -i --grep="issue-43560"
The below command is using a regex pattern search and will search for both the issue ids.
(下面这条命令通过两条 issue ids 的正式表达式查询提交记录)
git log -i --grep="issue-43560\|issue-89786"
Filter commits by files
(通过文件过滤提交记录)
Sometimes you need all commits changes that have affected some particular files. This will come in hand in many places.
(有时候你需要一些指定的变更文件的提交记录。这在很多地方都有使用。)
git log main.rb
This command will filter commits that made changes to the respective file.
(这条命令将按相应的文件过滤提交记录)
You can also pass multiple files to it.
(你也可以同时过滤多个文件)
git log main.rb search.rb login.rb
You can see I have passed three files to filter out.
(你能看到我通过三个文件去过滤提交记录。)
Remember you can also pass multiple options.
(记得你也可以通过多个选项去使用。)
git log -i --grep="fix " main.rb search.rb