Skip to content

Instantly share code, notes, and snippets.

@Shivam010
Created December 11, 2020 18:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shivam010/155ef4331a247908dde0ca6120a108e7 to your computer and use it in GitHub Desktop.
Save Shivam010/155ef4331a247908dde0ca6120a108e7 to your computer and use it in GitHub Desktop.
Redact in git history | remove accidentally pushed data from git history

Redact in git history

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.

@Shivam010
Copy link
Author

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.g 123412..43523

To find the first commit in which file was added use this command git log --oneline --diff-filter=A -- config.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment