Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permanently removing a file from git history

Tags:

git

github

Let's say i have done 5 commits in my git project.

At Commit 4 i accidentally added my credentials.json file and pushed to repo.
At Commit 5 i removed the file and did a commit and pushed this change to repo.

Currently the file is not showing in latest commit. But if a user checks commit history, he can go to commit 4 and check the contents of my file.

How can i permanently remove the file from commit history so that it looks like the file was never added to the repo.

Note:-
i have already tried below command:-

git filter-branch --force --index-filter \
  "git rm --cached --ignore-unmatch Credentials.json" \
  --prune-empty --tag-name-filter cat -- --all

It does not throw any error but the file is not removed from History.

Actually the file is inside /src folder. But the strange thing is that when i give the below command (including the full path):-

git filter-branch --force --index-filter   "git rm --cached --ignore-unmatch ~full-path/src/Credentials.json"   --prune-empty --tag-name-filter cat -- --all

I get error as:- ~full-path/src/Credentials.json is outside repository.

Is it because i have already removed my file in commit 5? What can i do in this scenario?
Is creating a new repo from scratch the only option?

like image 811
Akshunya Avatar asked Dec 28 '25 15:12

Akshunya


1 Answers

WARN: Always make a backup of the original repo before you start following operations.


No need to create a new repo. git filter-branch works.

Don't use full path in git filter-branch, use the relative path src/Credentials.json.

git filter-branch --force --index-filter "git rm --cached --ignore-unmatch src/Credentials.json" --prune-empty --tag-name-filter cat -- --all

BTW, bfg is much easier to use for a newbie. (bfg accepts filename without path.)

bfg --delete-files Credentials.json

References

  • Removing sensitive data from a repository
  • bfg repo cleaner
like image 147
Simba Avatar answered Dec 31 '25 18:12

Simba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!