Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove file from all git history

Based on this article, I've created a small script which is supposed to remove all occurrences of a file in the entire git repo, all branches, tags and commits. The script:

#!/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);

This script worked on a few repos (which are private) and on a specific one it kept the initial commit to the file I was trying to remove. After the script was run I did this git log --all -- .npmrc and found the initial commit. What am I missing?

like image 301
qballer Avatar asked Mar 02 '16 19:03

qballer


People also ask

How do I remove files from my entire git history?

To entirely remove unwanted files from a repository's history you can use either the git filter-repo tool or the BFG Repo-Cleaner open source tool. The git filter-repo tool and the BFG Repo-Cleaner rewrite your repository's history, which changes the SHAs for existing commits that you alter and any dependent commits.

How do I delete files from git?

The git rm command can be used to remove individual files or a collection of files. The primary function of git rm is to remove tracked files from the Git index. Additionally, git rm can be used to remove files from both the staging index and the working directory.


1 Answers

I think what happened is that you forgot to tell other users of this repo not to merge their changes to the new history, but rather to rebase. From the document you quoted:

Tell your collaborators to rebase, not merge, any branches they created off of your old (tainted) repository history. One merge commit could reintroduce some or all of the tainted history that you just went to the trouble of purging.

Try running the same script again, and see that no one re-introduces the old history by means of merging his/her local changes to the new head.

like image 129
Amir Arad Avatar answered Sep 18 '22 07:09

Amir Arad