【譯】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

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

(感謝你的閱讀)

參考資料

[1]

Ten Useful Git Log Tricks | Hacker Noon: https://hackernoon.com/ten-useful-git-log-tricks-7nt3yxy

[2]

KIWI 的碎碎念: https://blog.coder4j.cn/


本文分享自微信公衆號 - KIWI的碎碎念(kiwiflydream)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章