Oscar the owl and Vershd logo
Menu

How to Git Stash Effortlessly

Introduction

Git 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 Stash

Git 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. [email protected]{0}. The penultimate stash would be [email protected]{1}  and so on.

Essential Commands for Git Stash

Now 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

 git stash 
Saves a stash from the uncommitted changes you’ve been working on.
 git stash push 
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.
 git stash [-p|--patch] 
Choose which hunks (each change in each file) you’d like to stash, rather than all modified files.
 gigit stash -u 
Stashes changes to both modified and untracked files.
 git stash -a 
Stashes changes to modified, untracked, and ignored files.

Viewing your stash

 git stash list 
View all your stashes.
 git stash show [email protected]{2} 
View a summary of a specific stash (the third most recent in this example).

Applying your stashes

 git stash pop 
Applies the most recent stash to your working files and removes it from your stash list.
 git stash pop [email protected]{1} 
As above, but pops the penultimate stash.
 git stash apply 
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 [email protected]{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

 git stash drop [email protected]{1} 
Deletes the penultimate stash.
 git stash clear 
Deletes all stashes.

Use Git Stash correctly

Git 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 personal use Git client if you want something easier.

Zero stress Git

Vershd is an effortless Git GUI that boosts software development. It works on Windows, macOS, and Linux. It's free for personal, educational, non-profit and trade union use.

Designed for intermediate and beginner Git users, our unique UI/UX boosts your software development and makes Git simple.

Help & tools

We provide these pages to try to make your programming life easier. Our resources page gives an overview. You can see a full list of our Git tips & tools here.

We have these free tools:

Customers say...

I love any tool that makes my life easier... This is perfect, just the right amount of control. No more, no less. Easy to get started, push and rollback changes... It's a no brainer!
Hayden T.
Oscar the owl and Vershd logo
 © 2021 Blightysoft
Made in England
St. George's flag of England, the Union Jack flag of the United Kingdom, the European Union flag, and the United Nations flag.
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram