Skip to content

Instantly share code, notes, and snippets.

@pizofreude
Created July 11, 2025 03:55
Show Gist options
  • Save pizofreude/c08c0304ce3fb624a306591eea5f3b70 to your computer and use it in GitHub Desktop.
Save pizofreude/c08c0304ce3fb624a306591eea5f3b70 to your computer and use it in GitHub Desktop.
This gist captures the key learning and provides a reusable reference for future fork cleanup scenarios.

How to Clean Up Fork Commits and Sync with Upstream

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.

The Problem

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.).

Solution: 3 Options to Clean Up

Option 1: Reset Fork to Match Upstream (Recommended)

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

⚠️ Warning: This permanently deletes those commits from your fork.


Option 2: Rebase to Clean History (Alternative)

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

Option 3: Check What's Different First (Most Cautious)

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

Verify the Cleanup

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"

Set Up Proper Upstream Syncing for Future

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

Expected Result

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

Best Practices for Fork Management

  1. Keep your main/master branch clean - only sync with upstream
  2. Create feature branches for any changes you want to contribute
  3. Use separate repositories for personal notes/experiments
  4. Regularly sync with upstream to stay current

Use Cases

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment