~ 1 min read

Git - Salvaging Lost Commits

If you have not run a gc yet and it has been less than 2 weeks (default gc setting) since the commits you lost were created you are in luck.

STEP 1 - fsck

run git fsck --lost-found to identify the dangling commits

You will get something like this:

Checking objects: 100% (7151/7151), done.
dangling commit fa8249bd69c8cdf796e84a35dbb41d7d0c8e8930
dangling commit ce46084419bb1dfdb150c3af289efc1aafc5c066
dangling commit 934bf7ca1a24cea55cbef66d330b522ea0237339
dangling commit 9e0e15c2842838f1e0ad408ddc3ebe106454f962
dangling commit 36d1659104054279036e25c2d18406bddfdb848e
dangling commit dfe36b11aeb25b1164167563f9d81211914df4f1
dangling commit 1665174227ec2bda0a1edb5353427a4fdee239ba

Great! You have the commits but which one is which? git show to the rescue

STEP 2 - show

find the commit you need with git show COMMIT_HASH e.g. git show fa8249bd

Once you find the commit you would like to get back to - last step is to merge it to your current branch

STEP 3 - merge

merge, e.g. git merge fa8249bd