How to Use Git Stash Command
Tags: git, githunt, github, gitstash
Published: 2022-08-30 10:49:00

## 10 Tips you should know about Git Stash

Assume you are working on a complex feature that demands concentration, and your boss instructs you to solve an urgent problem, and you just noticed that the final change for that feature was implemented in a different branch.

In such a case, the first thought that comes to mind is to checkout to the branch that has the implemented features.

If you try to checkout to another branch from unstaged to staged modifications, you can run into the following issue:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2ko6ozoe2hxv0fc590b.png
)

> What happened in the above screenshot ?

In this case, the current branch's staged and unstaged modifications will be applied to the branch you switched to.

Next, Git does not always enable you to swap branches without first committing your changes. This is due to the possibility of losing the modifications you made in your current branch or having them clash with the destination branch. For whatever reason, we can't move branches without first committing or stashing the modifications.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mq51rfx8h4lty6vnwndn.png)

To address the aforementioned issue, we must stash the modifications in our working environment. Stashing means to store changes safely in a hidden place (the stash stack) for later use.

## How to stash your changes ?
`Git stash` saves the uncommitted changes locally, allowing you to make changes, switch branches, and perform other Git operations

```
`git stash`
```
 Or 

```
git stash save
```
## How to stash untracked files ?
You can use additional options to let git stash take care of untracked and ignored files.

- To stash untracked files: 

```
`git stash -u`
```
  or 
```
`git stash --include-untracked`
```

- To stash untracked files and ignored files:

```
`git stash -a`
```
 or 

```
`git stash --all` 
```

## How to list stashes ?

You can view your stashes with the command `git stash list`. Stashes are saved in a last-in-first-out (LIFO) approach:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/faqrtsvk7t22rnt7io3t.png)
## How to apply the stash ?
Run the following command to apply the recorded changes of your newest stash to the current working branch and delete that stash from the stash stack:

```
`git stash pop`
```

You could also wish to apply your most recent stash to the current working branch without deleting it from the stack. To accomplish this, use the following command:

```
`git stash apply`
```

You can choose which stash you want to pop or apply by passing the identifier as the last argument:

```
`git stash pop stash@{1}` 
```
Or

```
`git stash apply stash@{1} `
```

## How to delete a stash ?
It is good practice to remove stashes that are no longer needed. You must do this manually with the following commands:

- To empty the stash list by removing all the stashes
```
`git stash clear`
```
- To delete a particular stash from the stash list.
```
`git stash drop <stash_id>` 
```

## How to create a branch from stash ?
It is possible to create a new branch from your latest stash. Just use this command:

```
git stash branch <branch_name>
```

If you want to create a branch from an earlier stash, that's also possible by using stash reference:
```
git stash branch <branch_name> stash@{revision}
```

## How to add a description to stash ?

By default, stashes are marked as WIP on top of the branch and commit that you created the stash from. However, this limited amount of information isn't helpful when you have multiple stashes, as it becomes difficult to remember or individually check their contents. To add a description to the stash, you can use the command `git stash save <description>`:

```
git stash save "remove semi-colon from schema"
Saved working directory and index state On master: remove semi-colon from schema
```
Now if you list all the available stashes you will see something similar to this:

```
git stash list
stash@{0}: On master: remove semi-colon from schema
stash@{1}: WIP on master: d7435644 Feat: configure graphql endpoint
```

## How to Check a stash diffs ?
Sometimes you may need to view the stash diff, Use the command below to view the stash diff:
```
git stash show <stash_id>
```
```
git stash show stash@{1}
console/console-init/ui/.graphqlrc.yml        |   4 +-
console/console-init/ui/generated-frontend.ts | 742 +++++++++---------
console/console-init/ui/package.json          |   2 +-
```
You can also get a more detailed diff, pass the --patch or -p flag:
```
 git stash show stash@{0} --patch
```


## Conclusion

I hope you found this article useful and learned something new. If I missed any useful options for using stash, please let me know in the comments. For a fast reference, see [git stash command cheat sheet](https://gist.github.com/Preethi-Dev/fa8ae46a75761356dc1fa711376c8345)