Search code examples
gitgit-stashgit-revert

Wrongly applied git stash to a different branch


I was initially working on a branch, say A and stashed some of the changes in that branch using git stash save <message>. Upon completion of my work in that branch, I switched to a different branch B, and then wrongly typed git stash apply thinking I was on branch A, which resulted in conflicts in couple of files on branch B.

I tried doing git checkout and git reset HEAD on those files, but it says the files are unmerged and I am unable to bring my branch back to the original state.

Is there any way I could bring my branch B to original state or revert the stash I applied to branch B wrongly?

UPDATE : As per the latest answer, running git reset HEAD and then git checkout -- does fix the issue. But it might be time consuming if there are several files in the list as in my case, is there a better way to reverse all these changes in just one attempt?


Solution

  • For the unmerged files if you don't want the changes you have to do:

    git reset HEAD <filename>
    git checkout -- <filename>
    

    Git always tells you what to do to get the files to a different state (add to staging, discard changes) when you do git status. Most of the time following the instructions on screen will help.

    To get the whole branch to the state of the last commit HEAD (dot [.] means all in the current working directory):

    git reset HEAD .
    git checkout -- .