Skip to content

Instantly share code, notes, and snippets.

@ultrox
Last active June 26, 2019 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ultrox/2c3203bcbda3077ee467d9edb1d0ab5e to your computer and use it in GitHub Desktop.
Save ultrox/2c3203bcbda3077ee467d9edb1d0ab5e to your computer and use it in GitHub Desktop.
Permanently remove any file from repository!

This script should remove from existence file you don't want in your repo, as you never commited it, nothing more nothing less.

Tested on arch, might not work on other distributions, if not look at condition.

  1. Download script:
curl -Os https://gist.githubusercontent.com/ultrox/2c3203bcbda3077ee467d9edb1d0ab5e/raw/e477fbeaf8556ac02748809154c95e3e5b6cd04c/git-rmf && chmod u+x git-rmf
  1. move to $PATH
  2. Run it like this git-rmf OFFENDING-FILE
  3. If you pushed, you need to force it git push --force. If you work with a team, notify them or you'll be new next hated guy.
  4. Pat yourself on the back, good job!

NOTE: if you removing sensitive file you should run garbage collection to prune dangaling commits and expire reflog, after that there is no undo.

git reflog expire --expire=now --all && git gc --prune=now --aggressive

#!/bin/bash
FILE=$1
if [[ -f $FILE ]]; then
git filter-branch -f --index-filter "git rm -r --cached $FILE --ignore-unmatch" --prune-empty --tag-name-filter cat -- --all
else
echo File required
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment