Skip to content

Instantly share code, notes, and snippets.

@deads2k
Last active August 22, 2022 18:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save deads2k/49475a1d3c45ed7f4d7fdab73fd7f372 to your computer and use it in GitHub Desktop.
Save deads2k/49475a1d3c45ed7f4d7fdab73fd7f372 to your computer and use it in GitHub Desktop.
Clean git history
https://stackoverflow.com/questions/17901588/new-repo-with-copied-history-of-only-currently-tracked-files
git remote | while read -r line; do (git remote rm "$line"); done
git tag | xargs git tag -d
git branch | grep -v \* | xargs git branch -D
git stash clear
git config --local -l | grep submodule | sed -e 's/^\(submodule\.[^.]*\)\(.*\)/\1/g' | while read -r line; do (git config --local --remove-section "$line"); done
rm -rf .git/modules/
git ls-files | sed -e 's/^/"/g' -e 's/$/"/g' > keep-these.txt
git ls-files | while read -r line; do (git log --follow --raw --diff-filter=R --pretty=format:%H "$line" | while true; do if ! read hash; then break; fi; IFS=$'\t' read mode_etc oldname newname; read blankline; echo $oldname; done); done | sed -e 's/^/"/g' -e 's/$/"/g' >> keep-these.txt
git filter-branch --force --index-filter "git rm --ignore-unmatch --cached -qr .; cat \"$PWD/keep-these.txt\" | xargs git reset -q \$GIT_COMMIT --" --prune-empty --tag-name-filter cat -- --all
rm keep-these.txt
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
// to clear out lots of empty merge commits, you have to rebase on the first commit
git log --reverse // find the first one
git rebase -Xtheirs <that first commit>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment