How I removed a file from my git repo history | Funky Penguin

While making my most recent commit to my blog, I discovered (after the fact) that I’d accidentally committed a screenshot from a customer project, into my /images/ subdirectory.


This is a companion discussion topic for the original entry at https://www.funkypenguin.co.nz/note/how-i-removed-a-file-from-my-git-repo-history/

Just a comment.
You have just “rewrite the history”, so all git commits hash id have changed. T
It is a very dangerous operation if the commits already have been shared with any developer, and they are using them to make changes, as when they try to sync again the rebase / merge will be a hell on earth for them as Git market ALL the commits as different (as the hash id has changed).
Usually if this has to be done, before doing it in code-that-is-already-shared, a warning is made to all developers to commit and share all work all before and wait until the rewrite is done, and after that fetch the code again.
In this case as it is your own repo and you are the only contributor there is not problem. Just a comment for those that lands here searching for a solution to remove the passwords.txt inplain text that have been shared :wink:

Good point :slight_smile: Is there a better way to erase the secret data?

Not really,
It is just a warning, Git usually is very safe, but rewriting the history is one of the things that could become messy in a bit.

Rewriting the history is your only option. If you’re erasing only one file, I wouldn’t characterize this as dangerous. I once did this process for a huge legacy PHP project, removing all images. The project had assets in many subdirectories, so it was a big task.

First make a backup copy of your local repo in case anything goes wrong. You can simply copy the entire project to another folder (easy peasy).

For the other developers, ask them to rename whichever branch they’re working on to something like some-branch-old, and then checkout the new version of some-branch which has the rewritten history. If they have some-branch on localhost tracking some-branch on origin, then they will need to remove tracking so they can checkout some-branch with the rewritten history. From there they can either copy/paste their code back into some-branch, or they can cherry-pick their commits from some-branch-old into some-branch.

Doesn’t have to be a scary process. Best of luck.

Thank you for the resume of the actions that should be done.
What I meant, is that rewritting the history is the way to remove the file, but it has to be done carefully as it affects the others developers.
You already mention:

  • backup the git folder (this is probably one of the few reasons to do it it git)
  • Remove tracking from branches
  • Cherry picking commits.
    those actions are not the usual one for an normal user of git, and frankly if you are not a full time developer probably didn’t read the right documentation or learn properly git.

In my day-work, we have switch to git recently and the only with git experience from a group of 20 experience coders (10 years minimun) was me, and for most of them just the word rebase is magic :frowning:
Being say that, git is amazin and should be learned properly if you are going to use it. I recommend to read and undersand at least the three first chapters from the git-pro book.

1 Like