Search code examples
gitgit-mergegit-rebasegit-cherry-pickgit-merge-conflict

Interesting git rebase scenario with two branches


I was working with two git branches.

a -- b -- c                  <-- master
           \
            d                <-- newbranch

After reaching commit d, I realized I need to change commit c a little bit. I went into master branch and amended commit c. Now, commit c became commit e. c and e has the same commit message.

a -- b -- e                  <-- master
      \     
       c -- d                <-- newbranch

So, now my commit c is useless. Now commit c and commit e are conflicting in few files and in all the files they are conflicting I will prefer the changes of commit e to stay. Also, commit d has important changes that I don't wanna lose.

I want my final structure to be something like this.

a -- b -- e -- f             <-- master

where commit f is the amended version of commit d, having the same commit message.

I tried rebasing the master into newbranch first and was thinking of rebasing the newbranch into master later on. But as there lots of conflicts and after manually solving the conflicts I lost my way and became clueless and then lost all the manual correction and need to start rebase again.

If someone please guide me about the exact procedure to follow in my approach or have a better solution please share. Thank you.


Solution

  • The easiest thing to do here is probably to just cherry pick the d commit from newbranch onto master:

    # from master
    git cherry-pick d
    

    This would leave us with the following branch structure:

    master: a -- b -- e -- f  (*f is actually the cherry pick of d)
                  \     
                    c -- d