Git – Finding Lost/Dangling Commits

Losing a git commit is not fun. It can happen if you are in detached head mode and make a commit, then switch to a branch. Suddenly your commit might be lost in the git ether.

Here is how you can get a list of all dangling commits and blogs

$ git fsck --lost-found
Checking object directories: 100% (256/256), done.
Checking objects: 100% (4067/4067), done.
dangling blob 070388aded376a093f0e857d683234253780b84fd
dangling blob 47475e883a97428e85aef45a4c7895b211a0828ee
dangling blob 3cc81aef174d2392469d6409470f01eb6ed75ab08
dangling commit b9e11968670adfa0112f097e3dc708cb920a90261
dangling blob b755d92db0b404d8a43e7d647f91e0a9fba250e037
dangling commit d2e88d50104b7fdccf89a74773957fe9c2d95ab85f
dangling commit 73efaff27c6ce977f595176ef07644be2d6e70a9c
dangling blob 0df117d4f99adbe9912554042e3935b1dcadd10474
dangling blob 93b35497a6c5c225678dc52506524d9d8b221fcbe
dangling commit eaf494769766c4dc8f76238b93d5cb0a8bc806af178c
dangling blob 2cf6b48e463542427b424154271e15b9a268c4703a
dangling blob b03e18c49cb35e246c8b0bf8c9347866d0898148cb

If you want to get the details of the lost/dangling commits you can use this one liner:

$ git fsck --lost-found | grep "^dangling commit" | sed "s/^dangling commit //g" | xargs git show -s --oneline
d123e433 some commit message here
3e4af334 adds bar feature
ebd4331 fixes foo bug