image.png

原作者:Srebalaji 原文地址:Ten Useful Git Log Tricks | Hacker Noon 译者:KIWI的碎碎念

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.
  
  (这条命令可以帮助你以简洁的方式查看提交记录。它把提交记录浓缩在了一行,而且只保留类似较短的提交哈希值和提交信息)
  

  1. 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 会筛选出指定时间之前的提交记录。)

  1. git log --after="2020-05-15"

// 译者注:注意原文这里的日期格式是 2020-15-05 ,年日月?我本地测试时间不对,不知道是不是跟原作者配置或环境不一样,大家可以试一下自己本地可以支持哪种格式,后文的日期格式都是如此,不再赘述。

The above command will show only commits after May 15th, 2020

(上面这条命令将只显示 2020 年 5 月 15 日 之后的提交记录)

  1. 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

(你也可以使用如下的日期格式)

  1. git log --after="yesterday" // shows only commits from yeserday
  2. (只显示昨天之后的提交记录)
  3. git log --after="today" // shows only today commits
  4. (只显示今天的提交记录)
  5. git log --before="10 day ago" // omits last 10 days commits
  6. (只显示 10 天前的提交记录)
  7. git log --after="1 week ago" //show only commits from last week
  8. (只显示最近一周的提交记录)
  9. git log --after="2 week ago"
  10. (只显示最近两周的提交记录)
  11. git log --after="2 month ago" // shows only last 2 months commits
  12. (只显示最近两个月的提交记录)

git log with diff changes

(带变更差异信息的 git 日志)

  1. 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

(根据作者筛选提交记录)

  1. 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 日志是支持多选项的,所以你可以按你的需要自由组合选项。例如:)

  1. 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 支持通过正则表达式去查询日志消息并且显示所有匹配的提交记录)

  1. 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 参数)

  1. 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 的正式表达式查询提交记录)

  1. 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.

(有时候你需要一些指定的变更文件的提交记录。这在很多地方都有使用。)

  1. git log main.rb

This command will filter commits that made changes to the respective file.

(这条命令将按相应的文件过滤提交记录)

You can also pass multiple files to it.

(你也可以同时过滤多个文件)

  1. 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.

(记得你也可以通过多个选项去使用。)

  1. git log -i --grep="fix " main.rb search.rb

This command will filter out commits changes done to the specified files and also will match the log message by the given search pattern.

(这条命令将通过指定的文件和指定的日志消息的正则表达式去过滤提交记录)

Filter commits by file content

(根据文件内容过滤提交记录)

You may need to search for a specific string in the source code that has been added in the commit history. This can be possible by

(你也许需要在已经添加到提交历史的源码中查询指定字符串。这可能是如下的)

  1. git log -S"function login()"

The above command will search for the string “function login()”. By default, it’s case sensitive.

(上面这条命令将通过字符串 『function login()』查询。它默认是大小写敏感的)

You can make it case-insensitive by adding -i. And to view the content you can view the diff changes.

(你也可以通过添加 -i 来设置大小写不敏感的。并且同时显示差异变更的内容)

  1. git log -i -S"function login()" -p

Show only merge commits

(只显示合并提交)

This command helps us in knowing the merges done to the current branch.

(这条命令帮助我们知道当前分支的合并情况)

  1. git log --merges

The above command will show only the merge commits in the current branch. Nothing more.

(上面这条命令只显示当前分支的合并提交记录,仅此而已)

Showing diff between branches

(显示两个分支的差异)

We have already seen this command in one of our previous issues.

(在先前的问题中,我们已经见过这个命令了)

  1. git log master..develop

This command will help you show all the commits from develop but that are not present in the master branch. In this way, you can know that how many new commits are added to the develop branch that is not present in the master branch. And make sure you have the updated changes in the local before comparing.

(这条命令将帮助我们查看所有在 develop 分支中的但又不在 master 分支的提交记录。通过这个方法,你可以知道在 develop 分支上有多少新的提交,但是 master 又不存在的。确保你在比较前,本地有修改的变更。)

Custom formatting log messages

(自定义日志消息的格式)

Git also provides options to custom format our log messages. You can check out custom pretty options for more options.

(Git 也提供了自定义格式化日志消息的选项。你可以查看自定义的 pretty 选项的更多选项)

For example,

(例如,)

  1. git log --pretty=format:"%Cred%an - %ar%n %Cblue %h -%Cgreen %s %n"

image.png

You can see in the above image that the commit logs are custom formatted. It’s pretty easy and it comes in handy if you want to view only specific details of the log.

(你可以看到上面的图片的提交记录是自定义格式的。如果你只想查询日志的特定信息,它是非常简单与容易的。)

That’s it. Hope you learned something new :)

(就这样了,希望你学到了一些新的东西😀)

Thank you for reading :) :)

(感谢你的阅读)