How to use git stash effectively
Tags: git, beginners, programming, tutorial
Published: 2020-07-06 07:06:20

---
title: How to use git stash effectively
published: true
tags: git, beginners, programming, tutorial
canonical_url: https://gitbetter.substack.com/p/useful-tricks-of-git-stash
---

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F325441de-b490-41dc-91c0-7a0679a92b77_600x700.png)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.

If you are using Git for a while, you might have come across git stash.

Git stash helps you to temporarily save the changes in the working directory so that you will get a clean directory. Then you can apply those changes later in your git workflow.

Lets’s see an example

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F43f0456d-c2b6-4a44-85b7-9168baecb5a0_560x356.png)

You can see that there are some changes in the working directory. You can use git stash to save all the changes so that the working directory will be clean.

```
git stash
```
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F79e482f2-5411-437a-b349-b04b0f19540c_674x283.png)

After stashing, you can see that the changes in the tracked files are gone in the working directory. And also note that the untracked files are still present.

Those saved changes can be accessed and used in the future or else kept aside forever.

By default, stash won’t save the untracked files. But if you still need to stash the untracked files you can use

```
git stash -u
```

#### List all git stashes

You can use git stash multiple times across the git directory. To view all your git stashes

```
git stash list
```
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fa98d10b2-3251-4d21-a660-af813a124df2_517x133.png)

In the above image, you can see the list of git stashes.

Each stash will have a name, the working directory, and the HEAD commit.

Git stash name and the branch are very important. This helps you to identify the stash easily.

Remember that stash works like stacks. The most recent stash will be at the top.

#### View stash changes using show

It will be useful to view the changes which have been stashed.

```
git stash show
```

The above command will show the changes in the latest stash (i.e) stash@{0}

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F27285f69-2478-4750-a9eb-34952fc77bbf_402x108.png)

To view the changes of any particular stash

```
git stash show stash@{2}
```

To view the diff changes of stash, you can use

```
git stash show -p 
git stash show stash@{10} -p
```
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5dfba4df-1937-44bf-8c61-d73b27d1bfac_392x161.png)

#### Applying stashed changes in the working directory

Now you know how to stash the changes let’s see how to apply those changes in the working directory.

```
git stash apply
```

The above command will take the latest stash (i.e) stash@{0} an apply it to the working directory.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F8d65a1d0-988a-4746-b47c-2418d181ed48_657x349.png)

In the above image, you can see that changes in the latest stash are applied.

To apply the particular stash, you can use

```
git stash apply stash@{5}
```

##### Using pop

There is also another option to applying the stash in the working directory.

```
git stash pop
```

This command will apply the changes of the latest stash and will also delete the stash from the stash list.

Use this command when you don’t need the stash anymore after applying the stash.

Use **apply** when you need to apply the stash and still, you don’t want to delete the stash from the list.

#### Delete a stash

```
git stash drop
```

This will delete the latest stash.

```
git stash drop stash@{10}
```

This will drop the particular stash.

#### Create a branch from git stash

```
git stash branch <branch_name> stash@{5}
```

This command will create a new branch with the specified stash name. And this will delete the stash from the list like **pop.**

Thank you for reading :)

If you got any new tricks you share, you can share it in the comments :) :)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.