git: change one line in file for the complete history
Here's a command that will remove an offending line from a file's history in all your branches:
git filter-branch --tree-filter 'sed -i "/sensitive information/ d" filename' -- --all
This checks out every revision, runs the sed
command on it, then commits that revision back.
The sed
command in this case matches any lines containing the pattern sensitive information
in the file named filename
and deletes those lines.
Note: it's good if you have a backup, and try the sed
script on its own first to make sure it's doing what you want, because it can take quite a while to run on a long history.
My answer combines the best of everything. It replaces the just the given filter string instead of the whole line and it nicely skips if the file is not found in a commit. There are a lot of escaped quotes but the other answer had really weird quotes that didn't work for me.
git filter-branch --tree-filter "test -f \"$filename\" && sed -i \"s/$filter//g\" \"$filename\" || echo \"skipping $filename\"" -- --all