Git repository broken by interrupted commit

1.3k Views Asked by At

I broke my local git repository by interrupting a git commit with Ctrl+C (two commits back). How can I fix it ?

Output of git stash :

$ git stash
fatal: bad revision 'HEAD'
fatal: bad revision 'HEAD'
fatal: Needed a single revision
You do not have the initial commit yet

Output of git fsck --lost-found :

$ git fsck --lost-found
notice: HEAD points to an unborn branch (Multi-Threading)
Checking object directories: 100% (256/256), done.
Checking objects: 100% (67/67), done.
error: refs/heads/Multi-Threading: invalid sha1 pointer 0000000000000000000000000000000000000000
dangling commit 2d5af11417b9508ece28c1bb1502e5299a2fa2d0
dangling commit 3b0dfd77c49c12a23469c036db7f45378a1bf740
dangling commit 47d212cf4c018b9f3544325a26c90f74d3323489
dangling commit 82674535931943f64b4a3475c14475591d84a318
dangling commit 83604cf338ccb0491081f7f27c2217bc11fba0c2
dangling blob a3133e60fe8fec7977270d1e93c0869e169024f1
dangling commit aae880196744421d1ffbf7dc23aa8965d4ee1f46
dangling blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
dangling commit f9558f1235c4a239b2c8d0677d2b1c31eb400836

Output of git reflog :

$ git reflog
fatal: your current branch 'Multi-Threading' does not have any commits yet

It's also impossible to write in .git/refs/heads/Multi-Threading :

$ echo 47d212cf4c018b9f3544325a26c90f74d3323489 > .git/refs/heads/Multi-Threading 
An error occurred while redirecting file '.git/refs/heads/Multi-Threading'
open: No such file or directory
2

There are 2 best solutions below

0
On BEST ANSWER

My problem was due to a system error.

I restarted, re-executed echo 47d212cf4c018b9f3544325a26c90f74d3323489 > .git/refs/heads/Multi-Threading without error and it fixed my issue.

0
On

Interrupting Git is not supposed to break the repository (Git catches keyboard interrupt signals and handles them gracefully), but at this point, you need to figure out what happened to .git/refs:

  • Does it exist as a directory?
  • Does it contain a file named packed-refs? If so, use the contents of this file carefully. If not, that's probably OK.
  • Does it contain subdirectories heads, remotes, and/or tags? The heads one should definitely exist, the other two will be created if and when necessary.
  • If .git/refs/heads/ does exist, why do you get an error when trying to create a file named Multi-Threading within it?

Putting 47d212cf4c018b9f3544325a26c90f74d3323489 into a file named .git/refs/heads/Multi-Threading does looks like it's a correct approach (though I do not know why you picked 47d212cf4c018b9f3544325a26c90f74d3323489 as the specific commit to use; note that the ones shown by git fsck come out in somewhat random order; but one of those dangling commits is likely the correct one).

(The packed-refs file, if it exists, contains the values of references that are have been packed so as to occupy a single file instead of many separate files. Whatever format it has is the one Git likes. Note that creating a .git/refs/heads/<name> file will override the corresponding packed-refs value.)