Understanding git stash: A Beginners Overview

Understanding git stash: A Beginners Overview

When work shifts to another task, unfinished changes can still be sitting in your working directory. A Git stash saves changes to files Git tracks and clears them from your working directory for later restoration.

I’ll explain how to save changes with a message and restore the stash entry you choose.

TL;DR

Save a stash with git stash push -m “message”, find its reference with git stash list, then use that reference with git stash apply or git stash pop.

  • Git stores the message as a description, not as a separate stash name.
  • apply restores changes and keeps the stash entry.
  • pop restores changes and removes the entry after a successful application.

What is a Git stash?

A Git stash is a saved snapshot of tracked changes in your working tree and index, letting you clear those changes from the working directory for a while and apply them later.

A stash message makes entries clearer to recognize in the stash list, but Git still identifies each entry by a reference such as stash@{0}. Use that reference when restoring one particular entry.

What you need before naming a stash

Run these commands inside a Git repository with local changes you want to set aside and a descriptive message ready to identify that work.

git status --short

Check which files are modified before stashing because the default saves changes to tracked files, whether staged or unstaged, and leaves untracked files out.

How to save and restore a named Git stash step by step

Create the entry with a message, read its reference from the list, and choose whether to keep it after restoring. The commands below use a local example repository and show the recorded output.

Step 1: Create a stash with a descriptive message

Use push -m to save tracked changes with a short description. Replace the message with a label that describes the work.

git stash push -m "config-change"

The command returned:

Terminal output from git stash push with a descriptive message
Saved working directory and index state On master: config-change

The message is attached to the new entry. The working tree is reset to the state Git had before those changes.

Step 2: Find the message in the stash list

List saved entries to see each message and its current reference.

git stash list

The example output is:

stash@{0}: On master: config-change

stash@{0} is the newest entry in this example. Older entries have higher index numbers, and the indexes can change when you add or remove entries, so check the list before using one.

Step 3: Apply or pop the exact stash reference

Pass the listed reference to apply when you want to restore the changes and retain the entry.

git stash apply stash@{0}

The command reported that notes.txt was modified and exited successfully. The stash entry remains available, so you can apply it again or remove it later.

Use pop instead when the changes should be restored and the entry removed after the application succeeds:

git stash pop stash@{0}

The command returned:

On branch master
Changes not staged for commit:
	modified:   notes.txt

no changes added to commit
Dropped stash@{0} (5f204b424ee21835ea7b8524339153943e148be4)

If applying the stash causes conflicts, Git does not drop it during pop. Resolve the conflicts, review the working tree, and remove the entry only when you no longer need it.

Common Git stash errors and edge cases

A message is a description, not a unique key. If several entries use similar messages, choose the exact stash@{n} reference from the current list instead of relying on a message search.

Git also supports revision searches such as stash^{/message}, but the message is treated as a search expression and can match more than one commit. The explicit list reference is clearer when selecting one saved entry.

Ordinary stash creation leaves untracked files out. Add -u to include untracked files, or -a to include ignored files as well:

git stash push -u -m "config-change-and-new-file"
git stash push -a -m "include-ignored-files-too"

The save subcommand appears in older tutorials, but Git documents git stash push as its replacement. Use push -m for new commands.

Conclusion

A stash message helps you recognize saved work, while the stash reference selects the entry. Check git stash list immediately before restoring, then choose apply to retain it or pop to remove it after a successful restore.

For related Git tasks, see the site guides to basic Git commands, shallow clones, recovering from detached HEAD, installing Git on Linux, and using GitHub CLI.

Can I apply a Git stash by its name?

Git does not give a stash message a unique name. Find the entry with git stash list, then apply its stash@{n} reference. Revision-message search syntax exists, but can match more than one entry.

Does git stash pop delete the stash?

After Git applies the changes successfully, pop removes that stash entry. If applying it causes conflicts, Git keeps the entry so you can resolve the conflicts and decide what to do next.

How do I include untracked files in a named stash?

Use git stash push -u -m “message” to include untracked files. Use -a instead of -u when ignored files should be included too.