maintain different versions of the code simultaneously using git

I got a code that I’m required to optimize. I would like to maintain a set of versions of the code (each version can be described as a composition of some features, optimizations) simultaneously. Eventually, I’ll decide which version is the best one. I do not want to merge these versions into fewer versions. However, I would like to be able to make a (minor) modification to a (big) source file which may divert across the versions and I want this modification to write through more than one (possibly all) versions. How can I achieve that with git?

For example let us consider 3 versions: v1, v2, v3 and source code file source.cpp which has lots of code that is different across all versions but class A method aMethod() is identical. I would like to update the method and write the update to versions v1 and v2 only.

  • Git: Unable to understand why branch (topic) commits/merges are happening on the master branch
  • How do I publish to <me>
  • How push directly to remote server just using push?
  • How can I track multiple svn branches in git
  • How to push a new branch non-existing on the remote server without --set-upstream?
  • How do I split a branch after a certain commit?
  • How can I do that?
    If I modify source.cpp, for example, in v1, than merge it to v2 there will be a merge conflict (because source.cpp is different in v1, v2). Is there any way to avoid the conflict? If not, what is the best way to deal with the merge conflict int this case?
    By the way, I don’t want to increase code granularity so that the aMethod() will be placed in a dedicated file, because there is already lots of source code written and there will be too much overhead for doing this for any such modification that I describe.

    Thanks in advance,


  • Git commands to automatically subtree merge a project with submodules?
  • Xcode - error: pathspec '…' did not match any file(s) known to git
  • Git pull results in “needs update” and files shown as modified
  • catching a git post-receive error in a script
  • In Git, how do I create a commit that's before the oldest commit?
  • git add -p --ignore-submodules?
  • 3 Solutions collect form web for “maintain different versions of the code simultaneously using git”

    Have you tried git cherry-pick? If you do “git merge” from branch v1 to branch v2 all commits that are in v1 and not in v2 will be merged over, thats not what you want. If you just cherry-pick the single commit with your change I don’t think there will be a merge conflict.

    If the optimizations you’re making are mostly independent of each other, you could branch for each individual optimization from your starting point, and when you want to test some combination, make another branch from the starting point and do an octopus merge of the desired optimizations.

    It would be ok to test the various alternatives in a single source file.

    As you noted, git merging is designed to make versions converge, which is particularly useful when distributed teams want to work without stepping on each other toes.

    It doesn’t help you keep files distinct. cherry-pick can fill the gap, but unlike the darcs equivalent it doesn’t use the history, and may not work in many cases. Introducing your minor changes into topic branches could work, but you would need to make them dependent on an early, common revision and only merge from them, not into them.

    Since you control everything, you can test diverging algorithms while keeping them compatible and in sync, and you don’t need multiple branches.

    Git Baby is a git and github fan, let's start git clone.