Skip to content

Instantly share code, notes, and snippets.

@jonathanduke
Last active January 27, 2021 21:52
Show Gist options
  • Save jonathanduke/020d97409396e784a2db84f178c8b907 to your computer and use it in GitHub Desktop.
Save jonathanduke/020d97409396e784a2db84f178c8b907 to your computer and use it in GitHub Desktop.
Squash historical commits, leaving only the last 30 days of history, without harming the current worktree
###
## Based on: https://gist.github.com/jonathanduke/020d97409396e784a2db84f178c8b907#file-git-squash-history-sh
## Public domain: http://unlicense.org/
## Modify as you wish, but you are encouraged to leave this comment for future reference in case the original is updated.
###
# Use at your own risk! To make sure you know what you're doing, "origin" and "master" have been replaced with different names.
git checkout -B TEMPhead
git worktree add --checkout -B TEMPstart _TEMPworktree TEMPhead~30 # goes back 30 commits (or manually specify your own branch)
cd _TEMPworktree
git checkout --orphan TEMPnew # OR git checkout -B TEMPnew AND git reset --soft MYtarget # where "MYtarget" is the new base
git commit -m "Squashed history"
git cherry-pick TEMPstart..TEMPhead # assumes no other branches and merges are involved; otherwise do this manually
git branch -D TEMPstart
cd ..
git worktree remove _TEMPworktree
git checkout TEMPnew
git branch -D TEMPhead
git branch -M TEMPnew MYinitial # where "MYinitial" is the original branch we are squashing (probably master)
git push --force "MYremote" HEAD # where "MYremote" is the remote name (probably origin) and assumes HEAD to be where we started
git reflog expire --expire=now --all
git gc --prune=now
#git lfs prune # if using git-lfs, this will prune old versions of the file locally (not on the server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment