0

I have a pushed file with an history of sensitive data.

I'd like to clear the repository from all the file commits history, and leave only the current one, which is clean of sensitive data.

How can I do it (using git bash or GitHub desktop\web)?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
dushkin
  • 1,939
  • 3
  • 37
  • 82
  • Is there any reason https://stackoverflow.com/questions/35115585/remove-files-completely-from-git-repository-along-with-its-history won't work for you? – Jeffrey Mixon Nov 18 '20 at 00:24
  • 1
    *Files* don't have history, in Git. Files are members of commits, and commits *are* history. For your particular use case, though, The BFG (which I have never used) has a specific solution; consider using it. – torek Nov 18 '20 at 04:41
  • @JeffMixon First of all the solution you offered removed also the last commit - which I do not want to delete. Second, given my file is under src/main/resources/myfile.java, I have tried git filter-branch --tree-filter 'rm -f resources\myfile.java' -- --all , and it didn't work for me. – dushkin Nov 18 '20 at 10:17

1 Answers1

0

Find a version of that file you're willing to keep, say the latest master copy's clean:

safeversion=`git rev-parse master:path/to/file`

Filter history removing any version of that file that's not the safe one:

git filter-branch \
        --setup safeversion=`git rev-parse master:path/to/file` \
        --index-filter '
                if thisversion=`git rev-parse -q --verify $GIT_COMMIT:path/to/file` &&
                        [[ $thisversion != $safeversion ]]
                then git update-index --force-remove path/to/file
                fi
        ' -- --all
jthill
  • 55,082
  • 5
  • 77
  • 137