title: git fsck: duplicateEntries: contains duplicate file entries - cannot push to gitlab
body: <p>We have a big git repository, which I want to push to a self-hosted gitlab instance.</p>
  
  <p>The problem is that the gitlab remote does not let me push my repo:</p>
  
  <pre><code>git push --mirror https://mygitlab/xy/myrepo.git
  </code></pre>
  
  <p>This will give me this error:</p>
  
  <pre><code>Enumerating objects: 1383567, done.
  Counting objects: 100% (1383567/1383567), done.
  Delta compression using up to 8 threads
  Compressing objects: 100% (207614/207614), done.
  remote: error: object c05ac7f76dcd3e8fb3b7faf7aab9b7a855647867: 
  duplicateEntries: contains duplicate file entries
  remote: fatal: fsck error in packed object    
  </code></pre>
  
  <p>So I did a git fsck:</p>
  
  <pre><code>error in tree c05ac7f76dcd3e8fb3b7faf7aab9b7a855647867: duplicateEntries: contains duplicate file entries
  error in tree 0d7286cedf43c65e1ce9f69b74baaf0ca2b73e2b: duplicateEntries: contains duplicate file entries
  error in tree 7f14e6474400417d11dfd5eba89b8370c67aad3a: duplicateEntries: contains duplicate file entries
  </code></pre>
  
  <p>Next thing I did was to check <code>git ls-tree c05ac7f76dcd3e8fb3b7faf7aab9b7a855647867</code>:</p>
  
  <pre><code>100644 blob c233c88b192acfc20548d9d9f0c81c48c6a05a66    fileA.cs
  100644 blob 5d6096cb75d27780cdf6da8a3b4d357515f004e0    fileB.cs
  100644 blob 5d6096cb75d27780cdf6da8a3b4d357515f004e0    fileB.cs
  100644 blob d2a4248bcda39c0dc3827b495f7751b7cc06c816    fileC.xaml
  </code></pre>
  
  <p>Notice that <code>fileB.cs</code> is displayed twice, with the same hash. I assume that this is the problem, because why would the file be two times in the same tree with the same file name and blob hash?</p>
  
  <p>Now I googled the problem but could not find a way how to fix this. 
  One seemingly good resource I found was this: <a href="https://stackoverflow.com/questions/13178720/tree-contains-duplicate-file-entries/24868719#24868719">Tree contains duplicate file entries</a></p>
  
  <p>However, it basically comes down to using git replace which does not really fix the problem, so git fsck will still print the error and prevent me from pushing to the remote.</p>
  
  <p>Then there is this one which seems to remove the file entirely (but I still need the file, but only once, not twice in the tree): <a href="https://stackoverflow.com/a/44672692/826244">https://stackoverflow.com/a/44672692/826244</a></p>
  
  <p>Is there any other way to fix this? I mean it really should be possible to fix so that git fsck does not throw any errors, right? I am aware that I will need to rewrite the entire history after the corrupted commits. I could not even find a way to get the commit that points to the specific trees, otherwise I might be able to use rebase and patching the corrupted commit or something. Any help would be greatly appreciated!</p>
  
  <p>UPDATE:
  Pretty sure I know <em>what</em> to do, but not yet <em>how</em> to do it:</p>
  
  <ol>
  <li>Creating a new tree object from the old tree, but corrected with <code>git mktree</code> &lt;- done</li>
  <li>Create a new commit that is identical to the old one that references the bad tree but with the newly fixed tree &lt;- difficult, I cannot easily get the commit to the tree, my current solution runs like an hour or more and I do not know how to create the modified commit then, once I have found it</li>
  <li>Run <code>git filter-branch -- --all</code> &lt;- Should persist the replacements of the commits</li>
  </ol>
  
  <p>Sadly I cannot just use <code>git replace --edit</code> on the bad tree and then run <code>git filter-branch -- --all</code> because <code>filter-branch</code> seems to only work on commits, but ignores tree-replaces...</p>