Skip to content

Instantly share code, notes, and snippets.

@brianloveswords
Last active September 3, 2024 14:10
Show Gist options
  • Save brianloveswords/7545299 to your computer and use it in GitHub Desktop.
Save brianloveswords/7545299 to your computer and use it in GitHub Desktop.
git-obliterate: for removing sensitive files you may have committed from the entire history of the project.

git-obliterate

Has this ever happened to you?

cue video of someone accidentally committing super-secret-private-stuff.json

Time to blow away the entire project and start over right?

Wrong!

With git-obliterate, you can make it seem like the file has never existed in the project's history!

Usage

First, read this because I'm mostly just scripting a lot of what they say here: https://help.github.com/articles/remove-sensitive-data

Download the file, chmod +x it, and copy it to /usr/local/bin (or somewhere else in your PATH). Then you can do git obliterate <file> and it will wipe it from history. Singing Cher's β€œIf I Could Turn Back Time” is optional. You will then have to git push --force to update your remote.

After that's done, you'll need to force update your remotes with git push --force, making sure to do all the branches and tags and everything that the file might have been in – if you're lucky, you will have caught it early and hopefully that should just be master.

Lazy Install

curl -O https://gist.githubusercontent.com/brianloveswords/7545299/raw/6128cb79b624d915bfc1942177a3508fd47efbfe/git-obliterate && chmod +x git-obliterate && sudo mv git-obliterate /usr/local/bin
#!/bin/bash
file=$1
test -z $file && echo "file required." 1>&2 && exit 1
git filter-branch -f --index-filter "git rm -r --cached $file --ignore-unmatch" --prune-empty --tag-name-filter cat -- --all
git ignore $file
git add .gitignore
git commit -m "Add $file to .gitignore"
@davemc0
Copy link

davemc0 commented Mar 25, 2022

The 2022 way to obliterate a file is to just type the following line. No need to download any scripts or tools at all. It's line 4 of the above git-obliterate script:
git filter-branch -f --index-filter "git rm -r --cached $file --ignore-unmatch" --prune-empty --tag-name-filter cat -- --all
Just replace $file with your filename.

After this finishes, don't forget to push your changes to the remote using:
git push --force

Since this isn't in the script I think not doing this is why some people say the script doesn't work.

@imgnx
Copy link

imgnx commented Sep 1, 2024

@brianloveswords
Copy link
Author

@imgnx updated

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