Skip to content

Instantly share code, notes, and snippets.

@stephen-james
Forked from magnetikonline/gittruncatehistory.sh
Last active August 29, 2015 14:27
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 stephen-james/7165e303923e4caca95e to your computer and use it in GitHub Desktop.
Save stephen-james/7165e303923e4caca95e to your computer and use it in GitHub Desktop.
Truncate Git history to a specific SHA1, dropping everything before it.
#!/bin/bash
# Lifted from: https://github.com/adrienthebo/git-tools/blob/master/git-truncate
# Note: seem to be finding the need to run this twice over to commit the truncate
if [ -z "$1" -o -z "$2" ]; then
echo "Usage: $0 <drop before SHA1 commit> <branch>"
exit
fi
git filter-branch --parent-filter "sed -e 's/-p $1[0-9a-f]*//'" \
--prune-empty \
-- $2
git for-each-ref --format="%(refname)" refs/original | \
while read REFNAME; do
echo $REFNAME
git update-ref -d "$REFNAME"
done
git reflog expire --expire=0 --all
git repack -ad
git prune
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment