Skip to content

Instantly share code, notes, and snippets.

@l1x
Last active November 12, 2020 12:02
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 l1x/01700e1b23072bec64df11124ae879aa to your computer and use it in GitHub Desktop.
Save l1x/01700e1b23072bec64df11124ae879aa to your computer and use it in GitHub Desktop.
Deleting a sensitive file from Git (including history) starting a certain commit hash
#!/usr/bin/env bash
PATH_TO_BE_DELETED="${1}"
if [[ -z ${PATH_TO_BE_DELETED} ]]; then
echo "Path variable a is not set"
exit 1
else
echo "Deleting $PATH_TO_BE_DELETED"
fi
STARTING_HASH=$(git log --pretty=format:"%h" -- ${PATH_TO_BE_DELETED} | tail -1)
echo "Starting $STARTING_HASH"
git filter-branch \
--index-filter "git rm --cached -f -r --ignore-unmatch $PATH_TO_BE_DELETED" \
--prune-empty -- $STARTING_HASH..HEAD
@kodfodrasz
Copy link

When completed, you need to *force push the changes upstream

git push -f origin/master

If someone has a local version affected by the rewrites they need to change their local branches to upstream:

git fetch origin
git reset --hard origin/master

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