BLOGS

git stash – How to git stash changes? [THE ULTIMATE GUIDE]

Category
Programming
Author
IRSHAD S
Date
10/28/2019

WHY DO WE USE git stash?

If you are a developer who is using git, you surely may have encountered this error a lot of times.

Your local changes to the following files would be overwritten by checkout. Please commit your changes or stash them before you switch branches.

On my initial days, if this error occurs, I would discard my local changes (to be on the safe side). I should’ve committed it, right? But what if I don’t want to commit my half-done work? Is there a better way? What is a stash in git? Let’s explore in this blog about the various usage of git stash command.

When should you use git-stash?

CASE 1:

You are in the middle of something, and you have got a call to urgently resolve a bug. (Thanks to your boss). So now you need to switch to a different branch and work on the bug fix. But you have changes you have not committed and you are not yet ready to commit this. What will you do?

CASE 2:

You have uncommitted files and try to “git pull” and due to conflicts with the remote changes, git may refuse sometimes to overwrite.

Your local changes to the following files would be overwritten by merge. Please commit your changes or stash them before you merge.

This same type of error may also occur during a merge request, while pulling the latest changes from the remote branch.

How will you resolve this scenario?

CASE 3:

You had committed to the wrong branch !!! (Seriously, sometimes we make mistakes. Who hasn’t?)

git stash” could come for your rescue in most of these cases.

So, What is a git stash?

git stash

According to git-scm documentation, git-stash command is used to

"Stash the changes in a dirty working directory away".

Which means you could throw away your ‘uncommitted changes’ when you don’t want to commit those, in a temporary location (in an intention to take it later). To be precise, your modified ‘tracked’ files and staged changes – will get saved to a ‘stack’ and you can apply these changes later. 

How to stash changes in git? Now, Let’s git stash - your changes

We can ‘stash’ the changes by simply executing the following command

git stash
[Note that, only tracked files will be stashed by default with this method. How to stash ‘untracked files’ will be explained later.  And also, using a name to identify the stash is a better practice, which will be also explained below.] When you execute just git stash, it is equivalent to calling the git stash push command.

git stash list

To retrieve the list of all the saved stash entries, we can use the command

git stash list
The git stash list command will give you all the stashes saved so far in a descending order where stash@{0} is the most recently created stash, and stash@{1} is the one you created before it.
What does  WIP on master  means? Because, you stashed those changes when you where on master branch. By default, a stash will get listed as ‘WIP on branchname’ which will be easier to identify it with respective to the branch you stashed it. Also,  these are stashed after the commit –825e72d initial commit

How does stash work?

When you ‘git stash’, it will get saved/pushed in a ‘STACK’ type temporary location (let’s call that stash-list) so that you can pop/apply them later. The latest stash will be stored in refs/stash. And the subsequent pushes will be available in the reflog of this reference.

git stash with name (OR) git stash with message

When you list the stashes using git stash list command, isn’t it confusing to identify a stash, if there are many stashes happened after a commit? What if there is a way to give a name or message along with it when you stash them? Let’s see that in action.

The syntax for saving a name or message along with the stash is as follows. 

git stash push -m <name or message>

git stash save vs push

Please note that, git stash save command has been deprecated in favour of git stash push. Anyway, it’s also easier to remember push and pop than a command like ‘save’ when dealing with git-stash which gives a ‘stack’ like storage. Am I right?

git stash pop

You had stashed away your changes and also listed those stash entries. But how do you take back the stash you saved? Is there any git unstash command? Or how do you undo git stash you just did stash?  Just ‘pop’ that from the stash, like you pushed.

SYNTAX:

git stash pop

By default, this will retrieve the ‘last stashed entry’. What if you wanted to retrieve previously added stash entry or a specific stash?

git stash pop specific

You can use the following command to pop a specific stash entry, where ‘n’ is the stash index.

git stash pop stash@{n}

Or more simply,

git stash pop n

[Note:- Some shells would give error using curly brackets and so they have to be put inside quotes. So its better to use the second command.]

git stash apply

git stash apply is similar to pop, but the difference is:

git stash pop – throws away the stash entry from the stash-list after ‘applying it’, whereas
git stash apply – keeps it in the stash-list even after applying it, for later reuse.

(I can’t find a reason, why would you need to keep stash entry for later reuse. But anyway some people may prefer that instead of pop).

git stash one file

If you have multiple files modified, and you want to git stash a single file alone, mentioning the <path> of the file along with the git stash push command would be enough.

SYNTAX for git stash single file:

 git stash push -m <message> <path-of-file>

git stash specific files

Same like above, if you have multiple files to be stashed selectively, mentioning all the files’ path which you required to be stashed, along with the git stash push command would do the job.

 git stash push -m <message> <path-of-file1> <path-of-file2>

git stash untracked files

As I discussed in the beginning, by default git stash command wouldn’t stash the untracked files and ignored files. If you want to stash all the files including untracked ones as well as ignored ones add a --all flag with the stash command.
git stash --all

git stash show

What if you just need to sneek peek the contents of a stash entry without applying it? Or to know what files will get changed by applying a stash without applying it?

git stash show – will show the list of files changed in ‘last’ stash.
git stash show n – will show the list of files changed in ‘n th’ stash.

By adding a -p flag we can preview the contents too.

git stash show -p – will preview the git diff contents of the ‘last’ stash.
git stash show -p n -will preview the git diff contents in the ‘n th’ stash.

git stash drop

When you feel that you no longer require to keep a stash entry in the stash-list, you can delete the stash by executing git stash dropcommand, which will remove the latest stash entry from the stash list. You can also mention the stash-index used like before to remove a particular stash entry from the stash-list.

git stash clear

When you want to drop all the stash entries with a single command, you can use the git stash clear command for the same.

You May Also Like To Read

Share on facebook
Share on twitter
Share on whatsapp
Share on linkedin
Share on facebook
Share on twitter
Share on whatsapp
Share on linkedin