How to update topic branch with upstream changes on master?

不羁岁月 提交于 2019-12-03 03:24:34

Let me rework your example for clarity.

    C-D-E < topic, origin/topic
   /
A-B < master, origin/master

Then someone does work.

    C-D-E < topic, origin/topic
   /
A-B-F-G < origin/master
  ^
master

You fetched F & G from origin, and then rebased topic onto master. So now your repository looks like this.

    C-D-E < origin/topic
   /
A-B-F-G < master, origin/master
       \
        C'-D'-E' < topic

And this is the problem. origin/topic at E can not be fast-forwarded to topic at E'. Rebase is really only meant for commits that have not been pushed to origin. Since you pushed C, D, and E to origin/topic already, you would have to rewrite history on the remote repository. Hence the error. So you really have three options:

  1. Stop pushing a topical branch. If it's only you who is working on topic, there's no need to push it. Just keep rebasing topic on top of master, and when done, fast-forward merge master to topic & push master. Delete local topic branch. Voila!

  2. Merge topic & master. If you need to collaborate on a topical branch, you should probably suck it up and merge.

  3. Force the remote rebase:

    git push origin topic -f

    This will force origin/topic to E'. Except by re-writing history in the remote repository, you'll have fire and brimstone, human sacrifice, dogs and cats living together, mass hysteria... and your fellow developers not liking you very much. Not recommended at all.

; update master
git checkout master
git pull --rebase origin master  

; rebase topic
git rebase master topic

; push topic (force)
git push -f origin topic

There are some problems with your question.

  1. You will not get merge conflicts when you push. This can only happen when you merge, rebase, cherry-pick or stash pop/apply.
  2. You cannot avoid conflicts if someone else is changing the same content as you are at the same time (even though it may be just you working on another copy of the repo).
  3. "All of my commits on topic are seen as uncommitted" doesn't make sense. You probably mean that "all of the changes that my topic branch commits introduce" are not committed yet due to a conflict when I try to rebase.

Your steps are correct, except that you are ignoring that your rebase has stopped on a conflict and you are simply ignoring it. You must resolve your conflicts and then:

git add confilctedfile1.txt conflictedfile2.txt
git rebase --continue

You may need to do this for all of your commits in your topic branch. Only when the rebase is complete should you push.

Another note: For this reason, rebase is not the preferred workflow for day-to-day work. Merge is much easier and more accurate in what is happening with the code base. There are many reasons to favour merge over rebase.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!