Why Would You Want This?
Sometimes your Git history accumulates commits you no longer need — experimental changes, sensitive data that was accidentally committed, or simply a messy history you want to clean up. If you've already pushed to main, you can rewrite the branch history so it contains only the current project state as a single commit.
⚠️ Warning: This requires a force push, which rewrites the remote history. Any collaborators will need to re-clone or reset their local copies. Only do this if you fully own the repository or have coordinated with your team.
Step-by-Step Commands
1. Switch to the main branch
Make sure you're on the branch you want to reset:
git checkout main
2. Create a new orphan branch
An orphan branch has no commit history — it starts completely fresh while keeping all your current files:
git checkout --orphan temp
3. Stage all files
Add everything in the working directory to the staging area:
git add -A
4. Create a single clean commit
This becomes the one and only commit in your new history:
git commit -m "Initial commit"
5. Delete the old main branch locally
Remove the old main branch that still carries all the previous commits:
git branch -D main
6. Rename the new branch to main
Rename your clean orphan branch to main:
git branch -m main
7. Force-push the new history
Push the rewritten history to the remote. The --force flag is required because you're replacing the entire commit history:
git push --force origin main
Complete Script
Here's the full sequence you can run in one go:
git checkout main
git checkout --orphan temp
git add -A
git commit -m "Initial commit"
git branch -D main
git branch -m main
git push --force origin main
What Happens Behind the Scenes
| Step | What It Does |
|---|---|
--orphan temp | Creates a branch with no parent commit — a blank history |
git add -A | Stages all current files (your code is preserved) |
git branch -D main | Deletes the old branch with all its commits |
git branch -m main | Renames temp → main so the branch name stays the same |
--force push | Overwrites the remote history with the new single-commit history |
Important Notes
- Your code is safe — only the commit history is removed. All current files remain intact.
- Collaborators will be affected — anyone who has cloned the repo will need to re-clone or run
git fetch origin && git reset --hard origin/main. - GitHub PRs may break — any open pull requests targeting
mainwill lose their base reference. - This is irreversible on the remote — once force-pushed, the old commits are gone (unless someone still has them locally).
When to Use This
- ✅ Personal projects where you want a clean start
- ✅ Removing accidentally committed secrets from history
- ✅ Resetting a demo/template repository
- ❌ Shared team repositories without coordination
- ❌ Production repos with active pull requests