DEV Community

Idris
Idris

Posted on

Mastering Git Stash: A Handy Tool for Managing Uncommitted Changes

When you're working with Git, it's common to encounter situations where you're in the middle of something, but you need to switch contexts quickly—whether it's switching branches, trying a new feature, or fixing an urgent bug. In these cases, git stash becomes your best friend. This command allows you to temporarily save your uncommitted changes and retrieve them later, without cluttering your commit history. Let's dive into how to use git stash and why it’s a vital tool in your Git toolkit.

What is Git Stash?

Git is all about keeping track of changes, but what if you're not ready to commit those changes yet? Or maybe you're in the middle of working on something, but you need to change branches to fix a bug? Committing incomplete work can clutter your commit history, and simply leaving your changes untracked isn’t ideal. Enter git stash.

git stash allows you to temporarily save your uncommitted changes (both staged and unstaged) to a stack-like structure. This way, you can apply those changes later without committing them. It’s like a clipboard for your code changes, making it easy to pick up where you left off.

Basic Git Stash Commands

Let’s take a look at the most commonly used commands when working with git stash.

1. Stash Changes

When you're in the middle of something, but you need to switch branches or do something else, you can save your changes using:

git stash
Enter fullscreen mode Exit fullscreen mode

This command saves both staged and unstaged changes and leaves your working directory clean. But what if you have untracked files you want to stash too? Use the -u (or --include-untracked) flag:

git stash -u
Enter fullscreen mode Exit fullscreen mode

If you also want to stash ignored files, use:

git stash -a
Enter fullscreen mode Exit fullscreen mode

2. List Stashes

You can stash multiple sets of changes, and Git will keep track of them in a stack-like fashion. To see all your stashes, use:

git stash list
Enter fullscreen mode Exit fullscreen mode

Each stash is numbered (stash@{0}, stash@{1}, etc.), making it easy to reference the specific stash you want to work with.

3. Apply Stash

Once you've switched branches or done whatever you needed, you can retrieve your stashed changes. To apply the latest stash:

git stash apply
Enter fullscreen mode Exit fullscreen mode

If you want to apply a specific stash (e.g., stash@{0}), use:

git stash apply stash@{0}
Enter fullscreen mode Exit fullscreen mode

This command applies the stash but keeps it in the stash list. If you’re sure you don’t need the stash anymore, you can use git stash pop, which applies and removes the stash from the list.

4. Pop Stash

To apply the latest stash and remove it from the stash list (like "popping" it off the stack), use:

git stash pop
Enter fullscreen mode Exit fullscreen mode

5. Drop Stash

If you no longer need a specific stash, you can remove it from the list without applying it:

git stash drop stash@{0}
Enter fullscreen mode Exit fullscreen mode

6. Clear All Stashes

If you want to remove all stashes at once, use:

git stash clear
Enter fullscreen mode Exit fullscreen mode

Advanced Git Stash Usage

Stash Only Unstaged Changes

Sometimes, you only want to stash the changes that haven't been staged. To do this, use the -k (or --keep-index) flag:

git stash push -k
Enter fullscreen mode Exit fullscreen mode

This will stash only the unstaged changes, leaving the staged changes intact.

Creating a Named Stash

To keep your stashes organized, you can name them with a helpful description. For example:

git stash push -m "Work in progress on feature X"
Enter fullscreen mode Exit fullscreen mode

This makes it easier to identify the stash when you’re looking through your stash list later.

Stash Specific Files

If you only want to stash changes to specific files or directories, you can specify them directly:

git stash push path/to/file
Enter fullscreen mode Exit fullscreen mode

This allows you to stash part of your changes while leaving other changes intact.

Real-World Use Cases for Git Stash

Here are some scenarios where git stash is particularly helpful:

  1. Switching Branches Mid-Work
    You're working on a feature, but suddenly need to switch to another branch to fix a bug. Instead of committing incomplete work, you can stash your changes, switch branches, and then come back to your work later.

  2. Trying Something Experimental
    You want to test a new idea or feature, but you’re unsure if it will work. Stashing your current work gives you the flexibility to experiment without committing your code.

  3. Interruptions and Context Switching
    You may need to quickly fix something else or change directions mid-way through a task. Instead of losing your progress, stashing gives you a safe place to store your unfinished work and return to it later.

  4. Stashing Untracked and Ignored Files
    If you're working with new files that aren't tracked by Git yet, or if you're dealing with ignored files, git stash can still save them (using -u and -a flags). This is handy when you want to save everything, not just the tracked files.

Conclusion

git stash is a simple yet powerful tool that can save you from a lot of Git headaches. Whether you need to temporarily shelve your changes while you fix a bug, experiment with a new feature, or switch branches without committing, git stash is there to help you stay organized and productive. By mastering this command, you'll become more efficient in managing your workflow and keeping your Git history clean.

With just a few commands, you can seamlessly switch between tasks and return to your previous work without missing a beat. So the next time you find yourself needing to pause your current work, remember to use git stash to keep things moving forward!


If you found this guide helpful, feel free to share it with your colleagues or drop a comment with any questions or additional tips you have about git stash! Happy coding!

Top comments (0)