[英]Pull one file out of a series of commits, into its own branch, preserving git history
我有一個分支feature/10/
,其中包含一個正在開發的具有許多文件的新項目,分支foo.py
中的腳本之一已變成它自己的切向側項目,不應放在feature/10/
分支,但現在應該進入feature/11/
分支,完成后再拉回master。 foo.py
依賴於master中的文件,但不與feature/10/
任何新文件交互或依賴。
有沒有辦法讓我創建這個新分支,將其復制到foo.py
但別無其他,並保留foo.py
的git歷史記錄?
我顯然可以創建一個新分支,然后手動復制並粘貼該文件,但是這感覺與git的精神foo.py
,因為它切斷了foo.py
在修訂歷史中的未來發展。
(您實際上並未在分支中命名帶有反斜杠的名稱,對嗎?我將假設正斜杠。)
git filter-branch可以做到這一點。
git filter-branch $(git merge-base feature/10 feature/11)..feature/11 \
--index-filter '
BASE=$(git merge-base feature/10 feature/11)
# Save the current state of foo.py
FOO=$(git ls-files --stage -- foo.py)
read -r mode object stage file <<<${FOO}
# Set the entire tree back to the base state
git read-tree ${BASE}^{tree}
# Add the current foo.py to the tree
git update-index --cacheinfo "${mode},${object},foo.py"
' \
--prune-empty
注意,這將重寫feature/11
分支。 您可以在重寫之前找到該版本為original/feature/11
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.