This is an old question but it's at the top of Google for "working on the wrong branch". I think a better answer is to simply checkout the branch you wanted to be working on. No need to stash anything:
git checkout branch123
The default behavior of checkout
is to NOT overwrite modified files in your working directory, so you won't lose anything. Based on what I found here.
Edit:
This method will only work if the files you have modified in the working directory are identical in both master
and branch123
. This was actually pointed out by Jefromi in the comments to the accepted answer, but I didn't understand what he was saying the first time I read it.