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

qba*_*ler 7 git node.js git-rewrite-history

基于这篇文章,我创建了一个小脚本,它应该删除整个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);
Run Code Online (Sandbox Code Playgroud)

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

Ami*_*rad 5

我想发生的事情是你忘了告诉其他用户这个回购不要他们的更改合并到新的历史记录中,而是其更改为rebase.从你引用的文件中:

告诉您的协作者重新绑定,而不是合并他们创建的旧(受污染)存储库历史记录中的任何分支.一次合并提交可以重新引入一些或所有被污染的历史记录.

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