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...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
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 dogit stash pop
to pop the last added entry to the stash (: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.
From git version 2.11 you can do
git stash pop stash@{n}
, or even betteryou can just use index instead stash@{n} like
git stash pop n
.Or when we want to stash specific files, we can't simply
git stash
too, as it will stash everything!Yesss!
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
Yes, thanks for adding the missing information 🙂
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.
I'm glad that it's useful 🙂
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?
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.
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
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.
I'm glad that it's useful :)
Nice! Another thing you can do with stashes is create branches from them...
git stash branch <branchname> [<stash>]
mirrors.edge.kernel.org/pub/softwa...
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}
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!
I'm glad that you learned something from this article 🙂
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?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
andabc.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.
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 thatgit stash
and its extensions are aliases to block commands consisting of primitive git commands (likeadd
,branch
,commit
,checkout
,reset
.Following up the interrupted workflow use-case in the help man-page
is an alias for
where wip stands for Work in Progress, and
is an alias for
So considering these boilerplate commands one has to type for an interrupted workflow, there definitely is some utility for using
git stash push/pop
:).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.
Thank you @tylerdurden07 👍🏻
thanks for posting this its a really useful for the development with branch switching
I'm glad that it's useful 🙂
thanks for adding the missing information
bestbedlotion.com/best-self-tannin...