| |

Git Stash: A Comprehensive Guide

Git is a powerful version control system that allows developers to track changes in their codebase and collaborate efficiently. One of the most useful and often underutilised features of Git is the “stash” command. Git stash is a versatile tool that allows you to temporarily save changes that are not ready to be committed, switch branches, and manage your work more effectively. In this comprehensive guide, we will explore Git stash in depth, covering its various use cases, commands, best practices, and some advanced techniques.

Understanding the Need for Git Stash

Before diving into the intricacies of Git stash, let’s understand why it’s a crucial tool for developers.

  1. Temporary Storage: Git stash provides a safe and efficient way to store changes that are not yet ready to be committed but need to be set aside temporarily. This is especially helpful when you need to switch to a different branch or address an urgent bug fix without losing your current work.
  2. Maintaining a Clean Working Directory: Stashing helps you maintain a clean working directory, reducing the risk of accidentally committing incomplete or experimental changes.
  3. Conflict Resolution: When you encounter merge conflicts while switching branches, stashing can help you resolve conflicts more easily by allowing you to switch branches cleanly and then apply your stashed changes back.
  4. Experimentation: You can use Git stash to experiment with different code changes without affecting your current working state. If an experiment doesn’t work out, you can simply drop the stash without committing it.

Now that we understand why Git stash is essential, let’s delve into the various aspects of using it effectively.

Basic Git Stash Commands

Stashing Changes

The most fundamental command for using Git stash is git stash save or simply git stash. Here’s how it works:

git stash save "Descriptive stash message"

This command will stash your current changes, providing a descriptive message to help you identify the stash’s purpose later.

Viewing Stashes

To view your stashes, use the following command:

git stash list

This command displays a list of all stashes, including their stash IDs and messages.

Applying Stashed Changes

To apply the latest stash (usually the one on top of the stash stack) and remove it from the stash stack, use the following command:

bashCopy codegit stash pop

Alternatively, if you want to apply a specific stash, you can use:

git stash apply stash@{n}

Where n is the stash index you want to apply.

Dropping Stashes

To remove a stash from the stash stack without applying it, use the following command:

git stash drop stash@{n}

Clearing All Stashes

If you want to remove all stashes, you can use:

git stash clear

Advanced Stash Techniques

Stashing Untracked Files

By default, Git stash only saves changes to tracked files. If you want to stash untracked files as well, you can use the -u or --include-untracked option:

git stash save -u "Stash untracked files"

Stashing with a Stash Message

Adding a meaningful message when stashing is crucial for identifying the purpose of a stash later. This can be incredibly helpful in a team setting or when you have many stashes. The stash message should be enclosed in double quotes after the git stash save command.

Stashing Interactively

Git provides an interactive mode for stashing, which allows you to choose exactly what you want to stash and what you want to keep in your working directory. To access this mode, use the following command:

git stash save -p

This command will present each change (hunk) individually and prompt you to decide whether to stash it or not.

Multiple Stashes

You can create multiple stashes by repeatedly running git stash save with different messages. This allows you to manage and prioritize different sets of changes separately.

Stash Branches

Sometimes, you might want to work on a specific stash in isolation. You can create a new branch from a stash using the following command:

git stash branch <branch_name> stash@{n}

This command creates a new branch <branch_name> and applies the stash stash@{n} to it. It’s a useful technique for isolating and working on specific features or bug fixes.

Best Practices

To make the most of Git stash, consider these best practices:

  1. Use Descriptive Stash Messages: Always provide a meaningful message when stashing your changes. This helps you remember why you stashed the changes and makes it easier to identify the correct stash to apply later.
  2. Stash Frequently: Stash changes frequently, especially before switching branches or pulling updates from a remote repository. This ensures a clean working directory and reduces the chances of conflicts.
  3. Regularly Check and Manage Stashes: Periodically review your stashes using git stash list to keep track of your stashed changes. Remove stashes that are no longer needed to avoid clutter.
  4. Combine Stashes: If you have multiple stashes that are related to the same feature or bug fix, consider combining them into a single stash with a clear message. This simplifies the management of stashes.


Git stash is an invaluable tool in a developer’s arsenal. It enables you to manage your work effectively, switch between tasks seamlessly, and experiment with code changes without the risk of losing your work. By understanding the basic stash commands, exploring advanced techniques, and following best practices, you can become a master of Git stash and streamline your development workflow. Incorporate Git stash into your daily Git routine, and you’ll find it to be a reliable companion in your coding journey.

Similar Posts