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

时间:2016-03-02 19:03:10

标签: node.js git git-rewrite-history

基于这个article,我创建了一个小脚本,它应该删除整个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);

这个脚本适用于一些repos(它们是私有的),并且在特定的一个上它保留了我试图删除的文件的初始提交。脚本运行后,我执行了此git log --all -- .npmrc并找到了初始提交。我错过了什么?

1 个答案:

答案 0 :(得分:5)

我认为发生的事情是您忘记告诉其他用户此回购不是合并他们对新历史记录的更改,而是 rebase 。 从您引用的文件中:

  

告诉你的合作者,他们要改变任何分支,而不是合并   从旧的(受污染的)存储库历史记录中创建。一次合并提交   可以重新引入你刚才的部分或全部污染历史   去了清洗的麻烦。

尝试再次运行相同的脚本,并看到没有人通过将他/她的本地更改合并到新头来重新引入旧历史记录。