#!/usr/bin/env bash
help_text="
NAME
repo-remove-history - Remove entire commit history from the current Git repo and push to origin.
USAGE
repo-remove-history [options]
OPTIONS
-h|--help
Show help text.
DESCRIPTION
Remove entire commit history from the current Git repo and push to origin.
AUTHOR
Martin N 2025
"
help_line="Remove entire commit history from the current Git repo and push to origin"
web_desc_line="Remove entire commit history from the current Git repo and push to origin"
try="Try ${0##*/} -h for more information"
tmp="${help_text##*USAGE}"
usage=$(echo "Usage: ${tmp%%OPTIONS*}" | tr -d "
" | sed "s/ */ /g")
while [[ "$1" != "" ]]; do
case $1 in
-h|--help)
echo "$help_text"
exit
;;
?*)
break
;;
esac
shift
done
echo "This removes the history from the current repo and pushes/commits this to the origin"
echo
read -p "Are you sure you want to continue [yN]? " yn
if [[ ${yn^^} != Y ]]; then
exit
fi
echo Resetting
echo
# 1. Create a new orphan branch (no history)
git checkout --orphan new-main
# 2. Stage all files
git add -A
# 3. Create a fresh initial commit
git commit -m "Initial commit"
# 4. Delete the old main branch
git branch -D main
# 5. Rename new-main to main
git branch -m main
# 6. Force push to overwrite the remote
git push --force origin main
# 7. Delete all other remote branches (keeps only main)
git branch -r | grep -v 'origin/main' | grep -v 'origin/HEAD' | sed 's/origin\///' | xargs -I {} git push origin --delete {}
# 8. Prune remote references
git remote prune origin