Worked on the wrong branch? Use git stash!

Did you just realize that you worked on a wrong branch? What do you do to move your modifications to the correct branch without affecting the current one? Luckily for us, Git has a pretty handy command that can help us in these sort of scenarios.

The git stash command records all the modifications that you did since your last commit / pull and leaves your current branch in a clean state. It reverts the working directory to match the HEAD commit but saves all your modifications in a separate location. This allows you to navigate to the correct branch and re-apply all the changes that you made.

I found it very useful when I needed to work on a new feature, on a developement branch. By mistake I wrote all the code on the master branch. In order to fix it I used the 3 commands listed below and it saved my day 🙂

git stash
git checkout <CORRECT BRANCH>
git stash apply

You can also display the saved modifications, if you want to take a closer look before you re-apply them.

git stash list

For a complete description of this command and all the nifty things it can do for you, take a look at https://git-scm.com/docs/git-stash