I'll help you clear Git commit history. Here are several methods, from least to most destructive:
Method 1: Soft Reset (Keep Working Directory)
If you want to keep all changes but reset to initial state:
# Reset to first commit, keep all files staged
git checkout --orphan new-branch
git add -A
git commit -m "Initial commit"
git branch -D main
git branch -m main
git push -f origin main
Method 2: Hard Reset (Common Approach)
Start fresh but keep current files:
# Create orphan branch with current files
git checkout --orphan temp-branch
git add -A
git commit -m "Fresh start"
git branch -D main
git branch -m main
# Force push to remote
git push -f origin main
Method 3: Completely Fresh Repository
If you want to completely erase history and start new:
# Remove .git directory
rm -rf .git
# Reinitialize git
git init
git add .
git commit -m "Initial commit"
# Set remote and force push
git remote add origin <repository-url>
git push -f origin main
Method 4: Interactive Rebase (Selective History)
If you want to squash many commits into one:
# Squash all commits into one
git reset $(git commit-tree HEAD^{tree} -m "Initial commit")
# Alternative: rebase interactively
git rebase -i --root
# Then mark all commits except first as 'squash'
Important Notes:
⚠️ Warnings:
-
Force push (
-f) will overwrite remote history - Collaborators will need to reclone or reset their local repos
- Lost commits cannot be easily recovered
Before Proceeding:
# Backup your current state
git branch backup-branch
# Verify remote
git remote -v
git log --oneline -5
After Clearing History:
# Tell collaborators to:
git fetch origin
git reset --hard origin/main
# Or they should reclone entirely
Recommended Workflow:
# 1. Backup current branch
git branch backup-main
# 2. Create new orphan branch
git checkout --orphan clean-main
# 3. Add and commit files
git add -A
git commit -m "Initial commit"
# 4. Delete old main and rename
git branch -D main
git branch -m main
# 5. Force push
git push -f origin main
# 6. Clean up local backup
git branch -D backup-main
Alternative: Shallow Clone (If You Can't Force Push)
If you don't have permission to force push:
# Clone with only last commit
git clone --depth 1 <repository-url>
# Continue working from there
Top comments (0)