【问题标题】:Repeatedly using git-filter-branch to rewrite new commits反复使用 git-filter-branch 重写新的提交
【发布时间】:2010-02-19 11:55:31
【问题描述】:

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

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

现在只需在上游的完整历史记录上重新运行 git-filter-branch,包括在我重写的历史记录中找不到的新提交,这不是一种选择,因为我必须为数百个模块执行此操作,并且提交的数量正在增加接近 100.000。

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

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

【问题讨论】:

  • 我也想知道这个。每次我拉一个依赖仓库时,我都必须再次运行 filter-branch 以将更新合并到我的项目中(我不想合并整个仓库)。
  • 我很惊讶没有人想出答案,毕竟这看起来像是一个有趣的挑战。好吧,我确实自己破解了一些东西,但忘了在这里发布。您的兴趣提醒了我 - 以下是我的解决方案。
  • 你看过git subtree吗?它可以将一个子树拆分为一个新分支,并使用--rejoin 选项以增量方式进行。
  • 是的[编辑:uuuh no:)]。不幸的是,在我的情况下,属于不同“模块”的文件大多位于顶级目录而不是子目录中。我将进一步研究 git-subtree ——也许可以对其进行更改以支持我的用例。

标签: git git-filter-branch


【解决方案1】:

对于第一个变基,这样做:

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

【讨论】:

    猜你喜欢
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多