Stash

If you make some changes in file-1but want to switch to a different branch to do something else. If that branch also contains file-1 then your changes will be overridden. You have to temporarily save the changes and later apply them.

This is what git stash does. It saves all your edits yet to be commited in a stack. Once the changes are stashed, the repo is in a clean state prior to these edits.

How to do

The typical workflow

  • You stash the changes. You can also give it a name for later references. Otherwise it will be the last in first out (LIFO) order.

  • You can freely switch branches and do some other work.

  • You come back to the original branch and pop the change from the stash stack.

Commands

  • Save the changes: git stash

  • Show stashes and their indeces: git stash list

  • Apply the changes:

    • git stash apply will apply the last (the last save, as per LIFO), or

    • git stash apply stash@{n} choose the n-th stash to apply

Last updated