newren / git-filter-repo

Quickly rewrite git repository history (filter-branch replacement)
Other
8.55k stars 708 forks source link

[feature request] ability to replace a blob in history #436

Closed peter17 closed 1 year ago

peter17 commented 1 year ago

Hi!

I have the following need: I commited an image file to a repository a long time ago, with sensitive data inside. Several commits happened since then but they did not modify that file.

I would like to be able to replace that image file with a new one (same name, same path) in the whole history.

I saw that replacing text in files, text in commit messages and refs is supported but it would be awesome to be able to do that with whole binary files.

Thanks in advance, best regards

me-and commented 1 year ago

This is already supported, although it's not as trivial as a single argument: you'll need to use the --blob-callback option. Something like the below (untested):

git filter-repo '
  if blob.original_id == "<hash of the bad object>":
    blob.data = open("<path to the replacement file>", "rb").read()
'

I suspect this is a relatively rare scenario; if there's a file in the repository that shouldn't be there, folk are more likely to want to just delete it. If there is sensitive data, it's probably passwords or similar, which the text filtering tools will be able to find.

peter17 commented 1 year ago

@me-and Thanks!