簡體   English   中英

了解為什么 git-filter-branch 沒有清理我的歷史記錄

[英]Understanding why git-filter-branch is not cleaning my history

我使用 gitleaks 檢查我的回購歷史中泄露的秘密。 當我運行以下命令並強制推送時

git filter-branch --force --index-filter \
  'git rm -r --cached --ignore-unmatch terra/fixtures.go' \
  --prune-empty --tag-name-filter cat -- --all

它似乎工作,除了我注意到以下幾點:

WARNING: Ref 'refs/heads/automate_tests' is unchanged
WARNING: Ref 'refs/heads/ethRawTransaction' is unchanged
WARNING: Ref 'refs/heads/feature/177/leave-bastion' is unchanged
WARNING: Ref 'refs/heads/feature/FAQ' is unchanged
WARNING: Ref 'refs/heads/master' is unchanged
WARNING: Ref 'refs/heads/mjolnir' is unchanged
WARNING: Ref 'refs/heads/tmp' is unchanged
WARNING: Ref 'refs/remotes/origin/master' is unchanged
WARNING: Ref 'refs/remotes/origin/automate_tests' is unchanged
WARNING: Ref 'refs/remotes/origin/bug/0.0.11-beta-fix' is unchanged
WARNING: Ref 'refs/remotes/origin/bug/bastion-ssh' is unchanged
WARNING: Ref 'refs/remotes/origin/bug/fix-examples-merge' is unchanged
WARNING: Ref 'refs/remotes/origin/develop' is unchanged
WARNING: Ref 'refs/remotes/origin/ethRawTransaction' is unchanged
WARNING: Ref 'refs/remotes/origin/feature/168/auto-ssh-to-bastion' is unchanged
WARNING: Ref 'refs/remotes/origin/feature/169/ethstats_for_pantheon' is unchanged
WARNING: Ref 'refs/remotes/origin/feature/175/ssh-to-certain-nodes' is unchanged
WARNING: Ref 'refs/remotes/origin/feature/176/tagging-nodes-to-ips' is unchanged
WARNING: Ref 'refs/remotes/origin/feature/177/leave-bastion' is unchanged
WARNING: Ref 'refs/remotes/origin/feature/FAQ' is unchanged
WARNING: Ref 'refs/remotes/origin/feature/README' is unchanged
WARNING: Ref 'refs/remotes/origin/master' is unchanged
WARNING: Ref 'refs/remotes/origin/mjolnir' is unchanged
WARNING: Ref 'refs/remotes/origin/tmp' is unchanged
WARNING: Ref 'refs/tags/0.0.4' is unchanged
WARNING: Ref 'refs/tags/20190820141131-866368a' is unchanged
WARNING: Ref 'refs/tags/20190820142202-bd96767' is unchanged
WARNING: Ref 'refs/tags/20190820143451-fc7f46a' is unchanged
WARNING: Ref 'refs/tags/20190820143903-832818a' is unchanged
WARNING: Ref 'refs/tags/20190820150546-05e3105' is unchanged
WARNING: Ref 'refs/tags/20190820154631-da0cdab' is unchanged
WARNING: Ref 'refs/tags/20190820160956-047caa6' is unchanged
WARNING: Ref 'refs/tags/20190820162243-a300fa5' is unchanged
WARNING: Ref 'refs/tags/20190820170410-47f8878' is unchanged
WARNING: Ref 'refs/tags/untagged-f148f02c4d71ed0bea99' is unchanged
WARNING: Ref 'refs/tags/v.0.0.1' is unchanged
WARNING: Ref 'refs/tags/v0.0.1' is unchanged
WARNING: Ref 'refs/tags/v0.0.1-alpha' is unchanged
WARNING: Ref 'refs/tags/v0.0.10' is unchanged
WARNING: Ref 'refs/tags/v0.0.11-beta' is unchanged
WARNING: Ref 'refs/tags/v0.0.14' is unchanged
WARNING: Ref 'refs/tags/v0.0.3-alpha' is unchanged
WARNING: Ref 'refs/tags/v0.0.4-chaos-poc' is unchanged

結果,泄漏的數量似乎並沒有下降。

我對為什么會發生這種情況感到困惑,並希望得到任何指示。

git filter-branch報告為未更改的參考文獻在其歷史記錄中的任何位置都沒有名為terra/fixtures.go的文件。 Filter-branch 通知您,盡管您要求它更新這些分支名稱以指向任何復制的提交,但在此過程中實際上沒有復制任何提交。

找到具有此類文件的可到達提交hash ID 的列表,然后在此類 hash ID 上運行git branch --contains可能會很有趣。 見下文。

哪些提交包含文件F?

請注意,這是對不同問題的不同答案。 它也不是尋找修改了某些路徑名的提交,而是尋找根本存在某些路徑名的提交。

我們首先使用git rev-list列出所有提交:

git rev-list --all |

git rev-list中的 output 只是每個提交 hash ID 的列表,可以從命名的修訂版訪問。 在這種情況下, --all命名所有分支和標簽,以及其他引用,例如refs/stash ,但不命名任何 reflog 條目。

然后,對於列出的每個提交,我們要測試此提交是否包含命名文件。 此時您通常需要很多可編程性。 例如,假設文件名是a/b/c.txt 您還想找到A/B/C.TXT嗎? 如果您使用的是 Windows 或 MacOS,則可能。 如果您使用的是 Linux,可能不是。 或者,也許您想查找名稱以某種模式開頭或結尾的任何文件。

我們在這里要做的是使用git ls-tree -r ,它列出了所有文件名,然后通過搜索和狀態命令運行它們,例如grep 請注意, grep搜索的是正則表達式,而不是 glob 模式,因此a*b表示零個或多個a字符后跟一個b字符,並將匹配字符串“abc.txt”、“b”、“flobby”等:這些都有零個或多個a s 后跟 a b 我們將讓實際匹配的名稱顯示出來,以便人們可以在需要時應用進一步的過濾:

git rev-list --all |
    while read hash; do
        git ls-tree -r $hash > /tmp/files
        if grep -s 'terra/fixtures\.go' /tmp/files; then
            echo "commit ${hash} :"
            grep 'terra/fixtures\.go' /tmp/files
        fi
    done
rm /tmp/files

這組命令的 output(您可能應該將其放入文件中,而我尚未測試過並且可能包含錯誤)是提交 hash ID 的列表,適用於提取,但后面還有匹配的名稱:您可能應該丟棄匹配例如sputerra/fixtures.gobble

(可以編寫更精確匹配的更高級的grep模式。在這種情況下,用^$錨定正則表達式就足夠了。在更復雜的情況下,需要更復雜的正則表達式。我把這個留給使用代碼的人。 )

獲得 hash IDs—運行上述並重定向到文件,清理文件,然后提取更有趣的 hash IDs—然后您可以執行以下操作:

git branch --contains <hash>

在任何給定的提交 hash 上查看哪些分支包含該特定提交。 請注意,可能有零個或多個分支包含任何給定的提交。 對於(更多)更多信息,請閱讀並理解Think Like (a) Git

嘗試使用雙引號

git filter-branch --force --index-filter \
  "git rm -r --cached --ignore-unmatch 'terra/fixtures.go'" \
  --prune-empty --tag-name-filter cat -- --all

嘗試使用新的git filter-repo替代舊的git filter-branch或 BFG

git filter-repo --use-base-name --path terra/fixtures.go --invert-paths

默認情況下,這個新命令適用於所有分支。 然后是git push --all --force ,以覆蓋遠程存儲庫的歷史記錄。

暫無
暫無

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

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