3

Suppose I found out that there are files that weren't suppose to be in my git revision. Such as passwords, API keys and trade secret documents.

Is there a way to delete those files in the revisions that they appear in? And still keep the rest of the files in that revision?

lucapette
  • 20,564
  • 6
  • 65
  • 59
Bill
  • 3,059
  • 3
  • 31
  • 47

1 Answers1

1

git filter-branch might be useful. This allows you to rewrite a series of past commits, changing what was committed.

If the the changes you've made have never left your local repository, then this will work. If you've pushed the offending changes somewhere, or if someone has pulled them from you, then you have a bigger problem of getting rid of them in everyone's repository.

But, to fix your local repository:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch bad-file1.txt bad-file2.txt' master..abc

This means:

  • git filter-branch: Let's rewrite some commits!

  • --index-filter: Change each commit's index without actually checking it out on disk.

  • 'git rm --cached --ignore-unmatch bad-file1.txt bad-file2.txt': For each commit, unstage the two files if they exist.

  • master..abc: Do this on all commits on branch abc back to where it forked from the master branch. You could also say "master~3" to rewrite the past 3 commits, or any other range of revisions. You can also use "-- --all" (two dashes, space, two dashes, "all") to rewrite everything in your repository.

Stephen Jennings
  • 12,494
  • 5
  • 47
  • 66