git fsck --lost-found
This command verifies the integrity of Git's object database and gathers any objects that are no longer referenced into a dedicated folder under .git, making it easier to review lost commits or blobs.
The fsck tool performs a consistency check on the repository and its object store, while the --lost-found flag directs Git to write any dangling blobs or commits it discovers into .git/lost-found/other and .git/lost-found/commit respectively, so you can manually inspect or restore them later.
Variations include git fsck --unreachable to only list unreachable objects without writing them out, git fsck --full for a more thorough check including connectivity, or combining --full and --strict for the strictest verification; complementary commands like git gc --prune=now or exploring recent history via git reflog can help clean or locate objects in tandem.