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 to use Stash?
- How to use Stash?
- How to apply Stash?
- 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
In the same master
branch, a new feature is developed in the feature-2.js
file which is not ready yet.
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,
- First add the unfinished files to the staging area by
git add
command,
git add feature-2.js
- 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"
- 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
- The
feature-2.js
will not be available in yourgit status
and as well as in your working directory. Now, you can work onfeature-1.js
and push your fix to the remote repository.
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,
- 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
- In this case,
stash@{0}
is the stash id. Use this in below command to apply the changes.
git stash apply stash@{0}
- 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
- 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 onFeature 2
. π
Lets try run git stash list
once more.
stash@{0}: On master: feature 2 in progress
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.
- 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}
- You can completely remove all the stash in the list. Warning: This will remove all the stashes from the stash list.
git stash clear
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)
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 ππ»
Some comments have been hidden by the post's author - find out more