It is in the middle of the night… Suddenly, you have a great idea for one of the projects you are working on… Your run to your PC… Start typing and coding like a boss… Proudly nodding about your done work, you head over to your console to commit the awesome stuff you wrote to your git repository - But wait! What’s this? You are on the patch#344 and not on the awesome_new_feature#211 - branch, where your changes are supposed to go. - Let’s learn how to resolve this mess!

First, I must admit that I am writing this post, because this little mistake happens to me way to often and I only recently discovered an easy solution for it, thanks to this stackoverflow thread.

Without further talking, let’s dive into it: Let’s assume your working tree is clear beside these you new changes you made. In this case just do the following:

git stash
git checkout the-correct-branch
git stash pop

Easy, isn’t it? We are simply utilizing git’s awesome stash function. It allows you to first move some changes you made from the working tree to some “virtual pile” in the background and to get them back into your working tree later on. So to break it down step by step: First, you throw all the changes you made onto your background pile. After this, our directory is clean again and we can switch branches by calling the checkout command. Now we only need to use stash pop to get our uncommitted changes back and to remove any evidence of them from our virtual pile; you can also use git stash apply, but when using apply a version of your changes will remain on top of the pile.

But what, if I have other uncommitted changes in my repository besides the awesome new feature I implemented in the middle of the night? - This is maybe a topic for another day; but here is a hint: Use interactive patch staging from the command line or within the git gui to commit these other changes and then follow the guide above.