Skip to main content

Git Stash

What is Git Stash?

git stash is a command in Git that allows you to save changes that you have made to your working directory but do not want to commit yet, so you can switch to a different branch and work on another task. This is especially useful when you are in the middle of a task and another high-priority task comes up.

The git stash command takes your modified tracked files and staged changes, saves them away for later use, and then reverts them to the state of the last commit. These changes are stored in a "stash" and can be reapplied later when you're ready.

Here's a basic outline of how to use git stash:

  1. Stashing Changes: When you have made changes in your working directory that you want to save for later, you can use the command git stash save "your message" to create a new stash. The message is optional but it can help you remember what changes are in the stash.

  2. Listing Stashes: You can view all of your stashes with the command git stash list. This will give you a list of all your stashes, which you can refer to when you want to reapply or delete a stash.

  3. Reapplying Stashes: When you're ready to continue working on your stashed changes, you can use the command git stash apply to reapply the most recently stashed changes. If you want to reapply a different stash, you can specify it by using its name from the git stash list.

  4. Deleting Stashes: Once you've reapplied your changes and no longer need the stash, you can remove it with the command git stash drop, followed by the stash's name.

  5. Popping Stashes: The command git stash pop works similarly to apply, but it also deletes the stash after applying it.

Understanding git stash can help you manage your workflow more efficiently, especially when you need to switch between different tasks.

Saving Work In Progress with Git Stash

While working on a Git project, you might want to switch tasks without committing incomplete changes. Git stash is a command that lets you do this. In this tutorial, we'll explain how to use Git stash to save your work in progress.

To stash your changes, use the git stash command:

git stash

This command takes your modified tracked files and staged changes, saves them on a stack of unfinished changes that you can reapply at any time, and then reverts them.

Applying Stashed Changes with Git

After you've stashed your work with Git, the next step is to reapply these changes when you're ready. This process is called "applying stashed changes." In this tutorial, we'll show you how to apply stashed changes using Git.

How to View Stashed Changes

First, if you want to see a list of your stashed changes, you can use the git stash list command:

git stash list

This command will show you a list of all the changes you've stashed.

How to Apply Stashed Changes

When you're ready to reapply your stashed changes, you can use the git stash apply command. By default, this command applies the most recently stashed changes:

git stash apply

If you've stashed multiple sets of changes and want to apply a different stash, you can do so by specifying the stash's name:

git stash apply stash@{2}

This command applies the stash named stash@{2}.

Deleting Stashes with Git

In Git, once you apply a stash, it isn't automatically removed from the stash list. It might be necessary to delete stashes to keep your stash list tidy. In this tutorial, we'll explain how to delete stashes using Git.

How to Delete a Specific Stash

To delete a specific stash, you can use the git stash drop command followed by the name of the stash:

git stash drop stash@{2}

This command deletes the stash named "stash@{2}".

How to Delete All Stashes

If you want to delete all the stashes at once, you can use the git stash clear command:

git stash clear

This command deletes all stashes.

Conclusion

Git stash is a powerful command that lets you switch between tasks seamlessly. It allows you to save your work in progress and return to it later, keeping your workspace clean and organized. Understanding how to use Git stash will make you a more efficient Git user.