3

There are two branches. The main branch and branch with one feature. But the feature branch is in conflict with the main. People told me, that I should rebase the feature branch against the main.

Does it mean git rebase origin/main (and I am on the feature branch) or git rebase feature_branch (and I am on the main branch)?

It is important that during the git merge PRODUCTION there should be no conflict and it has to be solved by the command rebase. How?

mare.zim
  • 199
  • 3
  • 12

1 Answers1

8

That would mean:

git fetch
git checkout feature
git rebase origin/main

You replay locally feature branch on top of the updated origin/main.
That gives you a chance to resolve any conflict locally, and then push the feature branch (a git push --force since its history has changed): make sure you are the only one working on that branch.

(as aduch mentions in the comments, if there were merges from origin/main to feature before, then a git rebase -p origin/main could be needed in theory. See "How to rebase only the commits after the latest merge?".
But that would also mean that, in that case, a git merge would be preferable to a git rebase, which have the added complexity of preserving past merges)

If you were not the only one working on that branch, then an alternative would be to merge origin/main to feature, solving any conflict there, before pushing feature (regular push).

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I did the three steps, then I resolved conflicts. And then I was not on any branch. Before commit it asked if I want to switch to some branch, I wanted to commit it to, so I switched to feature branch, there was conflict between the actual things and the feature branch, I merged it and now I am where I was before. The feature branch is still in conflict with the main. – mare.zim Sep 06 '15 at 18:55
  • @user3599497 try the same step in a new clone. Make sure you can cleanly checkout feature (there should be no message). Then start the merge. – VonC Sep 06 '15 at 18:56
  • Isn't `-p` option recommended to preserve merge commits? – axelduch Sep 06 '15 at 19:08
  • @aduch only if there were merge commits to preserve... in which case, I wouldn't do a rebase anyway. I would do a new merge. I have edited the answer to reflect your comment. – VonC Sep 06 '15 at 19:15