TIL: When you accidentally add commits to your fork that shouldn't be there (like personal notes), here's how to clean them up and get back in sync with the upstream repository.
Your fork shows: "This branch is X commits ahead of upstream:main" but those commits contain changes you don't want to contribute back (like personal notes, experiments, etc.).
This is the cleanest approach - makes your fork identical to upstream:
# Navigate to your forked repository
cd your-forked-repo
# Add upstream remote if you haven't already
git remote add upstream https://github.com/original-owner/original-repo.git
# Fetch the latest from upstream
git fetch upstream
# Reset your main branch to match upstream exactly
git reset --hard upstream/main
# Force push to update your fork on GitHub
git push origin main --force
If you want to be more careful and review what's different:
# First, check what commits you have that upstream doesn't
git log --oneline upstream/main..main
# If you're happy to remove them, rebase onto upstream
git rebase upstream/main
# Push the changes
git push origin main --force
See exactly what those commits contain before deleting:
# See what files were changed in those commits
git diff upstream/main..main
# See the commit messages
git log upstream/main..main --oneline
# If it's just unwanted changes, proceed with Option 1
After running the commands, verify your fork is clean:
# Check status
git status
# Confirm your fork matches upstream
git log --oneline -5
# Check on GitHub - should show "This branch is up to date with original-owner/original-repo:main"
Keep your fork updated going forward:
# Create an alias for easy syncing
git config alias.sync '!git fetch upstream && git checkout main && git merge upstream/main && git push origin main'
# Now you can easily sync with: git sync
After cleanup, your GitHub fork page should show:
- ✅ "This branch is up to date with original-owner/original-repo:main"
- ✅ No extra commits
- ✅ Clean history matching upstream
- Keep your main/master branch clean - only sync with upstream
- Create feature branches for any changes you want to contribute
- Use separate repositories for personal notes/experiments
- Regularly sync with upstream to stay current
This technique is useful when you:
- Accidentally committed personal notes to your fork
- Added experimental code that shouldn't be contributed
- Want to "reset" your fork to match the original exactly
- Need to clean up before creating a proper pull request
Created: 2025-07-11 | Last Updated: 2025-07-11