从所有git历史记录中移除文件

7

根据这篇文章,我创建了一个小脚本,旨在删除整个git库中所有分支、标签和提交中文件的所有出现。

#!/usr/bin/env node
var child_process = require('child_process');
if (process.argv.length < 3){
  console.error('USAGE: git-forget path/to/file')
  process.exit(1);
}
var path = process.argv[2];

var phase = 0;
function printLog(error, stdout, stderr) {
  if (error) {
    console.error('ERROR' + error);
  }
  console.log(++phase);
  console.log(stdout);
}

child_process.execSync('git filter-branch --force --index-filter \'git rm -f --cached --ignore-unmatch  '+ path +'\' --prune-empty --tag-name-filter cat -- --all');
child_process.execSync('echo "' + path + '" >> .gitignore', printLog);
child_process.execSync('git add .gitignore');
child_process.execSync('git commit -m "Add ' + path +' to .gitignore"',printLog)
child_process.execSync('git push origin --force --all',printLog);
child_process.execSync('git push origin --force --tags',printLog);

这个脚本在一些私有的代码库上运行成功,但是在一个特定的代码库中,它保留了我试图移除的文件的初始提交。运行脚本后,我执行了这个命令git log --all -- .npmrc并找到了初始提交。我错过了什么吗?


尝试使用BFG,本地可以工作但不会更新远程。 - qballer
@qballer,然后强制推送您的更改到远程? - Chris
@chris 不,它说一切都是最新的。我认为这与关键文件在最初提交中引入有关。 - qballer
1个回答

5
我认为发生的情况是您忘记告诉此存储库的其他用户不要将其更改合并到新历史记录中,而应该使用rebase。从您引用的文档中可以看出:
告诉您的协作者对他们从旧(污染的)存储库历史记录创建的任何分支进行rebase,而不是合并。一个合并提交可能会重新引入您刚刚努力清除的某些或所有污染历史记录。
尝试再次运行相同的脚本,并确保没有人通过将其本地更改合并到新头部来重新引入旧历史记录。

虽然你似乎是对的,但我不知道如何解决这个问题。 - qballer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接