Git stash is a great way to maintain your current work and switch to another
branch quickly. If you want to work on something else and aren’t ready to commit
your half completed work, git stash allows you to switch context easily and then come back to your work later.
If you have some work that you haven’t committed yet, for example we adjusted our
Makefile
We can stash that change and switch branches to work on something else:
Then when we run a git status, we’ll see that there are no local changes
If we then come back we can put those changes back in one of two ways. If we want keep that
change around in our stash list we can just run git stash apply, or if we don’t
want that change around anymore, we can run git pop. Typically I default to git pop
since I’ll probably commit those changes the second time around.
By default, git stash does not stash new files, however you can stash new files by adding the
-u or --include-untracked flag to tell it to stash new files as well. That way
everything will be stashed, which can be nice if your changes include additional files:
Another nice feature of stash, is to have git iterate over every file that has changed
by using the -p or --patch.