1
  • Simple doing merge between branch gives unwanted code.
  • Cherry picking is not helping since relevant code spilled over multiple commits
  • rebase? not sure about it
neduma
  • 101
  • 8
  • 2
    Having relevant code split over multiple commits does not render cherry-picking useless. You just need to cherry-pick multiple commits, possibly using -n. It's much worse if single commits include too much code. – William Pursell Jan 30 '11 at 11:36

1 Answers1

2

For a common set of files truly common between two branches, one solution is indeed rebase (before merge), but that supposes:

  • you haven't pushed yet your branch to a remote repo (because you are about to change the history)
  • you have all modifications on the common set of files localized in some commits (in other word, a commit don't contain both topic-specific and some common modifications)

If your commits are well identified as "common" or "topic specific", then you can change their order, making the ones with common code the most recent commits on your branch.

From there, it it easy to :

  • rebase those commits onto a 'common' branch
  • merge back that branch in your current topic branch (n order to get back all those changes)
  • merge that same 'common' branch to the other 'topic2' branch (for it to also have the same common evolutions)

See git: how do I merge between branches while keeping some changesets exclusive to one branch?

t1a--t1b--ca--cb--t1c  # topic1 with some common commits in it 'ca' and cb'
t2a--t2b               # second topic branch

rebase --interactive on topic1 branch:

t1a--t1b--t1c'--ca'--cb'  # topic1 with some common commits in most recent position
t2a--t2b               # second topic branch

rebase --onto (or simply reset the HEAD of topic1 and checkout -b common, if common branch didn't exist yet), as show in "merge between branches while keeping some changesets exclusive to one branch?"

t1a--t1b--t1c'  # topic1 without any common code
ca''--cb''      # common branch
t2a--t2b        # second topic branch

Merge back the common changes in both topic branches:

t1a--t1b--t1c'--c1  # topic1 with a commit with common code evolutions
ca''--cb''          # common branch
t2a--t2b--c1'       # second topic branch with same common code evolutions
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250