1

Newly transitioned from svn to the world of GIT.

I have my master branch: A(initial commit) - B - C - D - E

My feature branch: A(initial commit) - B - Z

I want to rebase my feature branch to master such that: A B C D E Z

How could this be possible? I try to checkout to feature branch and do git rebase master, but apparently it gives me lot conflicts and newly added files of feature getting deleted. I understand that it is rebasing like A - B - Z - C - D - E

Chirag Dhyani
  • 863
  • 11
  • 24
  • I think I misinterpreted your question at first, because I assumed the commits were shown left to right, which is the most common (in English, at least). Am I right in thinking you're showing them going *backwards* in time, so Z and E are the most recent, and A is the oldest? – IMSoP Jul 22 '19 at 17:23
  • @IMSoP Right, that's what I was thinking. – iBug Jul 22 '19 at 17:23
  • If you have conflicts, you're going to have to resolve them one way or another. [Git mergetool](https://git-scm.com/docs/git-mergetool) may assist. – InfiniteHigh Jul 22 '19 at 17:33
  • 1
    I think it's `git rebase --onto`. – mrek Jul 22 '19 at 17:54
  • 2
    To clarify before answering: if you what is you say is correct, your `git rebase master` command should work. The heavy conflicts you describe seem to be related to a change in master like another rebase of the base branch. Could you ensure your commit hashes actually match and if not post an update? Thanks! – Haralan Dobrev Jul 22 '19 at 19:57
  • @IMSoP: Yes indeed!, Edited. – Chirag Dhyani Jul 23 '19 at 04:06

2 Answers2

1

Following saved my day (thanks to comment from @mrek. His advice gave the direction):

git checkout featurebranch

git rebase --onto masterbranch featurebranch^

(^ denotes parent commit of featurebranch referenced with featurebranch^)

For more information: git rebase --onto explained

Chirag Dhyani
  • 863
  • 11
  • 24
1

This will do exactly what you need, assuming your feature branch was created from master:

git checkout feature
git rebase master
# fix possible conflicts
git checkout master
git merge feature

Firstly, git rebase master on your feature branch will get Z and move it to a temporary space. Then bring C, D and E to feature and finally apply Z from the temporary area on top of E.

Resulting in A B C D E Z

At this point, if there is any conflicts you could simply run git mergetool, solve the conflicts and then git rebase --continue.

If you run into any issues while fixing conflicts you can cancel, run git rebase --abort and try again.

After this, the subsequent merge back to master will be a fast-forward (no conflicts).

I think this is a good approach because you can keep your feature branch up-to-date with master with your new code always on top of it and you can solve any potential conflicts directly on your feature branch.

Also the merges to back master are always clean and you can create a merge commit by using git merge feature --no-ff.

Just as a note, this approach has been working for me for a long time. I use kdiff3 as mergetool.

rbento
  • 9,919
  • 3
  • 61
  • 61