gitgit-gcgit-untracked

find lost `git write-tree`


I have a script that uses git write-tree to make up for the deficiencies in git stash, it works perfect unless I ctrlC the script.

[... other parts of script ...]
exec('git add -f .');
workingTreeHash=exec('git write-tree');
exec('git checkout -f [...]');

At this point the I ^C the script and thus lost the write-tree hash variable workingTreeHash.

My attempt at recovery:

$ git fsck --lost-found --full --unreachable
$ git config gc.auto 0
$ (
          for i in $(ls .git/lost-found/{commit,other}/*);do
                  git show $(basename $i);
          done;
  )
  |grep -iae 'unique modifications';

showed nothing... so where did the lost git add -f .;git write-tree object go? Where can it be found. (I am aware of git workspaces, I will try that as an alternative for the script).

This isn't a high priority, just for future reference in case this happens again with something important.


Solution

  • find .git/objects/?? -type f -exec ls -t {} + | head
    

    (it's probably the first one), then git show them.

    For me, I've made a change since my last commit, so I get

    $ find .git/objects/?? -type f -exec ls -t {} + | head
    .git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
    .git/objects/e1/c72a7c34b57c7570dd13c269d3065ef27fb896
    .git/objects/1a/e2d4203a023095ce1cfc63e9b201f402234b60
    .git/objects/fc/13259859a21139736e58d3a80f15c495ee90fe
    .git/objects/01/ca0f1889baacba762a2841c9bce3c3f3927a90
    .git/objects/6a/c151c9adf00d90e55afd8da4a0534deeb27d00
    .git/objects/91/82087c12c32c2dc475798913b949a5a063d1e0
    .git/objects/ca/291ca41a16b5dcda64c063f2844337bbb16b1f
    .git/objects/35/6b4df63d859611895c730633790dd1bcdb7eb4
    .git/objects/b1/615cd532dea5e28cf57be5521cb216dcf90115
    find: ‘ls’ terminated by signal 13
    $ git show 1ae2d
    tree 1ae2d
    […etc]
    

    and the full hash would be git rev-parse 1ae2d if you don't want to type it. c72a7's the commit, e69d, you might even recognize that hash, it's an empty file I added by mistake.

    Also, if you know you haven't touched the index since writing it, git write-tree is idempotent, just do it again, you'll get the same tree.