How to sync my GitHub fork with upstream changes without duplicating commits

I have a forked repository on GitHub and I made some commits to it. The maintainer of the original repo accepted my pull request and also merged other contributions from different developers.

Now I want to update my fork with all the new changes from the upstream repository. When I tried doing a regular pull and then push, I ended up with my own commits showing up twice in the history.

What is the proper workflow to keep my fork synchronized with the main repository while avoiding commit duplication? I need to get the latest changes but maintain a clean git history.

to avoid duplication, try git fetch upstream and then git rebase upstream/main. it puts your commits on top of the latest changes from the original repo. much cleaner than a merge!

Duplicate commits happen because your fork still has the original commits that were already merged upstream. Skip the rebasing or hard reset - just use git merge upstream/main after fetching, but delete your local branch first if it’s already merged. I run git branch -d feature-branch for any branches that got accepted upstream, then make fresh branches from the updated main. Stops you from having the same changes show up multiple times and keeps your git history clean. I’ve been doing this for years and it kills those annoying duplicate entries.

Honestly, just run git pull upstream main --rebase in one command. Saves you from doing separate fetch and rebase steps. I’ve been using this shortcut for months and never get duplicate commits anymore. Way simpler than all the stashing and branch deleting stuff.

I hit this exact problem when I started contributing to open-source projects. Git creates new commit objects during regular merges even when the content’s identical to what’s already upstream. Now I just use git fetch upstream then git reset --hard upstream/main if I don’t have unmerged local commits. This syncs your fork perfectly without duplicates. Got unmerged local work? Stash it first with git stash, do the reset, then git stash pop to get it back. Keeps your fork clean and prevents those messy duplicate commits.