Github - how to keep your forked repo in sync with the original while updating the code

Viewed 485

So I am almost at the point of my 1st PR at an open source project. I know how to keep my forked/local repo in sync with the original. But I am contributing to a code since last 1 week and in the meantime, the original repo got ahead with new commits.

So how should I sync my forked/local repo in sync with the original without loosing the changes I made locally?

1 Answers

Your local repo can have more than one remote repo. If you forked a repo on a place like GitHub, and then cloned that locally, you already have one remote, probably called origin. What you can do is add a second remote to your local repo, called something like upstream:

git remote add upstream https://original/repo/url
git fetch upstream

Now let's say you've been doing work in a local branch called my-feature, and you want to update it with the most recent changes from master in the upstream repo.

git checkout my-feature
git merge upstream/master

Then you resolve any conflicts, commit the merge to your local repo, and push the changes to origin like any other commit.

Related