DEV Community

Cover image for Learn the hidden feature in Git - Stash
Yuvaraj
Yuvaraj

Posted on • Edited on • Originally published at yuvaraj.hashnode.dev

Learn the hidden feature in Git - Stash

Hello everyone πŸ‘‹,

In this article, Let's discuss about the stash feature in Git. We are going to cover the following topics:

  1. What is Stash?
  2. When to use Stash?
  3. How to use Stash?
  4. How to apply Stash?
  5. How to clear Stash?

1. What is Stash?

The general term of stash means storing something in a hidden place. It is similar in the Git terminology as well. It means moving the changes away from working directory.

2. When to use Stash?

When you don't want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is the git stash command.

3. How to use Stash?

Assume, we have a git repository which has 2 files - index.html & feature-1.js

stash-1.png

In the same master branch, a new feature is developed in the feature-2.js file which is not ready yet.

stash-2.png

Suddenly you got a call from your team lead about a blocker issue in feature 1 & you are requested to fix it immediately. Now, you have to make changes on feature-1.js without pushing feature-2.js.

This is where git stash will come to the rescue.

In this situation,

  1. First add the unfinished files to the staging area by git add command,
git add feature-2.js
Enter fullscreen mode Exit fullscreen mode
  1. Run the below command to move the files from working directory to stash.
// with -m flag, you can add customized stash message.
git stash push -m "feature 2 in progress"
Enter fullscreen mode Exit fullscreen mode
  1. Then run, git stash list to see the stash list. You will see the stash id along with the message. Here, stash@{0} is the stash id.
stash@{0}: On master: feature 2 in progress
Enter fullscreen mode Exit fullscreen mode
  1. The feature-2.js will not be available in your git status and as well as in your working directory. Now, you can work on feature-1.js and push your fix to the remote repository.

stash-4.png

Well Done! πŸ‘

You have successfully fixed issue in feature-1.js.

But, how to bring back feature-2.js to the work copy? πŸ€”

That brings to the next section on applying stash.

4. How to apply Stash?

Like how the files were moved from working directory to the stash through git stash push command,
one has to use git stash apply command to move the changes from stash to working directory.

To do that, follow the below steps,

  1. To see the list of stashes along with stash id, run the git stash list command. You will see the stash id along with the stash message we have given earlier.
git stash list
stash@{0}: On master: feature 2 in progress
Enter fullscreen mode Exit fullscreen mode
  1. In this case, stash@{0} is the stash id. Use this in below command to apply the changes.
git stash apply stash@{0}
Enter fullscreen mode Exit fullscreen mode
  1. You will see the following output on running the above command
  On branch master
  Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
    new file:   feature-2.js
Enter fullscreen mode Exit fullscreen mode
  1. In the last line of above log, you can see feature-2.js is bought back from stash. Now, you can work on continue working on Feature 2. πŸ™Œ

Lets try run git stash list once more.

stash@{0}: On master: feature 2 in progress
Enter fullscreen mode Exit fullscreen mode

You will be surprised to see the Stash is still there. We have already have feature-2.js file, But, why is still in Stash? We will see how to clear from stash on next section.

5. How to clear Stash?

There are 2 ways to clear the applied stash.

  1. You can remove the stash by id. In our case, stash@{0} is the stash id. This will remove only the specific stash from the stash list.
git stash drop stash@{0}
Enter fullscreen mode Exit fullscreen mode
  1. You can completely remove all the stash in the list. Warning: This will remove all the stashes from the stash list.
git stash clear
Enter fullscreen mode Exit fullscreen mode

That's it!

Thanks for reading my article. I hope you've learned something new day!

Here's the link to my next article on React JS Series

Top comments (26)

Collapse
 
lucassperez profile image
Lucas Perez • Edited

Git stash can sometimes be very useful, specially because we can't switch branches with modified files around.
Another way is to simply type git stash, it will put everything in the stash with a WIP message automatically. Then you can do git stash pop to pop the last added entry to the stash (:

Collapse
 
yuvgeek profile image
Yuvaraj

You are correct. But, when we have multiple stash and If one would like to apply the stash which is in the middle, then pop doesn't solve it.

Collapse
 
branislavtotic profile image
Branislav Totic

From git version 2.11 you can do git stash pop stash@{n}, or even better
you can just use index instead stash@{n} like git stash pop n.

Collapse
 
lucassperez profile image
Lucas Perez

Or when we want to stash specific files, we can't simply git stash too, as it will stash everything!

Thread Thread
 
yuvgeek profile image
Yuvaraj

Yesss!

Collapse
 
branislavtotic profile image
Branislav Totic

Stash is awesome, but you didn't mention one more flag that sometimes you will found useful.
git stash -u

flag -u: will stash untracked files as well

Collapse
 
yuvgeek profile image
Yuvaraj

Yes, thanks for adding the missing information πŸ™‚

Collapse
 
dyagzy profile image
dyagzy

So I faced this exact issue today at work and I didn't know how to resolve. Had to call a senior member of the team to help me out. I wished I had read your article before today. I learnt a lot from this article thanks for sharing.

Collapse
 
yuvgeek profile image
Yuvaraj

I'm glad that it's useful πŸ™‚

Collapse
 
johann_lr profile image
Johann

Thanks for sharing! I'm happy that I was introduced to stashes early when I learned git.
What did you do instead of stashing before?

Collapse
 
yuvgeek profile image
Yuvaraj

Like years before, the contents of the modified file is copied and saved it in another backup folder. Then discard the changes in working copy.

Collapse
 
johann_lr profile image
Johann

Ahaha relatable, guess I did that for everything before I even knew git - like creating a new "version directory" after every big change to have a chance to go back :p

Collapse
 
lico profile image
SeongKuk Han

I didn't know about 'stash' well.
I used 'stash' only when I had to pull same files I'm working on now.
At that time, I just ran four commands 'git add .', 'git stash', 'git pull', 'git stash pop'.
Now, I think I'm able to use the command 'stash' better.
Thank you for sharing.

Collapse
 
yuvgeek profile image
Yuvaraj

I'm glad that it's useful :)

Collapse
 
bc profile image
Brian Canzanella

Nice! Another thing you can do with stashes is create branches from them...

git stash branch <branchname> [<stash>]

mirrors.edge.kernel.org/pub/softwa...

Collapse
 
mishrabhavesh profile image
Bhavesh Mishra

For multiple stash we can do something like git stash list which will give us all the stash allng with no and stash name if given any and we can siplu apply this by git stash apply name / git stash apply stash@{1}

Collapse
 
sdiamante13 profile image
Steven Diamante

This was informative. I am a serial git stash/popper. Never really dove deeper than that. I've been on teams that lost stashes because of just relying on the stack. Thanks!

Collapse
 
yuvgeek profile image
Yuvaraj

I'm glad that you learned something from this article πŸ™‚

Collapse
 
baalkikhaal profile image
Sreekar Guddeti

Thank you for the tutorial :). However, if I work with a two separate branches for the two features described above, I do not find any reason to use git stash. Am I missing something?

Collapse
 
yuvgeek profile image
Yuvaraj

It is okay to create a feature branch and work separately. Assume when you and your teammates working on the same file called abc.js and abc.html to fix a bug. One is handling UI and you will take care functional issue. You are trying to fix the functional issue but there is some dependency on UI fix as well.
You're debugging the issue by putting console.log debugger etc.. at the same time other person fixed the UI issue.

In this case, you have some unfinished code and you cannot push it until it's done. But since your issue has some partial dependency on UI fix, you need to take pull.

In this case, you can move your existing changes to stash, pull the UI fix, apply the stash, and then you can continue your work.

If you see here, you haven't pushed the code due to stash.

This is one example to use stash.

Collapse
 
baalkikhaal profile image
Sreekar Guddeti

Thank you for the helpful use case. After reading your write up and doing some look-up of git stash --help, I get a feeling that git stash and its extensions are aliases to block commands consisting of primitive git commands (like add , branch , commit, checkout, reset.

Following up the interrupted workflow use-case in the help man-page

git stash push
Enter fullscreen mode Exit fullscreen mode

is an alias for

(main) $ git branch my_wip
(main) $ git checkout my_wip
(my_wip) $ git add .\n
(my_wip) $ git commit -m 'WIP'
Enter fullscreen mode Exit fullscreen mode

where wip stands for Work in Progress, and

git stash pop
Enter fullscreen mode Exit fullscreen mode

is an alias for

(main) $ git checkout my_wip
(my_wip) $ git reset --soft HEAD^
(main) $ git checkout main
(main) $ git branch --delete my_wip
Enter fullscreen mode Exit fullscreen mode

So considering these boilerplate commands one has to type for an interrupted workflow, there definitely is some utility for using git stash push/pop :).

Collapse
 
mexicanwave profile image
Gnana Sekar Jesuraj

Good one Yuvaraj. git stash was the life saver whenever I wanted to switch to main branch to fix any prod issues without having to worry about the current feature branch changes.

Collapse
 
yuvgeek profile image
Yuvaraj

Thank you @tylerdurden07 πŸ‘πŸ»

Some comments have been hidden by the post's author - find out more