Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to do "Checkout with Rebase" and then push the merged files in Intellij IDEA

In Git, I have two branches: master and myFeatureBranch (which I created via IDEA's new branch while I was on master). In the time since I created myFeatureBranch, other team members have committed several changes to master. I would like to bring in those changes, and I am aware that I will have merge conflicts.

My general idea of how to deal with this is:

  • I get all of the commits from master and apply them to my branch
  • I deal with any merge conflicts
  • I push all of these changes, including the merged files, to the remote branch
  • There are no duplicate commits on my remote branch

Intellij IDEA 2016 added some new features including Checkout with Rebase. From their announcement:

The Checkout with Rebase action is useful if you don’t want to waste your time on extra files synchronization and compilation when you do two operations: Checkout and then Rebase.

Sounds like what I'm wanting. I am already on master, so I click on the branch selector at the bottom right of my IDEA window. I navigate to my local branches and choose "Checkout with Rebase".

Intellij IDEA Checkout with Rebase

After doing this, I am told about my merge conflicts. I choose the "Merge" button and resolve them manually.

Intellij IDEA Merge Conflict

After I do that, I get a confirmation that my rebase was successful:

Intellij IDEA Rebase Successful

And there are no pending changes:

Intellij IDEA No Pending Changes

Everything looks like it worked. Now I want to push everything, including my merged classes, to my remote branch. So I go to the menu bar and choose VCS -> Git -> Push. But when I do that, I get an error saying that my push was rejected and that I should merge:

Push of current branch <code>myFeatureBranch</code> was rejected. Remote changes need to be merged before pushing. In this case **merge is highly recommended**, because there are non-pushed merge commits. Rebasing them can lead to problems.

I decided to follow the advice and choose "Merge". However, this brought me right back to the merge conflict.

Intellij IDEA Merge Conflict

If I go through with this and then try pushing it again, it is successful, but I wind up with two copies of the merge.

What am I doing incorrectly with this "Checkout with Rebase" and then pushing my merged files?

Also, I don't want to do this from the Terminal. That may be easier for some developers, but I would like to learn how to do it entirely through the IDEA GUI (we have QA and other users who are not as technical as our developers and they have expressed a desire to not have to use the command line, and I'd like to know the best way for them).

like image 336
Thunderforge Avatar asked Jan 24 '17 21:01

Thunderforge


2 Answers

IMHO, you've got the same problem if you checkout first your feature branch, then rebase it on the master and want to push to remote.

By rebasing your feature branch, you have modified the commit history, either by use "Checkout with Rebase" or doing this manually. So you can't simply push the new commit tree to remote branch, you've got to push a new commit tree to remote.

In my case, as i usually work alone on feature branch, when i rebase it on master, I use "Force push" option when i want to push my commits on remote branch.

Force push option

In that case, git doesn't try to add new commit (calc by diff) on the top of the remote commit tree but erase the entire tree of the remote branch and replace it with the commits of your local feature branch. Be very careful if other people work with the remote feature branch, don't use this option ! they'll be in big trouble if they want to push after your forced push because git can't compare their commit history and the new history you've just publish and they can't push their commits (they can't).

There certainly a better way to do this when more than one developper work on feature branch but I don't know it.

Hope than it can help

like image 168
jolisylvain Avatar answered Sep 21 '22 22:09

jolisylvain


I also have met such problem when I did rebase. A lot of conflicts resolved and there's problem in the end. Of course, merge isn't good solution from my point, because result won't look so great.

Before rebasing, rename your origin branch which you going to rebase, and then after rebase use your old origin branch name. Or simply after rebasing push changes to new origin branch.

Main point to push to different branch, not to the same. That's help me to resolve this problem.

For example,

feature        - - - - 
             /
dev   - - - . - - - - - - . - -

Will become to:

update-feature               - - - - 
                           /
dev   - - - . - - - - - - . - - - - -

And it will be done without any problems.

I hope it helps you :)

like image 38
DefaultXYZ Avatar answered Sep 21 '22 22:09

DefaultXYZ