Skip to content

Instantly share code, notes, and snippets.

@AppleBoiy
Created February 25, 2025 06:44
Show Gist options
  • Save AppleBoiy/a9fa1cff9819cc471310c41cc87fccf8 to your computer and use it in GitHub Desktop.
Save AppleBoiy/a9fa1cff9819cc471310c41cc87fccf8 to your computer and use it in GitHub Desktop.
bash script for prune all git 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
@AppleBoiy
Copy link
Author

Execute Directly Without Saving

If you don't want to save the file locally, use curl or wget and pipe the output directly to bash:

Using curl:

curl -s https://gist.githubusercontent.com/AppleBoiy/a9fa1cff9819cc471310c41cc87fccf8/raw/151db93c96ee3d95305c87c4b0214812944d708d/remove_git_history.sh | bash

Using wget:

wget -qO- https://gist.githubusercontent.com/AppleBoiy/a9fa1cff9819cc471310c41cc87fccf8/raw/151db93c96ee3d95305c87c4b0214812944d708d/remove_git_history.sh | bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment