乐趣区

关于java:译10-个有用的-git-log-技巧

原作者: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.

(这条命令能够帮忙你以简洁的形式查看提交记录。它把提交记录稀释在了一行,而且只保留相似较短的提交哈希值和提交信息)

git log --oneline

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.

(这条命令将显示带变更差别信息的提交记录。所以你能够很分明的晓得每次提交都有哪些变更。)

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

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

(你兴许须要在曾经增加到提交历史的源码中查问指定字符串。这可能是如下的)

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 来设置大小写不敏感的。并且同时显示差别变更的内容)

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

Show only merge commits

(只显示合并提交)

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

(这条命令帮忙咱们晓得以后分支的合并状况)

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.

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

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,

(例如,)

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

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 :) :)

(感激你的浏览)

退出移动版