Skip to content

Instantly share code, notes, and snippets.

@AppleBoiy
Created May 24, 2024 03:10
Show Gist options
  • Save AppleBoiy/c7d03693dfc509cb1958ec05d1f4b089 to your computer and use it in GitHub Desktop.
Save AppleBoiy/c7d03693dfc509cb1958ec05d1f4b089 to your computer and use it in GitHub Desktop.
Delete all git commit history
#!/bin/bash
clear && cat << EOF
##########################################| WARNING |##########################################
!THIS SCRIPT WILL DELETE ALL COMMIT HISTORY AND PUSH A NEW INITIAL COMMIT.
Make sure you really want to do this before proceeding.
##########################################| WARNING |##########################################
EOF
read -p "Are you sure you want to delete all commit history and force push a new initial commit? (y/n): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "Operation aborted."
exit 1
fi
echo "Proceeding with deleting commit history and creating a new initial commit..."
# delete commit history and create a new initial commit
del_commit() {
git checkout --orphan latest_branch || { echo "Failed to create orphan branch"; exit 1; }
git add -A || { echo "Failed to stage files"; exit 1; }
git commit -am "initial commit" || { echo "Failed to create initial commit"; exit 1; }
git branch -D main || { echo "Failed to delete main branch"; exit 1; }
git branch -m main || { echo "Failed to rename branch to main"; exit 1; }
git push -f origin main || { echo "Failed to force push to main"; exit 1; }
}
del_commit
echo "New initial commit has been created and pushed to the main branch."
echo "Operation completed successfully."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment