If a user accidentally commits a file, let's say a file with database passwords, how do you permanently remove all instances of that file from a git repository? It still needs to live on developers machines but not in the git repository.
Asked
Active
Viewed 343 times
1
-
possible duplicate of [Removing a file from Git source control but not from the source](http://stackoverflow.com/questions/936249/removing-a-file-from-git-source-control-but-not-from-the-source) – ChrisGPT was on strike Jan 24 '14 at 01:45
3 Answers
1
You have to rewrite all commits containing that file. git filter-branch
will help you with that.
From git help filter-branch
:
EXAMPLES
Suppose you want to remove a file (containing confidential information or copyright
violation) from all commits:
git filter-branch --tree-filter 'rm filename' HEAD
Using --index-filter with git rm yields a significantly faster version.
git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD
Of course, if you already published your commits and other people cloned it, they still have the file in their repository and might even reintroduce it if they are allowed to push to your repository.

michas
- 25,361
- 15
- 76
- 121
1
Github has an article that explains the process with Git.

kenorb
- 155,785
- 88
- 678
- 743

Miguel Nieva
- 11
- 1
- 3
0

kenorb
- 155,785
- 88
- 678
- 743

ThangNguyen
- 197
- 1
- 12
-
Specifying `my-repo.git` is not necessary, just use the filename/glob and run the command in your repo root. – kenorb Aug 10 '15 at 10:42