Stash working directory

Sometimes you may start changing your code before switching to a new branch. For example, assume that you are in master or development branch with clean working directory. Now you change something in the code file and then you find that you should actually create a new feature branch for that changes.

You can use git stash to temporary save your current changes.

$ git stash

This command saves your current status of non-committed changes, so that the working directory becomes clean again.

Now we can create new branch:

$ git checkout -b new_feature_branch

In the new branch, we can pop the saved state by using git stash pop.

$ git stash pop

Stash allows you to save work-in-progress changes to make the working directory clean. This allows you to save the changes and switch branch to checkout something.

Please note that Git allows you to push and pop the stash in different branch. But it could be confusing when you pop a stash into a commit that was not its original. Except the above example, I usually just commit the work-in-progress changes as a new commit and then reset it later when I’m back.