How to sync changes between GitHub forks without duplicating commits?

Hey everyone! I’m having trouble with my GitHub fork. I made some changes and pushed them to my fork. The original repo merged my stuff along with other updates. Now I want to get those new changes into my fork without messing things up.

I tried doing a simple pull and push, but it ended up duplicating my commits. That’s not what I want at all! Does anyone know a better way to sync everything up? I’m pretty new to this whole forking and merging business, so any tips would be super helpful.

Here’s a quick example of what I’ve tried:

git pull upstream main
git push origin main

But this just doubled up my commits. There’s gotta be a smarter way, right? Thanks in advance for any advice!

hey olivias, i’ve been there! try using git rebase instead of pull. it’ll keep ur commit history clean. do this:

git fetch upstream
git rebase upstream/main
git push origin main -f

the -f is important cuz ur rewriting history. just be careful n only do this on ur personal fork!

One effective approach involves rebasing your fork’s main branch onto the upstream repository’s main branch. First, update your local main branch and ensure that the upstream remote is added. Next, fetch the changes from the upstream repository and rebase your local branch to integrate those commits. Finally, force push the updated branch to your fork. This method maintains a linear commit history and prevents duplicate commits. It is essential to backup your work before proceeding, as rewriting history can lead to complications if the branch is shared.