IntroductionGit stash is a feature in Git that lets you save your work when you’re not ready to commit. If you don’t know what git stash is, or how to use it, then read on.
If you’ve ever been worried about losing changes you’ve been making on a branch when you’re not ready to commit, then you’ll want to learn more about git stash.
Understanding Git StashGit stash is a simple way to save your work when you’re not yet ready to commit it. It stores any uncommitted changes temporarily in a safe area, so you won’t lose them.
It takes what you’ve been working on and stashes it away. Pretty simple really. Then when you’re ready, you can just come back and pick it up again.
Understanding and using the stash effectively means you can make changes elsewhere, switching branches and making new commits, without affecting the unfinished work in your stash.
When Should You Use Git Stash?There are many occasions when you might want to use the stash.
If you’re working on a piece of code on one branch, developing a new feature or making updates, but then something urgent comes up somewhere else, you’ll want to save your changes and switch branches.
It happens to all of us, and that’s why git stash is so useful. Use it whenever you don’t want to commit half-done work, but you need to save the changes you’ve made and move elsewhere.
It’s a helpful way to tidy up work and clean up your working directory. And if you’re merging branches or pulling from a remote, you’ll need to have clean (unmodified) working files.
Git stash is also useful if you want to save changes in one branch and then apply those same changes to multiple branches.
How Does Git Stash Work?The stash works by keeping a separate area from your working directory, staging area and repository.
It takes your modified tracked files and staged changes and then saves them on a separate stack in your local repository. Nothing in your stash is transferred when you push to the remote repository.
By default, git stash only stashes changes to tracked files (staged and unstaged). It doesn’t stash new untracked files or ignored files. You’ll need adapt the command if you want to stash changes to those files. (See our guide to stash commands below)
You can save multiples stashes too. Each stash is listed with a number, where zero is the latest, i.e. stash@{0}. The penultimate stash would be stash@{1} and so on.
Essential Commands for Git StashNow you know what git stash is, how it works and when to use it, you’ll need to learn the specific stash commands. Below is a handy guide, until you get familiar with the stash.
NB: Where you see two parameters within square brackets separated by a pipe, it means that they are synonymous. So: [x|y] means that x and y are the same command.
Saving a stash
Saves a stash from the uncommitted changes you’ve been working on.
A synonym for calling ‘git stash’ without any arguments.
git stash save ‘your message’
Name stashes with your own message to help you remember what they contain.
Choose which hunks (each change in each file) you’d like to stash, rather than all modified files.
Stashes changes to both modified and untracked files.
Stashes changes to modified, untracked, and ignored files.
Viewing your stash
View a summary of a specific stash (the third most recent in this example).
Applying your stashes
Applies the most recent stash to your working files and removes it from your stash list.
As above, but pops the penultimate stash.
Applies the most recent stash item to your current working files but keeps it in your stash (useful if you want to apply the same stashed changes multiple times).
git stash apply stash@{2}
As above, but applies the third most recent stash.
git stash branch my-branch
Applies the latest stash item to a new branch called my-branch, starting from the commit at which the stash was created (useful if changes on the older branch have diverged).
Cleaning your stash
Deletes the penultimate stash.
Use Git Stash correctlyGit stash is a really useful way to keep unsaved changes that you haven’t committed, but don’t get carried away with it. It’s not a replacement for regular commits, as you can soon forget which stashes are which and create a very messy workflow.
You may also run into conflicts with git stash if you’re merging branches and can still lose changes if you’re not careful. Try a
free for any use Git client if you want something easier.