stdcerr
stdcerr

Reputation: 15598

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?

Upvotes: 14

Views: 26330

Answers (2)

JOHN OGINGO
JOHN OGINGO

Reputation: 21

//fetch from upstream
git fetch upstream
git checkout main
git merge upstream/main
git push origin main

Upvotes: 1

b-fg
b-fg

Reputation: 4137

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

Upvotes: 20

Related Questions