git: How to remove a file from all the commits of a repository

4 min read | by Jordi Prats

If we have included a file that shouldn't be in a git repository, we can remove it from all the commits that it is present using filter-branch

We just need to specify the file we want to get rid of (for example: secretfile.txt):

$ git filter-branch -f --index-filter  'git rm -rf --cached --ignore-unmatch secretfile.txt' HEAD 
Rewrite 57e045747695b69b980f05d878e81d7d60f2b5f8 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite dcdfbcf80d70dbb5c292ab2c12c4ef70c18e13b0 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite ee288ce5b880d59bf941d36f1e242b48a214d6b4 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite e2a5fbaf3b97805eed64104a39da794a512b0f29 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite defe252723c66ad294dbb5bcf82086b0340611f6 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite 57480355f91d232fa81ad4249d27e43d877a9501 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite 1cf89e843114ce26e3c835cc6b5f60bbed0bbf74 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite 40a5fd142f9712ad694452f90b4030fcfa20d0f3 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite 19a279dd9c37d1b9163daf337534b91a11e80db5 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite 250d217f1a4573a840df798a042cda0a0b2487f6 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite 96023083ad6ed5aa2bd321adb2b9ad4d95602f6a (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite 6eaeec2d1a7235ca7c7243f18639a1b9007fb6f7 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite 25997a8d679ff81ff9c52c59f4cad5ced0ff1b13 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite 5d37c0d41cd41ed4990f99a35bdcff5b32aa0d93 (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite d5b5e11c36d114d1e9d793a80b5ed0310ba249db (19/53) (1 seconds passed, remaining 1 predicted)    rm 'secretfile.txt'
Rewrite 2e5eb6c085fca0a65c213bbf36672144f7f76d10 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite e7bd95f74cf018f2c2ce7ab92f9b4863b54349b9 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite 29a7464cc623dd01f70f056ead262d12cb370b43 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite f384bbc28d6fb422ff7a6526c65e98113e5f67ca (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite c7718508000a38fc3b3a2d8b3b7eeac1d8ebd1b4 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite 7ba457929a0bc592f9d39079c4d60b53a085019e (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite a4fad16ea15a9bab24e0445c75fccffe0f9eb2d9 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite 2a3c17085b2e049491cf415b782aee80a3eaa0df (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite d7edae762166221819c888805de51b7f1f6bf563 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite d496af88abc81d8529a53a652d02e38acedd4b67 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite 5ec34ac893ba7b41863b04ced82856a341bbc954 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite e82227e87cc7ab117633920f0e9feb142eff5c38 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite 9b67ee85dd3a6bd228cffa661bca271698f48ac7 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite 11ab64629fb4515c62014af3160d4e92e8e79eef (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'
Rewrite a812c9370a899cc9a39a0468fb17c2d2604bc1f3 (39/53) (1 seconds passed, remaining 0 predicted)    rm 'secretfile.txt'

Ref 'refs/heads/main' was rewritten

This command will scan all the commits from the git repository and modify them to remove any files with that name from them. Due to this, the commit history will be rewritten to it so we won't be able to push the changes directly:

$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1499 and 1501 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean

To be able to update any remote repository we will have to forcefully push them using git push -f:

$ git push -f
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 329 bytes | 329.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To ssh://git.github.com/pet2cattle/demo.git
   fd9efee..917a648  master -> master

Posted on 25/05/2022

Categories