0

We have a library with several versions which can be used in parallel.

Each version is in its own branch because a lot of code is shared but some things need to be different.

Developers should be working on different branches in parallel and merge forth and back as needed.

I don't know how to do this because with merge some changes cannot be preserved when merging back.

Here's a simple example, expressed in shell script form:

#!/bin/bash
rm -rf test
mkdir test
cd test
git init .

echo keep a diff > test.cpp
echo merge >> test.cpp
git add .
git commit -a -m a1
git branch -m a
git branch b

sed -i -e 's/merge/merge a1/g' test.cpp
git commit -a -m a2

git checkout b
sed -i -e 's/keep a diff/keep b diff/g' test.cpp
git commit -a -m b1

git merge --no-ff a
# manual resolve
echo keep b diff > test.cpp
echo merge a1 >> test.cpp
git commit -a -m "m1a->b"

git checkout a
echo a3 >> test.cpp
git commit -a -m a3

git merge --no-ff -m "m1b->a" b # hoping to get a conflict on the first line of test.cpp

print_file() {
    git checkout $1
    echo $1:
    cat test.cpp
    echo $1 end
    echo
}

echo
print_file a
print_file b

It can be run online.

Its graph looks like this:

a1---a2--a3-mb
 \    \    /
  b-b1-mf--

I'm basically trying to preserve the first line with its differences per branch and merge all other lines. The problem occurs when merging back where the first line is overwritten.

I was hoping to have the following state in the two branches:

branch a, file test.cpp:

keep a diff
merge a1
a3

branch b, file test.cpp:

keep b diff
merge a1

but instead, for branch a, i get

keep b diff
merge a1
a3

I tried all different merge strategies but it seems there's always a fast forward possible, e.g. no conflicts are present.

Is there a way do this? I can use a manual merge tool which ignores the common ancestor but this is just a simple example and when merging many commits at once i'd like to preserve them.

ridilculous
  • 307
  • 8
  • The short answer is no: as you observed, if Git decides that the file can be handled without conflicts, it won't even run any custom merge driver you define. See the linked duplicates in [this question](https://stackoverflow.com/q/71677511/1256452) and see if any of the techniques there will get you what you need (none of them get you what you *want* though). – torek Mar 30 '22 at 21:35

0 Answers0