gitmergegit-fetch

How can I merge changes from an upstream branch to my fork's branch


I want to pull changes from an upstream repo's branch. I have set the upstream repo using:

git remote add upstream http://URL-of-master-repo.git 

and then I tried to pull the changes using

git checkout my-local-branch
git fetch upstream remote-branch
git merge upstream/remote-branch

but the files still didn't appear on my disk, but I get a conflict:

Auto-merging minimal-build-config.cmake
CONFLICT (content): Merge conflict in minimal-build-config.cmake
Automatic merge failed; fix conflicts and then commit the result.

How do I properly resolve the conflict to be able to fetch the files from the upstream branch?


Solution

  • A good practice is to have this structure, which looks like you do, but is good to explain for clarification purposes:

    origin  https://github.com/your-username/forked-repository.git (fetch)
    origin  https://github.com/your-username/forked-repository.git (push)
    upstream    https://github.com/original-owner-username/original-repository.git (fetch)
    upstream    https://github.com/original-owner-username/original-repository.git (push)
    

    So origin is your fork and upstream the original repository. Then use

    git fetch upstream
    

    And you will get this output

    From https://github.com/original-owner-username/original-repository
     * [new branch]      master     -> upstream/master
    

    where you have now a branch called upstream/master which tracks the original repo.

    Then checkout to your local fork branch and merge

    git checkout master
    git merge upstream/master
    

    If you have conflicts (as it seems you do) fix them and commit the changes.

    Source