Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active February 27, 2020 22:29
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save magnetikonline/5062718 to your computer and use it in GitHub Desktop.
Save magnetikonline/5062718 to your computer and use it in GitHub Desktop.
Truncate Git history to after specific SHA1, dropping everything at and before it.
#!/bin/bash -e
# Inspired by: https://github.com/adrienthebo/git-tools/blob/master/git-truncate
if [[ (-z $1) || (-z $2) ]]; then
echo "Usage: $(basename "$0") DROP_AT_SHA1 BRANCH"
exit 1
fi
if [[ ! $1 =~ ^[0-9a-f]{7,40}$ ]]; then
echo "Error: invalid Git commit SHA1" >&2
exit 1
fi
# note: if fails - include [--force] argument
git filter-branch \
--parent-filter "sed \"s/-p $1[0-9a-f]*//\"" \
--prune-empty \
-- "$2"
IFS=$'\n'
for refName in $(git for-each-ref --format="%(refname)" refs/original); do
echo "$refName"
git update-ref -d "$refName"
done
git gc --aggressive --prune=all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment