Search code examples
gitgit-stash

GIT stashing and switching between branches issue - work from one branch appeared on the other without merging


I was working recently with multiple branches at once (3 branches to be exact, let's call them A, B and C).

I was on branch A and then decided to switch to B, but I had some work, not ready for commit. So I stashed it and switched.

After I reveiwed code on B, I switched to C, to make some immediate corrections. Again, I needed to switch to B, so I stashed them.

Then I decided to switch to A branch, in order to start working from last "checkpoint" I applied last stash.

Then strange things started to happen - I was seeing partial changes from branch C.

I know it is messy workflow, but today it happened to me...

Since I didn't merge anything, how is it possible to happen?

...the worst part is I deleted all stashed except one I appied, thinking it is some old, unnecessary stash.


Solution

  • It's all about stashes...

    They are shared between branches. If you stash work on one branch it can be easily applied (so, it is visible from other branch) on another branch.

    I must have applied stash from branch C to branch A. That's why I was seeing partial work from A - because I applied only a stash, not whole branch.

    HOW TO RECOVER LOST STASH

    When stashing, the special kind of commit is

    After deleteing it, it still can be found in GIT history, as @Code-Apprentice mentiond and can be inspected with git reflog, which is just shhortcut for

    git log --reflog --oneline
    

    In my case lost commit appeared as:

    b10b2fc (refs/stash) WIP on branch: 78beda0 [COMMIT MESSAGE]
    

    So I can merge this commit into my branch (it means to me a couple of conflicts, but it is better than just redoing everything):

    git merge b10b2fc