Search code examples
gitgithubforkbranchupstream

How to fetch upstream and resolve conflicts for a forked branch locally?


I have forked a Github Repo and raised PR for some contribution. Before getting merged, some other commits were made to the parent repo. To fetch upstream I need to resolve conflicts with the upstream. How do I fetch upstream locally and resolve those changes?


Solution

  • You can follow the GitHub docs for that, but a quick way to do it would be:

    Assuming you created a fork and cloned your fork only:

    First thing you need to do is to add a new remote that points to the upstream project.

    In your local fork, run git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

    Fetch the remote: git fetch upstream

    Be sure that you are in your branch you want to merge and run git rebase upstream/<branch you want to merge to>

    This will move your branch to and will add the commits from your branch on top of that.

    If you encounter any merge conflicts in the process, you need to:

    • resolve the conflict(s)
    • git add the affected file(s)
    • git rebase --continue to continue with rebasing

    Once you are done, use git push --force origin <your branch> (you need to use the --force as you changed the branch history if you previously pushed your branch)