Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get git fsck to show commit names?

Tags:

git

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?

like image 797
IcedDante Avatar asked Jun 10 '15 15:06

IcedDante


1 Answers

This works on Linux and Mac:

git fsck --lost-found | grep "dangling commit" | \
   cut -d" " -f 3 | xargs -I "{}" git --no-pager show --stat "{}"

With pager and more like git log (kudos to Rafał Cieślak):

git fsck --lost-found 2>/dev/null | grep "dangling commit" | \
    cut -d" " -f 3 | \
    xargs -I "{}" git --no-pager show --no-patch --format=format:"%h <%an> %s%n" "{}" | \
    less

I'm not sure if you have grep and xargs in the Windows git shell.

Explanation:

  • git fsck finds the data
  • grep reduces that to only the dangling commits
  • cut leaves only the commit ID
  • xargs runs the command git show --stat with the IDs

[EDIT] Fixed for cases when there are several dangling commits.

like image 172
Aaron Digulla Avatar answered Oct 11 '22 13:10

Aaron Digulla