How to Use Git Stash Command

Save your work, without committing a crime!

Post cover displaying the title and the Git logo
Stash it for later!

Learn how to use Git stash in case you ever wondered how to save your changes without committing them; that's exactly the point from Git stash, you stash the changes in a dirty working directory.

In this BitCode you will learn the essentials of Git stash to get going.

When To Use Git Stash?

Often when you work on some part of a project, and you would like to switch to work on another thing unrelated in even a different branch, you would like to save your changes without committing them; maybe because they are undone or needs some refactoring.

This is where Git stash is super useful, now let's dive into the essential commands.

Create a Git Stash

To create a stash, there are two commands to do it.

Both git stash/git stash push; will create an entry holding the current

Note: Keep in mind that Git stash will only hold changes from already tracked files and/or staged changes; meaning if you just created a file, you will have to stage it first before being able to stash it.

Creating a stash is awesome, but it stores the stashes with a naming like WIP on branchname which is not ideal if you have a lot of stashes.

So to create a stash with a message, you can use the following command:

git stash push -m "A message so I can remember what this stash is about"

Like this, you won't forget what the stash is about, and you will be able to apply a stash to the working directory with ease from the list.

List & Retrieve Git Stashes

Now we will see how to list Git stashes and apply them into our working directory.

To list the stashes:

git stash list

# Should print something like this:
# stash@{0}: On master: Another stash for a new feature
# stash@{1}: On master: A stash with a message

To retrieve a stash:

Before we start retrieving the stashes, we need to know the commands used and the difference between them.

The commands are: git stash apply and git stash pop

The difference between them is that apply applies the stash to the working directory and keeps the stash entry intact, while pop applies the stash and removes the stash entry from the list.

Now that this is cleared out, let's dig into it:

# Apply the most recent stash:
git stash apply # or git stash pop

# Apply a specific stash by it's index
git stash apply <index> # or git stash pop <index>

Keep in mind that while applying a stash you can encounter a conflict, this is normal you just have to solve it as any other merge conflict.

In the case of using pop the stash is not removed, so after solving the conflict you should remove the stash manually by using drop which is next in the post.

Drop a Stash or Clear All Stashes

As you can create, you can delete, and you have the choice to delete a specific stash or clear out the list.

To delete a single stash:

git stash drop <index>

To clear out the whole list:

git stash clear

Conclusion

Git has a lot under it sleeves, and Git stash is one of those things I wish I learned since day one.

Sources