IcedDante IcedDante - 1 year ago 71
Git Question

Can I get git fsck to show commit names?

I have a deleted commit, among many deleted commits, that I'm trying to restore. I found out about the fsck --lost-found command. Great!

The problem is I have over a hundred dangling commit statements with very little information.

dangling commit 654857f5e8418c4031e1d8411579906c528da562
dangling commit 74499bd482d688c1416d5091b391d82a438855a9
dangling commit 124ed7cd4465434865577c82757732df62febb59
dangling commit 92573bf4595be6f80f22eba94548dbc88d8796fc
dangling commit 125b0ffa3f0db71f23fda65d6adb2f9941748cc0
dangling commit ba5b1f8d6d920900abc88bd725d44ba86c8c772f
dangling blob e760d751ae4e3dab9beed0996e683c0f291eb4cc

If it could just throw out the commit name with the sha that would be a big help. As it is, I have to run the git show on each one, one-by-one, to find the right commit. Is there an easier way?

Answer Source

This is not an answer solving this with git fsck, but something which might work in this situation.

git reflog outputs a list of past HEADs in the repository. For example, if you switch from a branch master to a branch devel, this produces a reflog entry with the HEAD of the master branch.

So if the commit was part of a branch at some time, the chances are great that you can recover it using a combination of git reflog, git log and a bit of manual work.

There are a few cases:

  1. Just fire up git reflog and scroll through it. If it shows the commit message you were looking for, that’s great! Copy the commit ID and use it for whatever you want (e.g. git cherry-pick, or save it into a branch using git branch save-my-commit commitid).

  2. The commit is not directly in git reflog, but you see a commit which you made after the commit you want to find (and before deleting it). In that case, use the commit ID of the commit you made afterwards as found in git reflog and pass it as argument to git log: git log commitid. Scroll through the output and see if you can find your commit. Use the commit ID as in case 1.

  3. The commit is not directly in git reflog and you do not find a commit of which you know that you made it after the commit you deleted and before deleting it.

    This is the most tedious case, in which case it might be easier to go through the other processes here. I am not sure though that they will actually be listed as dangling commits, so it is worth a shot if you cannot find your commit using git fsck.

    Go over all entries in git reflog and use the commit IDs as argument to git log as described in case 2. Try to find the commit you are after.

Good luck!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download