Git Stash — Basics of Git
Tags: git, gitbasics, gitstash, gettingstarted
Published: 2022-11-26 11:16:29

While working on a feature in a project, Do you ever felt any one of the following scenarios?

* Oh! I might have made these changes before

* I should have commit that part first and start doing this feature

* I must have merged that branch before touching this feature

* I should have made a commit in that particular branch and push the changes and continue working on this feature

If yes, then `git stash` command is there for you to help.

### What is Git Stash?

`git stash` temporarily stores the changes (shelves / stashes) you’ve made on your working copy. This gives you the freedom to work on something else and re-apply your changes when you’re back.

Fascinating!!!! Isn’t it?

Let’s understand more by digging deeper
>  *git stash *is handy, when you have to switch context and work on some other part of your project, but you’ve already started building a new feature or fixing a bug or cleaning a tech debt and you’re not in a logical state to commit your changes.

### Stash your work

Simple `git stash` command saves your staged and unstaged changes (not yet committed) for later use and removes them from your working copy. Simple `git stash` command doesn’t touch the untracked and ignored files.

I’ve created a sample project and made some changes.

![Git Stash — Make changes to a file](https://cdn-images-1.medium.com/max/2000/1*NrxM2ObslVuAUy-Q_zN2-g.png)

But, I want a commit to go before this. So, I’ll stash this change.

![Git Stash](https://cdn-images-1.medium.com/max/2000/1*-gLCiPhifbklTBKRd4wc8A.png)

You can see that my changes are reverted after applying `git stash` command.

Let’s make a commit now.

![Git Stash — Make commit in-between](https://cdn-images-1.medium.com/max/2000/1*UOt0VzLw5t8t7_UA2xtJRw.png)

Now that we have made a commit. Let’s bring our changes back.

### Stash Pop

`git stash pop` command brings back your latest stashed changes from git. Let’s bring our changes back.

![Git Stash Pop — Demo](https://cdn-images-1.medium.com/max/2000/1*OEYMUCceE7w-WS_KCMaM9A.png)

### Q & A

Remembering your quote,
>  Simple `git stash` command doesn’t touch the _untracked_ and ignored files.

**What if I have to stash the untracked and ignored files?**

This can be achieved with using option flags with the git command.

Adding `-u` or `--include-untracked` option with `git stash` command will include the untracked files on stashing

and

Adding `-a` or `--all` option with `git stash` command will include the ignored files on stashing

In the below screenshot, I have created two files in our repo.

File3.txt -> Untracked file

Hello.txt -> Ignored file

![Git Stash — Untracked and Ignored files 1](https://cdn-images-1.medium.com/max/2000/1*2eXFyPVrXjF8Ju58C4rhWQ.png)

Let’s stash both the files using the above options

![Git Stash — Untracked and Ignored files 2](https://cdn-images-1.medium.com/max/2000/1*QWpOalbmadoSqFUn4CTm4g.png)

I ran the commands (-u and -a) individually, instead you can run both the commands together ( `git stash -u -a` )

You made two stashes above. **How can I see all my stashes together?**

`git stash list` command will list the history of stashes stored in the repo.
>  Note: This command will not show the popped stash

![Git Stash List](https://cdn-images-1.medium.com/max/2000/1*lIDaBn2G688Ex8vFEUvxoQ.png)

**Can I see the changes made in each stash?**

Yes. You can. `git stash show <stash_id>` will show the changes made in that particular stash.

I made a change to File1.txt file.

![Git Stash Show Changes](https://cdn-images-1.medium.com/max/2000/1*POuwVIAWAn_DA_9oN1zObQ.png)

Adding `-p` option flag to `show` command will show the line by line changes.

**Is there a possibility to pop a particular stash? **Say, the middle stash item.

Yes. Absolutely you can do.

Simply add the stash-id at the end of `stash pop` command

![Git Stash — Pop Particular Stash](https://cdn-images-1.medium.com/max/2000/1*-JvbFFZNlck3RM9KnPGUVg.png)

After running the command, you can see the middle stash entry has been removed.

**How to save the stash with a message? **It would be helpful for me to identify quickly and pop that particular commit in the future.

Yes. You’re right. We have a way in Git to save the stash with a customized message.

`git stash save <message>` command saves the stash entry with the customized message. You can enclose the message in double quotes (“”), if your message has spaces or special characters in it.

![Git Stash — Save with customized message](https://cdn-images-1.medium.com/max/2000/1*2VgMvgY6nPVDA6U6umqTuQ.png)

**I need to pop the stash entry but it should not remove the entry from the stash list. Is it possible?**

Yes. It is.

You can achieve that with `git stash apply` command. This command will apply the changes from that stash to your working repo and will not remove the entry from the stash list.

![Git Stash — Apply](https://cdn-images-1.medium.com/max/2000/1*fHIX1ybdABsFyhoN02fioQ.png)

You can see from the above screenshot that the last stash was not removed, and the changes made in that stash and pulled into the working repo.

**Will I be able to remove a particular entry from stash?**

Yes. Obviously, you can. If you’re able to create it, you can also delete the same.

`git stash drop` command helps you to remove a entry from stash list. Just running `git stash drop` will remove latest entry from stash list. You can add a `stash-id` with the `drop` command to remove the particular entry.

![Git Stash Drop](https://cdn-images-1.medium.com/max/2000/1*U0egJq8Sn_h1BMSmdMN7JQ.png)

### Tip

If you wish to remove all the stash entries, you can achieve it by simply running `git stash clear` command.

![Git Stash Clear](https://cdn-images-1.medium.com/max/2000/1*6X00UDb0Aw7lW3nF0FBn9g.png)

Hope you enjoyed reading this article. If you have any questions or suggestions, please post them in the comments. Give a clap 👏, if you liked and learnt something new.

Subscribe to our [newsletter](https://5minslearn.gogosoon.com/?ref=devdotto_git_stash) to receive more such insightful articles that get delivered straight to your inbox.

Happy Coding