After some issues with our hosting, we decided to move our Git repository to GitHub. So I cloned the repository and tried pushing that to GitHub. However, I stumbled upon some errors we have never encountered before:
C:\repositories\appName [master]> git push -u origin master
Counting objects: 54483, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (18430/18430), done.
error: object 9eac1e639bbf890f4d1d52e04c32d72d5c29082e:contains duplicate file entries
fatal: Error in object
fatal: sha1 file '<stdout>' write error: Invalid arguments
error: failed to push some refs to 'ssh://git@github.com/User/Project.git'
When I run fsck
:
C:\repositories\appName [master]> git fsck --full
Checking object directories: 100% (256/256), done.
error in tree 0db4b3eb0e0b9e3ee41842229cdc058f01cd9c32: contains duplicate file entries
error in tree 9eac1e639bbf890f4d1d52e04c32d72d5c29082e: contains duplicate file entries
error in tree 4ff6e424d9dd2e3a004d62c56f99e798ac27e7bf: contains duplicate file entries
Checking objects: 100% (54581/54581), done.
When I run ls-tree
with the bad SHA1:
C:\repositories\appName [master]> git ls-tree 9eac1e639bbf890f4d1d52e04c32d72d5c29082e
160000 commit 5de114491070a2ccc58ae8c8ac4bef61522e0667 MenuBundle
040000 tree 9965718812098a5680e74d3abbfa26f527d4e1fb MenuBundle
I tried all of the answers already given on this StackOverflow question, but haven't had any success. Is there any way I can prevent this repository and its history from being doomed?
Method 1.
Do the
git fsck
first.If this won't fix the problem, you're in trouble. You can either ignore the problem, restore the repository from the backup, or move the files into new repository. If you having trouble pushing the repo into github, try changing the repository to different one or check: Can't push to GitHub error: pack-objects died of signal 13 and Can't push new git repository to github.
The below methods are only for advanced git users. Please do the backup before starting. The fix is not guaranteed by the following steps and it can make it even worse, so do it for your own risk or education purposes.
Method 2.
Use git ls-tree to identify duplicate files.
As you can see, it contains the duplicated file entries (commerce_coupon_per_user)!
Again, you can see the duplicated file entries (commerce_coupon_per_user)!
You may try to use
git show
for each listed blob and check the content if each file.Then keep running ls-tree for that invalid ls-tree object across your different git clones to see if you can track the valid object, or if all are broken.
If this won't help, you can undo the change by:
Method 3.
When you know which file/dir entry is duplicated, you may try to remove that file and re-create it later on. In example:
Read more:
Method 4.
Check your commit for invalid entries.
Lets check our tree again.
It seems the above commit is invalid, lets scan our git log for this commit using one of the following commands to check what's going on:
In this particular case, our commit points to the bad object, because it was commited as part of git subproject which doesn't exist anymore (check
git submodule status
).You may exclude that invalid object from the ls-tree and re-create tree without this bad object by e.g.:
Note: The old object should still throw the duplicate file entries, but if you've now duplicates in the new tree, then you need to remove more stuff from that tree. So:
Now lets try to remove all commits and blobs from that tree, and replace is again:
Now you have empty tree for that invalid entry.
If you have some weird changes for stage, reset your repository by:
If you'll have the following error:
Do the rebase and remove that commit (by changing
pick
toedit
):Method 5.
Try removing and squashing invalid commits containing invalid objects.
Read more: Git Tools - Rewriting History and How do I rebase while skipping a particular commit?
Method 6.
Identifying the invalid git objects by the following methods for manual removal:
for uncompressed objects (*please remove first two characters, as git uses it for the directory name):
for compressed objects
See: How to unpack all objects of a git repository?
Related:
The only solution I have ran across is to use git-replace and git-mktree. Its not the easiest solution in the world but it does work.
Look at this link for a reference guide.
git tree contains duplicate file entries
Note: Git 2.1 will add two option to
git replace
which can be useful when modifying a corrupted entry in a git repo:commit 4e4b125 by Christian Couder (
chriscool
)--edit
And commit 2deda62 by Jeff King (
peff
):replace: add a --raw mode for --edit
However, some forms of corruption break the tree-walker, in which case our pretty-printing fails, rendering "
--edit
" useless for the user.Knowing how Jeff is used to debug Git (like in this case), I am not too surprised to see this option.