I have a git repo with a second repo as a submodule. Both repos have corresponding branches for various features which usually correspond pretty directly, e.g. if the parent repo is on branch A, the submodule will also be on branch A.
In both the parent and the submodule repos, as time goes on more branches are added, and sometimes I merge older branches into later ones, e.g. merge branch A into branch B periodically in both repos, while branch B is also getting new commits which are not in A.
The problem comes when I merge branch A into B for the parent repo after the A submodule was updated. I want to ignore those updates (i.e. I always want to discard submodule updates during merges, otherwise the parent branch B might end up with submodule branch A which is wrong).
I found some instructions for how to always use "ours" when merging files, but it seems ineffective for submodules. Here's how to reproduce the problem with a submodule:
# first command will update your ~/.gitconfig, you may want to undo it later
git config --global merge.ours.driver true
mkdir -p tmp/sub
cd tmp
git init
git commit --allow-empty -m "chore: Initial commit"
echo 'sub merge=ours' >> .gitattributes
git add .gitattributes
git commit -m 'chore: Preserve sub during merges'
mkdir sub
pushd sub
git init
git commit --allow-empty -m "sub: init"
popd
git submodule add ./sub
git commit -m 'add submodule'
git checkout -b demo-prod
pushd sub
echo prod > readme
git add readme
git commit -m 'add prod readme'
popd
git commit sub -m 'sub: add prod readme'
git checkout -
git submodule update
pushd sub
echo master > log
git add log
git commit -m 'add master log'
popd
git commit sub -m 'sub: add master log'
git checkout demo-prod
git merge -
The expected result is an automatic merge, but the actual result is a merge conflict because "sub" has new commits in both branches of the parent repo.
How can I achieve easy merging in such a situation?