我想将与更大的应用程序一起分发的模块拆分成单独的子模块,并保持从上游 pull 的能力。
所以这比 Detach subdirectory into separate Git repository 更复杂.我不仅曾经使用过 git-filter-branch 一次,而且还想在我这样做之后保留 pull 上游更改的能力(而上游没有)。
简单地在上游的完整历史上重新运行 git-filter-branch 现在包括在我重写的历史中没有找到的新提交不是一个选项,因为有数百个模块我必须这样做并且提交的数量越来越多接近 100.000。
我猜这涉及到将历史记录限制为仅新提交、重写那些然后在先前重写的提交之后添加它们,但我不确定如何执行此操作 - 也许有更好的方法。
如果分支和标签也能被保留就好了,但这不是绝对必要的,如果它使事情复杂化,我实际上宁愿失去它们。
最佳答案
对于第一个 rebase 执行此操作:
git checkout -b rebased master
git filter-branch --some-filter
git tag rebased-done master
并“merge ”以后的提交:
# Create a tempory branch and rebase it's tail use 'rebase-done~'
# and not 'rebase-done' because some filters (like --index-filter)
# require this, others might not.
git checkout -b rebased-tail master
git filter-branch -f --some-filter -- rebased-done~..HEAD
# Get the commit in branch 'rebased' corresponding to tag 'rebase-done'
# (which tags a commit in 'master' not 'rebased'). Depending on your
# situation you might have to determine this commit differently (in my
# use case I am absolutely sure that there is never a commit with the
# same author date - if that doesn't work you might want to compare
# commit messages).
start_time=$(git show --quiet --pretty=%at rebased-done)
start_hash=$(
git log --reverse --pretty="%H %at" rebased_tail |
while read hash time
do
[ "$time" = "$start_time" ] && echo $hash && break
done
)
# Finally apply the rebased commits.
git checkout rebased
git format-patch -k --stdout $start_hash..rebased-tail | git am -k
git branch -D rebased-tail
git tag -f rebased-done master
关于git - 反复使用 git-filter-branch 重写新提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2296047/