DEV Community

Cover image for How to Use Git Stash Command
Mwafrika Josué
Mwafrika Josué

Posted on

How to Use Git Stash Command

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

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

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`
Enter fullscreen mode Exit fullscreen mode

Or

git stash save
Enter fullscreen mode Exit fullscreen mode

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`
Enter fullscreen mode Exit fullscreen mode

or

`git stash --include-untracked`
Enter fullscreen mode Exit fullscreen mode
  • To stash untracked files and ignored files:
`git stash -a`
Enter fullscreen mode Exit fullscreen mode

or

`git stash --all` 
Enter fullscreen mode Exit fullscreen mode

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

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`
Enter fullscreen mode Exit fullscreen mode

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`
Enter fullscreen mode Exit fullscreen mode

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

`git stash pop stash@{1}` 
Enter fullscreen mode Exit fullscreen mode

Or

`git stash apply stash@{1} `
Enter fullscreen mode Exit fullscreen mode

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`
Enter fullscreen mode Exit fullscreen mode
  • To delete a particular stash from the stash list.
`git stash drop <stash_id>` 
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode
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 +-
Enter fullscreen mode Exit fullscreen mode

You can also get a more detailed diff, pass the --patch or -p flag:

 git stash show stash@{0} --patch
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)