Mastering Git Stash: The Ultimate Guide for Quick Code Switching

You are halfway through a feature when an urgent bug lands on another branch. Your changes are real, but they are not ready for a commit. If you switch branches without first protecting that work, Git may block the checkout—or you may end up mixing unrelated changes. Git stash gives you a temporary place to save local edits, switch context, and restore the work when you are ready.

This guide explains how to use git stash for quick code switching, how to choose between git stash pop and git stash apply, how to find a specific stash, and how to clean up safely.

What does Git stash do?

A stash records changes from your working tree and index, then returns those tracked files to the state of your latest commit (HEAD). Think of it as a temporary local snapshot for work you want to set aside—not as a commit or a long-term backup.

By default, Git stashes changes to tracked files. New, untracked files are not included unless you ask for them. Stash entries are stored in a stack: the newest entry is usually stash@{0}, followed by older entries such as stash@{1}.

The core command: git stash

The simplest command is:

git stash

Git saves your tracked, staged and unstaged changes, then cleans those changes from the working tree so you can change branches. The modern explicit form is git stash push; it makes the action clearer and supports useful options.

Add a message so you can recognize the stash later:

git stash push -m "Finish search form validation"

Include new untracked files when you stash:

git stash push -u -m "WIP: search form and test fixture"

The -u option (also written --include-untracked) includes untracked files. Use -a or --all only when you also want ignored files included; Git cleans those files from the working directory as part of the operation. Check what will be affected before using it, especially if ignored files contain local configuration or generated data.

You can also stash only selected paths by adding a pathspec after --:

git stash push -m "Save API work" -- src/api/

For an interactive, hunk-by-hunk selection, use git stash push -p. Review the prompts carefully to choose which changes to save.

Switch branches after stashing

Once the working tree is clean, switch to the branch you need:

git switch main

Do the urgent work, then return to your original branch when you are ready:

git switch feature/search-form

Before restoring a stash, check your branch and working tree. Applying changes onto a different branch can be useful, but it can also create conflicts if the surrounding code has changed.

Restore changes with git stash pop

Use pop when you want to apply the latest stash and remove it from the stash list if the application succeeds:

git stash pop

To pop a particular entry, name it explicitly:

git stash pop "stash@{1}"

Git applies that saved work to the current working tree. If the application succeeds, the stash entry is dropped. If Git encounters conflicts, it keeps the stash entry so you can resolve the changes and try again or apply it elsewhere. After a successful resolution, inspect your work with git status and review the diff before committing.

Restore changes with git stash apply

Use apply when you want to restore changes but keep the stash entry available as a backup or for reuse:

git stash apply

Apply a specific stash without removing it:

git stash apply "stash@{2}"

In short, pop applies and then removes the entry on success; apply applies and keeps the entry. When you are unsure whether an old stash will apply cleanly, start with apply. Once you have verified the restored work and no longer need the saved copy, remove it deliberately with git stash drop.

If you need to restore which changes were staged, add --index:

git stash apply --index "stash@{0}"

Restoring the original index state can fail when conflicts prevent Git from recreating the staged changes exactly. You can resolve the conflict manually and stage the appropriate files afterward.

Manage multiple stashes with git stash list

When you stash work more than once, list the saved entries before restoring anything:

git stash list

Example output:

stash@{0}: On feature/search-form: WIP: add validation
stash@{1}: On main: Update README draft
stash@{2}: On feature/profile: Add avatar upload

The index in braces identifies an entry: stash@{0} is newest, and larger numbers refer to older entries. A descriptive message helps you select the right one. You can inspect a stash before applying it:

git stash show "stash@{1}"
git stash show -p "stash@{1}"

The first command summarizes the changes; -p displays the patch. Stash indexes can change after you drop or pop entries, so run git stash list again instead of relying on an index you noted earlier.

Delete a stash with git stash drop

After confirming you no longer need a stash, remove one entry by specifying it:

git stash drop "stash@{1}"

If you omit the entry, Git drops the latest stash. Check the list first so you do not remove the wrong snapshot. Dropping a stash removes its reference from the stash list; it is not the same as deleting a branch, and it should be treated as cleanup rather than as a recovery strategy.

Remove all stashes with git stash clear

git stash clear removes every stash entry in the repository:

git stash clear

Use this only after reviewing git stash list and confirming that none of the saved work is needed. Clearing the list can make those commits difficult to recover, and Git may eventually prune unreachable objects. If you need only one stash gone, use git stash drop instead.

Best practices to avoid stash conflicts

  • Give each stash a short, specific message with -m.
  • Use git stash list and git stash show -p to identify and inspect an entry before restoring it.
  • Choose pop when you are done with the stash; choose apply when you want to retain a copy.
  • Return to the branch where the work started when practical, and make sure your working tree is clean before applying.
  • Use -u if your new files matter. Remember that ignored files require -a.
  • Resolve conflicts carefully, then run git status and inspect your diff. A stash is not a substitute for a durable commit or backup.
  • Do not run git stash clear as routine cleanup; remove only entries you have reviewed.

Quick reference

git stash push -m "message"       # Save tracked changes with a description
git stash push -u -m "message"    # Include untracked files
git stash list                    # List saved stashes
git stash show -p "stash@{1}"    # Inspect a specific stash
git stash apply "stash@{1}"      # Restore and keep the entry
git stash pop "stash@{1}"        # Restore and remove on success
git stash drop "stash@{1}"       # Remove one entry
git stash clear                   # Remove all entries

Frequently asked questions

Does git stash include untracked files?

No. Use git stash push -u to include untracked files. Use -a to include ignored files as well.

Can I apply a stash to a different branch?

Yes. Switch to the target branch and run git stash apply with the right entry. If the target branch has diverged, conflicts may need to be resolved manually.

What happens if git stash pop has conflicts?

Git leaves the stash entry in the list when it cannot apply cleanly. Resolve the conflicts, inspect the result, and drop the stash only when you are sure you no longer need it.

For the complete command reference, see the official Git stash documentation.

Conclusion

Git stash makes quick code switching easier by letting you save unfinished local changes, move to another branch, and restore your work when the time is right. Use messages and the stash list to stay organized, choose between pop and apply based on whether you want to keep the saved entry, and delete stashes only after reviewing them. Used carefully, stashing keeps branch changes focused and helps you handle interruptions without rushing an incomplete commit.

 

Post a Comment

0 Comments