DEV Community

Flávia Bastos
Flávia Bastos

Posted on • Originally published at flaviabastos.ca on

git stash 101

git is a very useful tool for software development and you only need to know a few commands to get most of the job done. However, you can be a lot more productive if you go beyond the basics.

One git command I started using not too long ago is git stash. In my opinion this is one command that might not be super useful if you are just starting to code or you are the only person working on a personal project. But if you start collaborating and need to switch between a new feature work to fixing a bug, for example, git stash is very helpful.

What does git stash do?

It allows you to save your work in progress out of your way, meaning, without the need to create a commit, and access that work later.

When to use git stash?

When you have some work in progress but you are not ready to create a commit yet (maybe you are just exploring some options or the work is not completed yet) and you need to switch branches OR you want to go back to start point but you want to keep the changes for later. You can then ask git to take the current changes and save they aside. In this way, when you run “git status”, you see that your repo is back to the same state it was when you first cloned it or created that branch: the recent changes are not visible anymore but they are saved in case you want to go back and work on them again. You can now checkout a different branch and start new work.

In other words…

This is almost like having a branch within a branch: imagine that you created a branch for some new work. You make a few changes but you think you can create a different solution for that issue. You can stash the current changes so your new branch gets “reset” to the same state as when it was created and you can try a different solution. If later on you decide that your first solution was the best one, you can stash this new code and retrieve the previous solution from the stash.

The key point for git stash is to save the changes for later. If you don’t care about the changes and just want to discard unstaged changes (changes you did not “git add” yet), you can just run git checkout . (note the . at the end!)

Each repository has its own unique stash and you can end up with entries from different branches in that repository’s stash. Each stash entry is assigned an index.

Basic stashing activities:

CREATE STASH

you can create a stash for uncommitted changes by running:

git stash push

OR simply

git stash

Add a message to your stash for easier identification afterwards:

git stash -m "recursive solution"

Stash only one file (instead of all the changes):

git stash push <path_to_file>

INSPECT STASH

you can check your stash by running:

git stash list

list all the files that changed when the stash entry was created:

git stash show stash@{1}

// you can omit the stash index if your stash has only one entry!

check the changes in a stash entry – for example, stash@{1}:

git stash show -p stash@{1}

MANAGE STASH

apply one item from your stash to your code and KEEP that entry in the stash:

git stash apply stash@{1}

apply one item from your stash to your code and REMOVE that entry from the stash:

git stash pop stash@{1}

remove one entry from the stash:

// this will delete this entry from the stash without applying it to your code!

git stash drop stash@{2}

clean up (delete) the whole stash:

git stash clear

There are many other options to use with git stash but these are the ones I use most often. You can check git stash documentation for more options: https://git-scm.com/docs/git-stash


If you found this helpful, let me know on Twitter!

The post git stash 101 _was originally published at _flaviabastos.ca

Discussion (1)

Collapse
extrawurst profile image
Stephan Dilly • Edited on

And if you do not want to memorise all these commands: github.com/extrawurst/gitui :P

demo