title: How to merge submodules and always keep "ours"?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>I found some <a href="https://medium.com/@porteneuve/how-to-make-git-preserve-specific-files-while-merging-18c92343826b" rel="nofollow noreferrer">instructions</a> for how to always use "ours" when merging <em>files</em>, but it seems ineffective for submodules.  Here's how to reproduce the problem with a submodule:</p>
  
  <pre><code># 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' &gt;&gt; .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 &gt; 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 &gt; 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 -
  </code></pre>
  
  <p>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.</p>
  
  <p>How can I achieve easy merging in such a situation?</p>