10 Git stash commands every developer should know
Tags: git, github, beginners, codenewbie
Published: 2023-12-08 05:10:52

> Git Stash is an extremely useful tool for developers. Learn the usage of the git stash commands to become a productive developer.

Developers are agile. Their work environments are also rapid and fast-moving to keep them on their toes most of the time. I can imagine a situation like this would be quite normal at your workplace:

You are working on a feature and, in between, making some changes that are still too unstable to commit. Now, your boss(they say the boss is always right!) comes over with a rush and informs you that a valued customer is facing the deadliest issue in production. You must take care of it RIGHT NOW.

You don't want to commit unstable changes to your current branch, but you have to switch to another branch and fix the issue to make your customer(and, of course, the boss) happy. However, `git` doesn't allow you to switch branches without your current changes for the tracked files being committed to the current branch. Wow, what a chicken and egg problem!

![Chicken-Egg Problem](https://cdn.hashnode.com/res/hashnode/image/upload/v1701249166359/69623089-d195-4516-82ee-4baeb8bbfa88.png)

Don't worry, we have `git stash` to rescue.

## Welcome to Git stash

The word `stash` means storing something safely in a hidden and secret place. In the world of git, it tracks your files and folders for changes for everyone's visibility, and with `git stash`, it also provides a way to secretly put some changes that are only known to you so that you can hide them and again bring them back when required.

The `git stash` command allows you to save the uncommitted changes locally, switch the branch to do other things, and return to your unfinished work along with the stashed changes. The workflow goes like this:

* You are in branch **feature/A-branch** where you have uncommitted changes
    
* You stash the changes.
    
* You then switch to another branch **feature/B-branch** to finish the work. You commit(and push) the changes.
    
* You switched back to the **feature/A-branch** branch, applied your stashed changes, and continued your work.
    

The command that you use to stash your changes is:

```bash
git stash
```

We will learn the usages for 10 commands related to the `git stash` in this article. If you are a beginner to git and want to know its fundamentals along with frequently used commands, here is a read for you:

> # [Top 10 Git Commands Every Developer Should Know](https://blog.greenroots.info/top-10-git-commands-every-developer-should-know)

## ✅ Stash your changes

You can have two types of uncommitted changes.

* The staged changes that git is tracking. It means you have used the `git add` command to stage them but haven't committed them yet using the `git commit` command.
    
* The unstaged changes that git is not tracking. It is a file that you haven't staged using the `git add` command.
    

Use the following command to stash a staged change:

```bash
git stash 

# You can also use `git stash save <description>` command to 
# save a stash with a description.
```

It will create a stash in the stack with a unique reference ID.

![git stash](https://cdn.hashnode.com/res/hashnode/image/upload/v1701240474005/5187a0bb-e46b-4373-b983-18db08a607ff.png)

If you want to stash an unstaged change, use the `-u` switch

```bash
git stash -u
```

Now, it will add one more entry to the stash stack.

![stash untracked](https://cdn.hashnode.com/res/hashnode/image/upload/v1701239396600/f3559d12-90fd-409a-a347-11c7055c610a.png)

You can use the `git stash -a` command to stash all the files, including the hidden files.

## ✅ List all the stashes

You can list all the stashes from the stash stack using this command:

```bash
git stash list
```

It will list all the stashes in a stack, meaning the latest stash will be at the top. Also, notice the format of the stash reference IDs. They are in the format `stash@{0}`. The number in the curly braces shows their position in the stash stack, `0` being the top.

![list stash](https://cdn.hashnode.com/res/hashnode/image/upload/v1701241477608/2264194f-2289-438d-87ae-381fe95255b9.png)

## ✅ Create a stash without a reference log entry

So far, we have created all the stashes with a reference ID. We have seen the `git stash list` command show them in the reflog. However, you can create a stash without logging it's reference to the stash reflog.

To do that, use the `create` command:

```bash
git stash create "valid stash"
```

It will create a stash with a description and unique ID, but nothing will be stored anywhere in the ref namespace. This stash creation method is mostly used for scripts.

![without reflog](https://cdn.hashnode.com/res/hashnode/image/upload/v1701248884722/35c23f53-f2af-4bed-bf03-ba873c911201.png)

To add this stash entry into the reflog later, you can use the `git stash store` command like this:

```bash
git stash store -m "adding to reflog" "98c7181a7a3621139a4899be71e93ec7ceb18afa"
```

The output,

![with reflog](https://cdn.hashnode.com/res/hashnode/image/upload/v1701248894931/82a3f792-39fd-4a4d-b981-a8c11184383c.png)

## ✅ Show the latest stash information

Use the `git stash show` command to see the information of the latest stash in the stack. By default, it shows the status information of the file stashed and the number of insertions and deletions in the files.

![latest stash info](https://cdn.hashnode.com/res/hashnode/image/upload/v1701243315057/d449fc69-ffd2-43a3-bbd8-5680b07cab8a.png)

If you want to see a verbose stash output, you can use the `-p` switch with the above command to see the stash information in the patch view.

```bash
git stash show -p
```

The output,

![stash info output](https://cdn.hashnode.com/res/hashnode/image/upload/v1701243393081/24d27c8b-033e-447c-b87a-a2d5cd5ba627.png)

The patch view shows the exact diff between the commits.

## ✅ Show the information of a particular stash

What if you want to see the information of a particular stash? Remember the stash reference ID? We can refer to a specific stash and get its information using the `show` command we learned above.

```bash
git stash show shash@{2} -p
```

Here, we are interested in seeing the information on the third stash in the stack. The output,

![info of a particular stash](https://cdn.hashnode.com/res/hashnode/image/upload/v1701243411698/ca340921-3c7f-4d15-bcde-817ff0dbdcde.png)

## ✅ Apply and remove a stash

The stashes stay in the stash stack. You can pop(take out) a stash from the stack and apply it to the current branch. Use the following command to pop a stash:

```bash
git stash pop
```

It will pop the latest stash(one at the top of the stack) and apply it to the current branch. Also, the applied stash will be dropped(removed) from the stash stack.

![pop stash](https://cdn.hashnode.com/res/hashnode/image/upload/v1701244712606/d5725e26-44bf-4228-bd02-5a4628def4e7.png)

## ✅ Apply a stash without removing it

What if you want to apply a stash without dropping it from the stack? You can use the following command for that:

```bash
git stash apply
```

The apply command will apply the latest stash from the stack without removing it from the stack.

![apply stash](https://cdn.hashnode.com/res/hashnode/image/upload/v1701245390018/975568be-43d4-4853-aa89-f3f1148a65df.png)

Also, if you want to apply a specific stash from the stack, you can use the stash reference ID like this:

```bash
git stash apply stash@{2}
```

## ✅ Remove a stash without applying it

You can also drop the latest stash from the stack without applying it. To do that, use the following command:

```bash
git stash drop
```

You can also drop a specific stash without applying it using the reference ID of the stash:

```bash
git stash drop stash@{3}
```

## ✅ Clear all the stash

Sometimes, you may not need the stashes and want to clear them all. Use this command to clear all the stashes from the stack:

```bash
git stash clear
```

## ✅ Create a new branch from the stash

You can create a branch from the commits in the stash using the following command:

```bash
git stash branch fix/resolve-conflicts-checkout stash@{0}
```

The command will create a new branch and switch to the new branch.

![create branch](https://cdn.hashnode.com/res/hashnode/image/upload/v1701247451883/9a65e78a-e8e6-4542-95b0-ac632a9331a9.png)

You can check the newly created branch using the `git branch` command.

![branch list](https://cdn.hashnode.com/res/hashnode/image/upload/v1701247470779/c936246f-d791-4b15-8690-2df89db20789.png)

That's all for now. Do you know any other usages of the `git stash` command? Please feel free to comment and let us know.

---

Thanks for reading it. I hope it was insightful and helped you get familiar with some new git commands. If you liked the article, please post likes/comments and share it in your circles.

Let's connect. You can follow me on [**Dev**](https://dev.to/atapas), and I also share content on these platforms:

* [**tapaScript on YouTube**](https://www.youtube.com/tapasadhikary?sub_confirmation=1)
    
* [**Twitter**](https://twitter.com/tapasadhikary)
    
* [**GitHub**](https://github.com/atapas)
    
* [**LinkedIn**](https://www.linkedin.com/in/tapasadhikary/)