title: git overwrite branch but keep history
body: <p>I want to overwrite all the content in a development branch with another one (namely, master), but I don't want to miss the older commits on the dev branch.</p>
  
  <p>This is what I want:</p>
  
  <p><a href="https://i.stack.imgur.com/nHe09m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nHe09m.png" alt="what I want"></a></p>
  
  <p>I tried both to <a href="https://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch">merge with ours strategy</a> and by <a href="https://stackoverflow.com/questions/4624357/how-do-i-overwrite-rather-than-merge-a-branch-on-another-branch-in-git">doing an hard reset</a> but the dev branch keeps losing the older commits.</p>
  
  <p>What I'm trying to obtain is a commit that has zero diff between blue and red, and the dropped code while diffing blue with the first black one.</p>
  
  <hr>
  
  <p>Just to share an overview of the solutions tried until now.
  By doing this:</p>
  
  <pre><code>git checkout master
  git merge -s ours dev
  git checkout dev
  git merge master
  </code></pre>
  
  <p>I obtain this (dev gets all the history from master)</p>
  
  <p><a href="https://i.stack.imgur.com/z57LA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z57LA.png" alt="merge"></a></p>
  
  <p>By doing this</p>
  
  <pre><code>git checkout dev
  git reset --hard master
  </code></pre>
  
  <p>I obtain this (if I push develop i will lose all the black commits):</p>
  
  <p><a href="https://i.stack.imgur.com/orGbP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/orGbP.png" alt="reset"></a></p>
  
  <p>If i do this:</p>
  
  <pre><code>git checkout dev
  git merge $(git commit-tree -p dev -m "Align master &gt; dev" master^{tree})
  </code></pre>
  
  <p>I obtain this (almost perfect, yet I would be difficult to recall the exact moment when I did the trick):</p>
  
  <p><a href="https://i.stack.imgur.com/ppRHE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ppRHE.png" alt="enter image description here"></a></p>