Skip to content

Instantly share code, notes, and snippets.

@N0K0
Forked from charandas/git-truncate.sh
Created August 1, 2017 11:44
Show Gist options
  • Save N0K0/60499b43f84fff5e0786e7a91b7a854e to your computer and use it in GitHub Desktop.
Save N0K0/60499b43f84fff5e0786e7a91b7a854e to your computer and use it in GitHub Desktop.
Truncate git history
# Prepare new_master
git checkout <hash-where-you-want-to-clip-history>
git checkout --orphan new-master
git checkout new_master
git cherry-pick first-commit-to-include^..last-commit-to-include
# Prepare master backup
git checkout master
git checkout -b master-backup
# Replace master
git branch -D master
git checkout new_master
git checkout -b master
git push --force origin master
# Delete the new_master branch as task is done
git branch -D new_master
# READ CAREFULLY, delete the master backup if sure that the task completed successfully
git branch -D master-backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment