gitgit-mergegit-stash

git lost a commit after stash and merge


I had a weird problem yesterday.

Everything good so far But then realize the commit "add transaction final" was missing !

git log --graph --oneline --decorate

*   ea34d09 (react) Merge branch 'add-trans' into react
|\  
* | 0a5cd5a dark theme
* | 49b3148 added color categorie
* | 5aaf600 ordering categories
| * 60d0bc5 (HEAD -> add-trans) add new transaction. draft 1
* | aef34ed merge categories and groups selection
|/  
| * 6ddb28d (master) moved flatpickr cdn
| * 6c3954f (origin/master, origin/HEAD) show amount if 0
...

Damned ! lost 4 hours of work. What happened ?!

I know git does not erase stuff easilly, so I could find the lost hash based on the post : How do I recover a dropped stash in Git?

git log --graph --oneline --decorate --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

*   ea34d09 (react) Merge branch 'add-trans' into react
|\  
* | 0a5cd5a dark theme
* | 49b3148 added color categorie
* | 5aaf600 ordering categories
| | *   5c96e57 WIP on (no branch): faa2f21 add transaction final
| | |\  
| | | * 558aef4 index on (no branch): faa2f21 add transaction final
| | |/  
| | * faa2f21 add transaction final
| |/  
| * 60d0bc5 (HEAD -> add-trans) add new transaction. draft 1
* | aef34ed merge categories and groups selection
|/  
| * 6ddb28d (master) moved flatpickr cdn
| * 6c3954f (origin/master, origin/HEAD) show amount if 0
...

I could get back the commit faa2f21 add transaction final and merge correctly, but I'm confused... can anyone tell me what happened ?

git reflog (after I fixed the problem)

0522a6d (HEAD -> react) HEAD@{0}: commit (merge): Merge branch 'add-trans' into react
0a5cd5a HEAD@{1}: checkout: moving from add-trans to react
faa2f21 (add-trans) HEAD@{2}: merge add-trans-final: Fast-forward
60d0bc5 HEAD@{3}: checkout: moving from react to add-trans
0a5cd5a HEAD@{4}: reset: moving to HEAD~1
ea34d09 HEAD@{5}: checkout: moving from add-trans to react
60d0bc5 HEAD@{6}: checkout: moving from faa2f21c33ce8da763e8d5580c718590318ed5c1 to add-trans
faa2f21 (add-trans) HEAD@{7}: checkout: moving from add-trans to faa2f21
60d0bc5 HEAD@{8}: checkout: moving from react to add-trans
ea34d09 HEAD@{9}: commit (merge): Merge branch 'add-trans' into react
0a5cd5a HEAD@{10}: checkout: moving from master to react
6ddb28d (master) HEAD@{11}: checkout: moving from faa2f21c33ce8da763e8d5580c718590318ed5c1 to master
faa2f21 (add-trans) HEAD@{12}: commit: add transaction final
60d0bc5 HEAD@{13}: checkout: moving from react to 60d0bc5a707cf626b416a66cad6df75dc167a18c
0a5cd5a HEAD@{14}: commit: dark theme
49b3148 HEAD@{15}: commit: added color categorie
...

Thanks a lot.


Solution

  • The problem is that your first bullet point didn't happen as you thought it did:

    committed change on branch add-trans with log message add transaction final

    Instead, this line of the reflog explains exactly what happened:

    60d0bc5 HEAD@{13}: checkout: moving from react to 60d0bc5a707cf626b416a66cad6df75dc167a18c
    

    Note at the time that checkout occurred, branch add-trans was pointing to commit ID 60d0bc5. If you had checked out branch add-trans, the reflog would have said:

    60d0bc5 HEAD@{13}: checkout: moving from react to add-trans
    

    and had you done that the issue wouldn't have occurred. But by checking out the commit ID instead of the branch, you ended up in detached mode, and then when you created commit faa2f21 add transaction final it became an orphaned commit, which you later found by searching for the stash that was created while your detached HEAD was pointing to that commit.

    As for how it happened, in a comment you stated you thought you did this:

    I checked out to add-trans using the VSCode GUI, so I'm not really sure.

    Depending on which VS Code plugin you were using the exact details may differ, but in Git Graph for example, if you right click on a branch name and select "Checkout branch" it will do so, but if you right click on the commit message (just slightly to the right of the branch name), and right click and select "Checkout" it will checkout as detached. (It will also warn you of this which you can dismiss, or dismiss forever meaning the next time it won't warn you.) I'm guessing you accidentally did something similar to this and didn't realize it until after the merge.

    The fix is relatively straight forward, once you know the orphaned commit ID, which is typically easy to find by using git reflog. I can tell from your reflog entries that you did fix it.