【问题标题】:How to modify remote history with git filter-repo?如何使用 git filter-repo 修改远程历史记录?
【发布时间】:2021-02-25 17:37:49
【问题描述】:

过去我曾使用git filter-branch 从我的 git 历史记录中删除文件。之后,我可以强制推送来更新远程存储库。例如,从本地 repo 中删除所有 HTML 文件,然后重写远程以反映更改:

$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch -r \*.html' --prune-empty -- --all
$ git push origin --force --all

这工作得很好。但是看到filter-branch 非常慢并且已经被弃用了一段时间,我想用git-filter-repo 来代替。到目前为止,这似乎是等效的命令:

$ git-filter-repo --force --path-glob *.html --invert-paths

这第一步似乎奏效了。我的问题是当我之后尝试强制推送时,我看到我的遥控器丢失了。

$ git push origin --force --all
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

当我检查git remote -v 时,filter-repo 命令似乎删除了我的远程 URL。手动加回遥控器会让我陷入其他设置无效的兔子洞。

为什么git-filter-repo 会删除我的遥控器?以及如何使用 git-filter-repo 重写遥控器的历史记录,就像使用 git filter-branch 一样?

【问题讨论】:

  • 有类似问题的朋友,可能需要将.git/config中的远程URL配置备份到git filter-repo之前的其他地方,以备日后恢复。这在 Github docs 中有提及

标签: git git-filter-branch git-filter-repo


【解决方案1】:

为什么 git-filter-repo 会删除我的遥控器?

The documentation 在标题为INTERNALS 的部分中解释了他们的推理:

  1. 我们不希望用户意外推回原始存储库,如 DISCUSSION 中所述。它还提醒用户,由于历史已被改写,此 repo 不再与原版兼容。最后,另一个小好处是这允许用户使用--mirror 选项推送到他们的新家,而不会意外发送远程跟踪分支。

我怎样才能像使用 git filter-branch 一样使用 git-filter-repo 重写远程的历史记录?

只需使用git remote addorigin 放回去,或者——因为命令序列中的第3 步是git remote rm origin——只需重命名 origin 到其他名称。但是,如果您这样做,请注意第 2 步。

你说:

手动加回遥控器会让我陷入其他设置无效的兔子洞。

显然,您需要在此处详细说明。

【讨论】:

    【解决方案2】:

    最近在 git 上工作。 遇到同样的问题。

    运行 git-filter-repo 后,我的“git branch -r”什么也没返回。 要恢复输出, 你需要:

    git remote add origin xxxxxxxxxxx
    git fetch --all
    

    然后,您将获得“git branch -r”输出。

    【讨论】:

      猜你喜欢
      • 2020-10-23
      • 2020-05-30
      • 2022-08-20
      • 2013-01-20
      • 2013-04-25
      • 2015-06-04
      • 2010-09-06
      • 1970-01-01
      • 2011-05-27
      相关资源
      最近更新 更多