Im sure there is a much more efficient way of doing this, but I had to remove about 2000 commits from my repo.. this is how I achieved it.
- Step 1: Identify the commit to be your new root
- Step 2: Clone and Trim all commits before
- Step 3: Actully trim
- Step 4: Clean up the remote (github)
Step 1: Identify the commit to be your new root
For this I simply looked in Github, at the commit history. And I selected this one (my first commit)
My commit was
Step 2: Clone and Trim all commits before
Check out a new branch with that commit hash
git checkout -b oldroot 295c4c6ece076396eef2d8477d768d4c255b14b9 git write-tree
Note the output. Mine was
Take that new, add a commit message and commit-tree
git commit-tree -m "New History Rebase" 00fb96df77166974bacb5bfa16e28cbd1baf8fbd
Note the output again. Mine was
Checkout a new branch with this new root, and rebase into master.
git checkout -b newroot 64e19740fb1b78d89c3ca3bf6cf60d124caf7c14 git rebase --onto newroot oldroot master # repeat for other branches than master that should use the new initial commit git checkout master git branch -D oldroot git branch -D newroot
Step 3: Actully trim
Danger Notice: Make sure you have a backup!!!
Step 4: Clean up the remote (github)
Im sure there is a much better way to do this…. but this is what worked for me.
git checkout newmaster git push --set-upstream origin newmaster
Now we need to go to github and change the default branch away from master (to new master). If you dont do this, the remote delete will be rejected.
Once done…we delete the remote master (and local master); then create a new master branch.
git push origin --delete master git branch -D master git checkout -b master git push --set-upstream origin master
Now go back to where you were on github before; and change the default back to master again; then we remove the newmaster branch.
git push origin --delete newmaster git branch -D newmaster
And done… all old commits before your hash should be removed, and the branches cleaned up.