There are situations where you want to keep your project files but get rid of the entire Git history. Maybe you’re preparing a formerly private project for a public release. Maybe you’ve removed sensitive data that was committed in the past. Or maybe you just want to start fresh — without rewriting history commit-by-commit.
This post shows how to fully wipe Git history, automatically detect the default branch, and set a custom commit message — all in a few safe and reproducible steps.
⚠️ Warning: This Is Destructive
The method described here will permanently delete all previous Git commits and force-push a new branch with a single clean commit.
Make sure:
- You have a backup (e.g. cloned repo or separate branch).
- You’re okay with losing the entire commit history.
When to Use This
- You’re open-sourcing a repo that previously contained secrets or internal info.
- You want a clean starting point without refactoring commits.
- You want to share a snapshot of code, not its development journey.
Git Commands to Delete All History
This approach works with any default branch (main
, master
, or custom) and lets you define your own commit message.
# Detect default branch (e.g. 'main' or 'master')
DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}')
# Optional: Set your commit message
COMMIT_MSG="Initial commit"
# Create a new orphan branch with no history
git checkout --orphan temp_branch
# Add all files to staging
git add -A
# Commit using your custom message
git commit -m "$COMMIT_MSG"
# Delete the old branch with all history
git branch -D "$DEFAULT_BRANCH"
# Rename orphan branch back to original name
git branch -m "$DEFAULT_BRANCH"
# Force-push to overwrite history on remote
git push -f origin "$DEFAULT_BRANCH"
Example Use Case
Let’s say your private repo once included .env
files or internal documentation that you’ve since removed and added to .gitignore
. Even though they’re gone now, they may still live in the commit history.
This approach wipes that history and publishes only the current clean state.
Pro Tip: Don’t Just Delete — Audit First
Before doing this, you can use tools like:
git log --stat
– to check what was historically addedgit filter-repo
orBFG Repo-Cleaner
– if you only want to remove sensitive files but keep the rest of the history
Summary
Feature | Description |
---|---|
Branch autodetection | Works with main , master , or custom |
Custom commit message | Yes (via COMMIT_MSG variable) |
Force pushes to remote | Yes, with full history replacement |
Safe to run locally | Yes, as long as you push manually |