git filter-branch --tree-filter <command> -- --all
This allows you to edit the contents of the tree (the actual files and directories) for each commit.
It will run a command in a directory containing the entire tree, your command may edit files, add new files, delete
files, move them, and so on. Git will then create a new commit object with all of the same metadata (commit
message, date, and so on) as the previous one,
But with the tree as modified by your command, treating new files as adds, missing files as deletes, etc (so, your
command does not need to do git add
or git rm
, it just needs to modify the tree).
e.g. git filter-branch --tree-filter "sed -i '' -e 's|SekrtPassWrd|REDACTED|g' config.json" -- --all
Remember to run this command in a copy of your project/repository, so if something goes wrong, you will still have the original and can start over again.
If the file you want to redact data in is not present in all the commits, you will get an error in the above command.
To resolve the error, instead of using
--all
history, use a valid range of commits (hash) in which the file is present e.g123412..43523
To find the first commit in which file was added use this command
git log --oneline --diff-filter=A -- config.json