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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With