Data report"State of code review 2024" is now liveRead the full report

Naming stashes in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Git stash is a feature used to temporarily save modified and staged changes without committing them, allowing you to switch branches with a clean working directory. Naming these stashes can further enhance your workflow by making it easier to remember the purpose of each stash. This guide will explore how to use named stashes in Git, including step-by-step examples and terminal outputs.

git stash is a git command that temporarily saves (or stashes) changes you've made to your working directory, so you can work on something else, and then come back and re-apply them later on. This feature is particularly useful when you need to quickly switch contexts and work on different branches without committing unfinished work.

  1. Creating a named stash: When you stash changes, Git normally saves them in a stack with a generic name like stash@{0}. You can add a custom message or name to a stash for better identification by using the -m flag:

    Terminal
    git stash push -m "Feature X updates"

    Here, "Feature X updates" is the custom name for your stash. This makes it easier to identify the stash later on.

  2. Listing all stashes: To view all your stashes along with their names, use:

    Terminal
    git stash list

    This will show a list of all stashes stored in the stack, including any names you've assigned to them. For example, you might see:

    Terminal
    stash@{0}: On main: Feature X updates
    stash@{1}: On dev: Bug fix for Y
  3. Applying a named stash: To apply a named stash and remove it from the stash list, use the git stash pop command with the stash identifier:

    Terminal
    git stash pop stash@{0}

    If you want to apply the stash but keep it in the stash list, use:

    Terminal
    git stash apply stash@{0}
  4. Finding a specific named stash: If you have many stashes, you can filter them by name using grep:

    Terminal
    git stash list | grep "Feature X"

    This command will search through the stash list and return entries matching "Feature X".

  5. Dropping a named stash: If you decide that you no longer need a specific stash, you can drop it using:

    Terminal
    git stash drop stash@{0}

    Replace stash@{0} with the identifier of the stash you want to drop.

  6. Using stashes effectively:

    • Descriptive names: Always use descriptive names for your stashes. This practice helps you quickly identify the purpose of each stash, especially in a collaborative environment where multiple developers might use the stash feature.
    • Regular cleanup: Regularly review and clean up your stashes. This prevents the build-up of unnecessary stashes that can clutter your stash list and confuse you and your team.

Here’s a practical example showing the creation, application, and deletion of a named stash in a typical development workflow:

  1. Stashing changes: Suppose you’re working on a feature and need to switch branches to fix a critical bug. First, stash your changes:

    Terminal
    git stash push -m "Incomplete feature A"
  2. Switching branches: Switch to your bug fix branch:

    Terminal
    git checkout bugfix-branch
  3. Returning and applying stash: After completing your bug fix, switch back and reapply your stashed changes:

    Terminal
    git checkout feature-branch
    git stash pop
  4. Cleaning up: Confirm the stash is applied correctly, then run git stash list to ensure it’s been removed or delete it manually if necessary.

By naming your stashes, you can maintain a clearer, more organized workflow, especially when juggling multiple tasks across several branches. This practice enhances both personal productivity and collaboration by providing a snapshot of changes with context, useful in fast-paced development environments.

For further reading on naming stashes see the official Git documentation on stashing.

Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2