How do I rebase correctly If my branch has a lot of commits?

Viewed 1090

My situations looks similar to this this except that my feature branch has a lot more commits

enter image description here

The bug fix which was committed in the meantime also affects my feature branch. This is why I would like to rebase my feature branch to the master.

I tried to do this with git rebase master (being on my feature branch) which ended up in chaos. Due my feature branch has a high count of commits, the rebase takes forever and I keep losing the overview during the rebase process and all its conflicts, with older commits and what so ever.

How would I do this correctly in this situation? What am I doing wrong?

2 Answers

'Keep losing overview' sounds like an issue with the UI you are using with Git. Many like the terminal and it is very powerful indeed but git is not an intuitive scheme and the terminal makes it worse.

I've done larger rebases using TortoiseGit with multiple merge conflicts which have only induced seemingly appropriate levels of frustration.

Learning a new git tool is of course a cumbersome solution to this problem. Then again I would recommend a proper user interface as an alternative to a text based console. It's not like the terminal goes away!

A second option would be to merge master to feature, which is probably not a good option if you want a nice linear git history.

If you want to rebase your whole history, you will somehow need to re-play each individual commit on top of master, and also do some extra work to keep the sub-feature -> feature merge.

I would suggest to not do that. To integrate bugfix into your feature branch, you could :

  • merge master (with its bugfix) into feature :

    git checkout feature
    git merge master
    

    this would be the most straight forward and git-ish way to do it

  • cherry-pick the bugfix commit onto feature :

    git checkout feature
    git cherry-pick bugFix
    

    it is not the cleanest way to do it, but it certainly works
    later on, you may have to remember that bugFix was present on both branches when merging feature into master, if the you get conflicts on the files modified by bugfix

  • first simplify the history of feature and sub-feature, and rebase the simplified versions on master

Related