I brought uncommitted changes to another branch, now I can't move them back

Viewed 199

I'm using Github Desktop because I don't understand git very well.

All I did was change some code in one branch then, without committing my changes, switched to my master branch (bringing along all my uncommitted changes). Now when I try to go back to the original branch with the same uncommitted changes, Github Desktop gives me an error:

"error: you need to resolve your current index first"

I believe it thinks that I'm trying to merge code into my master branch? Although I'm pretty sure I never had uncommitted changes resting in the master branch before hand.

I can see all my changes are in inline in the master branch code but with extra lines like "<<<<<<< Updated upstream" and ">>>>>>> Stashed changes" that surround them.

I'm not sure what do to as I don't want to merge the code like this if that's what it's really asking me to do. I just want to move my changes back to the other branch as if I had never moved them in the first place.

And again, since they are uncommitted changes, I can't just revert. Right?

I don't know what compelled me to bring my changes to the master branch in the first place anyways.

1 Answers

So, I think we might need a bit more info here, however I will continue with some assumptions.

I assume that github desktop did a git stash then changed branch and then popped the stash... but this resulted in a merge conflict, so... The first thing to do is the take a complete copy of the folder containing your repo - back it up. Now we can work safely on your repo and try a few things.

So when the stash pop merge fails (due to conflict) I believe that the stash is not deleted. You can check this by doing: git log --graph --oneline --all --decorate - you should see something like (see the refs/stash):

*   edc243c (refs/stash) WIP on test_branch: 85a2f16 added test3   <---- THIS
|\
| * 005e78b index on test_branch: 85a2f16 added test3
|/
* 85a2f16 (test_branch) added test3

So, we should have your local changes that where stashed in a separate commit and we can safely undo the merge and go back to your old branch and unstash the changes (if my assumption is correct). So here are the steps:

  1. Clear/unstage the merge: git reset HEAD --hard (again, make sure you backed up first)
  2. Now checkout your original branch git checkout my_branch
  3. Now unstash your changes: git stash pop

Now you should see your modified files appear back, and when you do git log --graph --oneline --all --decorate you will see that the stash has been removed (popped).

Related