过去我曾经使用 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 filter-repo
命令之前,在 .git/config 文件中备份远程 URL 配置,并将其存储在其他地方以供以后恢复。这在 Github 的文档中有提到。 - machine-building