簡體   English   中英

Git log - 如何只檢索git歷史記錄中每個文件的最新提交,其中每個提交只更改一個文件

[英]Git log — how to retrieve only the latest commits of every file in the git history, where each commit only changes one file

Arr matey!

假設我有2個文件foo.txtbar.txt

假設我每次提交git時,都只允許更改其中一個文件。 因此,我有一個提交歷史記錄,其中每個提交對應於foo.txtbar.txt的更改,但不是兩者。 即1提交​​= 1個文件更改。

現在讓我們說我到目前為止有4個提交,每個文件有2個提交。 當我做git log --oneline我得到以下結果:

hash1 latest changes in foo.txt
hash2 changes in foo.txt
hash3 latest changes in bar.txt
hash4 changes in bar.txt

注意:git log首先返回最新提交,因此hash1 commit是foo.txt的最新提交, hash3bar.txt

現在我想要做的是使用git log返回每個文件的最新提交,按照標題。 是否有開箱即用的過濾器標志來做到這一點? 即我希望在這種情況下的輸出是:

hash1 latest changes in foo.txt
hash3 latest changes in bar.txt

在真實的東西,我有成千上萬的文件,而不僅僅是兩個。

我不認為使用一個命令是可能的,但當然可能有兩個命令:

git log -1 --grep=foo
git log -1 --grep=bar

創建別名或shell腳本以輕松調用這兩個命令。

您可以使用:

git log --grep=bar.txt -1 --oneline && git log --grep=foo.txt -1 --oneline

如果您有許多文件,則可以添加更多--grep選項。

對於數千個文件,您可以使用shell腳本(例如git repo中的test.sh):

#!/bin/sh

for file in $(git ls-files)
do
{
line=$(git log --grep=$file -1 --oneline 2>&1)
echo $line
}
done

然后你可以通過./test.sh執行腳本。

你可以做:

git log --oneline -1 <PATH>

結合GNU版本的xargs ,可能是:

git ls-files -z | xargs -0 -L 1 git log --oneline -1

此命令將遍歷給定存儲庫中的所有跟蹤文件,並對每個文件執行上述命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM