[英]Export subtree in Git with history
我的 Git 存儲庫中有一個文件夾,我想將其移到自己的存儲庫中。 是否可以隨文件夾移動該文件夾的歷史記錄?
我以前一直在做一個git rm -r --cached subfolder/
然后在子文件夾上執行git init
。 但是,歷史不會導入到新存儲庫中。
引用git-filter-branch(1)
一個例子
重寫存儲庫以使其看起來好像 foodir/ 一直是它的項目根目錄,並丟棄所有其他歷史記錄:
git filter-branch --subdirectory-filter foodir -- --all
例如,您可以將庫子目錄轉換為它自己的存儲庫。 請注意將過濾器分支選項與修訂選項分開的 --,以及用於重寫所有分支和標簽的 --all。
對於 2021 年(或之后)來到這里的其他人,核心 git 團隊不強烈推薦 git filter-branch。
警告 git filter-branch 有很多陷阱,可能會對預期的歷史重寫產生不明顯的破壞(並且由於它的性能如此糟糕,因此您幾乎沒有時間調查這些問題)。 這些安全和性能問題無法向后兼容修復,因此不推薦使用它。 請使用替代的歷史過濾工具,例如 git filter-repo。 如果您仍然需要使用 git filter-branch,請仔細閱讀 SAFETY (and PERFORMANCE) 以了解 filter-branch 的地雷,然后警惕地盡可能多地避免其中列出的危害。
相反,推薦的工具是git-filter-repo ,它可以輕松解決此問題
git filter-repo --path subfolder/ # Filter the repo to just the sub-dir
git mv subfolder/* . # Move the contents of the sub-dir to the root
git commit -m "Filtered from main repo"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.