简体   繁体   中英

git: cannot merge with upstream

I have made some changes on my forked repository, and I wish to overwrite a single file from upstream. The upstream repository is added as a remote. When I perform a merge, git tells me I am up to date:

$ git merge upstream/master master
Already up-to-date.

But when I run diff, git shows the missing file for my forked repository

$ git diff upstream/master origin
diff --git a/Library/Formula/exim.rb b/Library/Formula/exim.rb
deleted file mode 100644
index 48d52cf..0000000
--- a/Library/Formula/exim.rb

Is there anyway to get git to add in the missing file to my forked repository?

Upstream has the file. You had it from upstream and deleted it. 3-way merge of no change versus delete is delete, therefore the file is still not there after the merge. Once upstream/master is fully merged (which is the case given your first listed output), merging again is a no operation, because merge only considers changes since the branches were last merged and there are none on the other side.

You want to either:

  • revert the change in which you deleted the file:

     git log -- Library/Formula/exim.rb 

    will tell you the revision where you removed the file and than

     git revert <revision-id> 

    to revert it.

  • get the file's content from upstream and add it again:

     git cat-file blob upstream/master:Library/Formula/exim.rb > Library/Formula/exim.rb git add Library/Formula/exim.rb git commit 

I think you should do the merge to your local master like so :

git checkout master
git pull origin/master  # in case there are changes posted remotely
git pull upstream/master

After that you can push your local master to the remote origin with :

git push origin master

At this point upstream/master should be merged into master , which should equal origin/master .


Also your diff command doesn't make much sense, if you expect them to be equal :

git diff upstream/master origin

This compares origin/master to upstream/master but if you made changes on your master after you forked and you don't control upstream these can't be equal, it seems you expect them to be.

You're comparing upstream/master with origin/master , but merging upstream/master into master . It looks like you're confused between upstream repo (the original repo on GitHub) and origin (your forked repo on GitHub).

Once you merged changes, they're still local, if you want to make them appear in origin you need to push them: git push origin master .

Graphically you have this situation at the beginning:

A----B 
\    ^master and origin/master
 \---C 
     ^upstream/master

After merge:

A----B-----------------D
\    ^origin/master   /^master
 \---C---------------/ 
     ^upstream/master

And after pushing back to origin :

A----B-----------------D
\    ^                /^master and origin/master
 \---C---------------/ 
     ^upstream/master

The git merge command is not updating the upstream repo. You telling git to combine the changes from the upstream into your local and because you already have all the commits from the upstream in your local there is nothing to commit.

You want to "push" your changes. Run git push upstream master

http://www.lornajane.net/posts/2012/pushing-to-different-git-remotes

For your forked repo, you only need to do a git push origin master to push the master branch or just a simple git push

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM