Stashing Changes

How can you save changes temporarily - without committing them?

Stashing Changes

In this video, I’ll show you how to save changes temporarily in Git - without committing them.

Saving changes temporarily? Why is this useful?

We often have to switch between different tasks - and often, this leaves us with half-done work. For example, imagine you’re working on a feature, when suddenly an urgent bug report comes in. You have all those modifications in your working copy. All this unfinished work that doesn’t belong to the bugfix you’re going to make.

Committing your unfinished work would be a poor choice. Commits are really made to wrap up something completed. It might be small - but it should really be finished, tested, and working.

Here’s where Git’s “Stash” feature comes in.

A quick look at our working copy status shows that we’re in the middle of working on something. Let’s say that an urgent bug report comes in. We need to get a clean working copy to work on this new task.

Hitting the “Save Stash” button, all of our current local changes are saved away temporarily - and we’re left with a clean working copy.

You can think of Git’s stash as some kind of clipboard. In the “Stashes” section in Tower’s sidebar, you get a listing of all stashed states. This is where our local changes were just saved.

You can create as many Stashes as you want - you’re not limited to storing only one set of changes. Also, a Stash is not bound to the branch where you created it: when you restore it, the changes will be applied to your current HEAD branch, whichever this may be.

Later, we can simply continue where we left off. Just select “Apply Stash” and choose the item you want to restore. This restores the saved changes in our working copy. Voila! Our saved local changes are back - and we’re exactly where we left off.

It makes sense to clean up a bit. We delete this particular Stash from the clipboard.

In the “Apply Stash” dialog, you can also check this option here: it applies and directly removes the stash from the clipboard.

To finish off, let’s talk about when to use the stash. The answer is easy: any time you need a clean working copy. For example before you switch branches, before pulling changes from a remote, or before merging or rebasing branches.

Most importantly, you should keep in mind that stashing can never hurt: it’s very quick and easy, and has no side-effects or drawbacks.

That’s it for now! Thanks for watching - and see you soon in the next video.