Fresh Start: Backing Up Main Branch and Creating Clean Main
Overview
Sometimes you need to start fresh with your main branch while preserving the existing code history. This guide shows how to backup your current main branch to another branch and create a clean, empty main branch.
Step-by-Step Process
1. Backup Current Main Branch
First, create a backup of your current main branch:
# Switch to main branchgit checkout main
# Create a backup branch from current maingit checkout -b main-backup
# Push the backup branch to remotegit push -u origin main-backup2. Alternative: Create Named Backup Branch
You can also create a more descriptive backup branch name:
# Create backup with timestamp or versiongit checkout -b main-backup-$(date +%Y%m%d)# orgit checkout -b main-v1-backup
# Push to remotegit push -u origin main-backup-$(date +%Y%m%d)3. Create Clean Main Branch
Now create a fresh, empty main branch:
# Create a new orphan branch (no history)git checkout --orphan new-main
# Remove all files from staginggit rm -rf .
# Create initial commit with empty repositorygit commit --allow-empty -m "Initial commit - fresh start"
# Delete old main branch locallygit branch -D main
# Rename new-main to maingit branch -m new-main main4. Force Push New Main Branch
⚠️ Warning: This will overwrite the remote main branch. Make sure your backup is pushed first!
# Force push the new empty main branchgit push -f origin main5. Verify Backup and Clean Main
# Check that backup branch existsgit branch -a
# Verify main is cleangit log --oneline
# Check backup branch has your old codegit checkout main-backupgit log --onelineAlternative Approach: Using git reset
If you want to keep some history but start from a specific clean commit:
# Backup current maingit checkout maingit checkout -b main-backupgit push -u origin main-backup
# Reset main to initial commit (or any clean commit)git checkout maingit reset --hard <initial-commit-hash>
# Force push the reset maingit push -f origin mainBest Practices
Before Starting
Backup Verification
Communication
Recovery Process
If you need to restore from backup:
# Switch to backup branchgit checkout main-backup
# Create new main from backupgit checkout -b new-main
# Delete current maingit branch -D main
# Rename to maingit branch -m new-main main
# Force push restored maingit push -f origin mainUse Cases
This approach is useful when:
- Starting a major refactor or rewrite
- Migrating to a completely different technology stack
- Cleaning up a repository with sensitive data in history
- Creating a demo repository from an existing project
- Separating different versions of a project
Security Considerations
- Sensitive Data: If backing up due to sensitive data in history, ensure the backup branch is properly secured
- Access Control: Consider who has access to backup branches
- Cleanup: Remove backup branches when no longer needed
Notes
- The original commit history is preserved in the backup branch
- Contributors will need to re-clone or reset their local repositories
- Any open pull requests targeting main will need to be updated
- Consider using
git filter-branchorgit filter-repoif you need to remove specific files from history before backup