反复使用git-filter-branch重写新的提交

时间:2010-02-19 11:55:31

标签: git git-filter-branch

我想将使用更大应用程序分发的模块拆分为单独的子模块,并保持从上游拉出的能力。

所以这比Detach subdirectory into separate Git repository更复杂。我不仅一次使用git-filter-branch,而且希望在我完成之后保持上行更改的能力(而上游没有)。

简单地从上游重新运行完整历史记录中的git-filter-branch,包括在我的重写历史记录中找不到的新提交不是一个选项,因为有数百个模块我必须这样做,提交的数量正在增加接近100.000。

我猜这涉及将历史限制为新的提交,重写那些然后在之前重写的提交之后添加它们,但我不确定如何做到这一点 - 也许有更好的方法。

如果分支和标签也可以被保留,那将是很好的,但这不是绝对必要的,如果它使事情变得复杂,我实际上宁愿失去那些。

1 个答案:

答案 0 :(得分:8)

对于第一个rebase,请执行以下操作:

git checkout -b rebased master
git filter-branch --some-filter
git tag rebased-done master

以后“合并”提交:

# 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