DEV Community

Cover image for Learn the hidden feature in Git - Stash

Learn the hidden feature in Git - Stash

Yuvaraj on May 16, 2021

Hello everyone 👋, In this article, Let's discuss about the stash feature in Git. We are going to cover the following topics: What is Stash? When...
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 👍🏻

Collapse
 
karmakarsourov1 profile image
s o u r a b h k a r m a k a r

thanks for posting this its a really useful for the development with branch switching

Collapse
 
yuvgeek profile image
Yuvaraj

I'm glad that it's useful 🙂

Collapse
 
evelinafr profile image
Info Comment hidden by post author - thread only accessible via permalink

thanks for adding the missing information
bestbedlotion.com/best-self-tannin...

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