Sometimes you may forget to switch to correct branch while you are working on a project, so you need to move your changes to another branch and after that commit them to it. In such case git stashing could help you.

Stashing may be usefull in next situations:

  • You want to move current changes to another branch and you don't want to fix this changes from origin branch.
  • You want to work with other branch. Just switching won't help you because of uncommitted changes, which will go to another branch after switching.

Ok, let’s consider first problem. First of all, you need to stash your changes. Stashing saves current changes from last commit to special “stash list”, also a local copy of project in current branch is reverted to last commit. After that you can select desired branch where you want to apply stashed changes using “stash pop” command.

git stash
git checkout branch2
git stash pop

In addition, you can use stashing to save multiple changes, look further at specs in references section.