DEV Community

Cover image for git stash - Store your uncommitted changes aside to work on it later
Raunaq Chawhan
Raunaq Chawhan

Posted on • Originally published at raunaqchawhan.hashnode.dev

git stash - Store your uncommitted changes aside to work on it later

Introduction

In this post, we'll be looking at the git stash command and its usage. We come across situations in our development work where we are in the middle of a task with quite a good amount of code changes and have to switch to some other task that has come on priority. Now, if the changes are somewhat complete then we can commit it and start with the other task but what if those changes are unfinished and far away from a meaningful commit, in that case instead of committing it we can make use of

git stash
Enter fullscreen mode Exit fullscreen mode

git stash stores the uncommitted changes that we've made on our current branch away to be used at a later point in time and we are left with the code that was present before those changes. This allows us to work on our other task and get back to the stashed one later.

git stash

Suppose we have a demo project and we've modified some files in it

screenshot 1.png

As we can see from the above screenshot that we have modified two files and now we'll use git stash

git stash.png

After performing git stash, the changes get stashed and when we use git status, it shows that there is nothing to commit. Now, we can work on any other changes, switch branches and perform different operations and once we are done, we can re-apply the stash and continue working on it.

Re-applying the stashed changes

We can use the following command to re-apply the stashed changes

git stash pop
Enter fullscreen mode Exit fullscreen mode

git stash pop.png

We see that the above command pops out our stashed changes and applies them to the current branch. After the changes are applied, it gets removed from the stash.

If we don't want our changes to be removed from the stash after applying it on the branch, we can use the following command

git stash apply
Enter fullscreen mode Exit fullscreen mode

This command can be used to apply the same changes on multiple branches.

Multiple stashes

We are not limited to a single stash and can use the git stash command multiple times to create as many stash as required. We can then use the following command to view our stashes.

git stash list
Enter fullscreen mode Exit fullscreen mode

Suppose, we've created few stashes by running the git stash command multiple times

git stash list.png

We see that this command list out all the stashes that we've created and the stashes are identified as

stash@{n}: WIP(work in progress) on (branch on which stash is created): (commit from which the stash is created)
Enter fullscreen mode Exit fullscreen mode

Identifying which stash contains what change becomes difficult as the message does not provide any context related to the change that it holds. Therefore, instead of using only git stash we can use the following command that adds a description to the stash

git stash save "description to give you an idea about your change"
Enter fullscreen mode Exit fullscreen mode

git stash save.png

git stash pop and git stash apply re-applies the most recently created stash which is stash@{0}. If we want to re-apply the changes from a different stash, we should pass its identifier as the last argument to the respective commands.

git stash pop stash@{3}
Enter fullscreen mode Exit fullscreen mode
git stash apply stash@{3}
Enter fullscreen mode Exit fullscreen mode

Stash untracked and ignored files

By default, git stash stores the staged and unstaged changes only. It will not store the new files added to the directory that has not been staged yet and also the files that have been ignored.

If we also want to stash the new files, we can use the following command

git stash save "description" -u
OR
git stash save "description" --include-untracked
Enter fullscreen mode Exit fullscreen mode

Using the following command will stash all the files including the ignored ones

git stash save "description" -a
OR
git stash save "description" --all
Enter fullscreen mode Exit fullscreen mode

View the changes recorded in a stash

We can use the following command to view the changes recorded in a particular stash as a diff between the stashed contents and the commit back when the stash was first created.

git stash show -p stash@{n}
Enter fullscreen mode Exit fullscreen mode

Stash specific files

We can make use of the --patch flag while running the git stash command that instructs Git not to stash everything that is modified instead it will iterate through all the changes and ask whether we want to stash it or not

git stash save "description" -p
OR
git stash save "description" --patch
Enter fullscreen mode Exit fullscreen mode

Create a branch from the stash

Suppose we stashed some changes from a branch and continue to work on that particular branch and later try to re-apply those stashed changes, it might happen that the stashed changes try to modify a file which has already been modified and it results in a merge conflict. In that case we'll have to resolve the conflict or what we can do is use the following command that creates a new branch and checks out the commit we were on when the stash was created and reapply the stashed changes and removes the stash if it gets applied successfully.

git stash branch branch_name stash@{n}
Enter fullscreen mode Exit fullscreen mode

git stash branch.png

Cleaning the stash

So far we've seen how to create a stash and re-apply those changes back, now we'll be looking at the commands that will allow us to delete the stash. If we want to delete a particular stash, we can use the following command to delete it

git stash drop stash@{n}
Enter fullscreen mode Exit fullscreen mode

git stash drop.png

In case we want to delete all the stashes, it can be done using

git stash clear
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this post, we learned about the git stash command that lets us store our uncommitted changes that are not ready enough to be committed. We learned how to stash our work and also re-apply the changes back.

By default, the stash command only stores the staged and unstaged changes so we learned how to stash the untracked and ignored files as well.
We learned the command to view the changes stored in a stash and also stash specific files. Lastly, we created a branch from the stash and also deleted the created stashes.


Thanks for taking the time to read this post. I hope this post helped you in learning something and if you enjoyed it, please share.

It would be great to connect with you on Twitter. Please do share your valuable feedback and suggestions👋

Top comments (2)

Collapse
 
vinceknight profile image
Vincent Prytherch

Thanks for this, very clear. stash is one of those things I’ve used but never fully grok’d.

Collapse
 
raunaqchawhan profile image
Raunaq Chawhan

Thank you