Home » Git » Git stash changes – how to use git stash

Git stash changes – how to use git stash

By Emily

If you’ve just realised you’re working on the wrong branch and you’ve already made changes, then this post could be for you. Picture the scene, you’ve edited a few files you’re half way through building a shiny new feature and then it dawns on you that you *forgot to create a new feature branch* before you started. OMG.

If you have not staged or committed any of those changes yet then read on to find out how to keep your changes but move them to a different branch using the git stash command. I’ll answer the question ‘what is git stash?‘ and I’ll cover how to stash one file, how to view the git stash, and how to undo a git stash.

(If you don’t understand some of that first paragraph you might want to start by reading What is Git?)

What is git stash?

The git stash command itself is really simple – in it’s most basic form with no flags. You must be on the branch where you’ve made the changes and then type:

git stash

How to stash changes with git stash?

That’s all there is to stashing those changed files away to a safe place although it’s important to understand what is stashed – all unstaged and staged files. And what isn’t stashed? Any new file, any committed file will NOT be stashed.

How to save a the stash with a name or message

If you have several stashes you may struggle to remember which stash holds what changes. In this case you probably want to save your stash with a unique description. To do so you use this command:

git stash push -m "put your message here"

How to use the stashed changes

Now you want to grab those files back and put them on the right branch before you carry on working on that new feature. If the branch you want to use already exists then you need to change branches, or switch to that branch. Let’s say it’s called feature/shinybutton, then you would type:

git switch feature/shinybutton

If you need to create a new branch and switch to it immediately then type this instead:

git switch -c newbranch

So now you’re on the correct branch you want to take your stashed files and put them on this branch. I’ll also presume that you only want to use these files once, so that the ‘stash’ is empty once you have run the command. So type this:

git stash pop

This ‘pops’ the files out of the stash, applies them to the current branch, and clears the stash. If you want to use the stash on more than one branch then use this command instead:

git stash apply

This will ‘apply’ the files to the current branch but will keep the stash as it is, so you could then switch to another branch and apply the same changes there too.

Git stash changes – quick reference list

git stash //do this while on the branch with your uncommitted changes
git switch branchName //switch to the branch you want the changes to be put on
git stash pop //unpack the stashed files onto current branch & clear stash
git stash apply //unpack the stashed files onto current branch & keep stash

That gives you all you need to know to do a basic git stash. But there are some other details to be aware of so you can use it to it’s full potential.

Keep stashed files and use several times

Do the same as above but use git apply stash instead of git pop stash. This applies the stashed files to the current branch but leaves the files in the stash so you could switch to another branch and apply them there too.

See which files have been stashed

git stash show // lists all files that are in the stash
After git stash changes use git stash show to see stashed files

I’ve written another post that goes into a little more detail about how to view stash, and specifically how to view the contents of a git stash.

How to undo a git stash

Just stay on the branch where you did the stash and then run git stash pop which will unload the files back on to that branch.

Include untracked files in stash

git stash -u

Git stash one file

git stash -p

This will then start a dialogue and you’ll notice a question in the terminal window asking:

“Do you want to stash this hunk?” //WHO comes up with this?!!!

A list of options are available – click y for the one file you want to stash, and n for the rest. The patch options are as per this list:

y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

Summary

If you’ve been searching for help for “git stash changes” hopefully this post will have helped you understand it better, and has answered the question ‘What is git stash?’.