To restore changes saved with Git stash, use git stash apply to bring them back while keeping the stash, or git stash pop to apply them and remove the stash if successful. Choose the first when you want a safer copy; choose the second when you are ready to consume the saved entry.
What a Git stash saves
A stash temporarily stores work from your working tree so you can switch tasks or branches without making a commit. A typical command, git stash, saves tracked-file changes from both the working tree and the index, then restores those tracked files to the state of the current commit. The stash is stored in the repository, not as a normal commit on your branch.
By default, untracked files are not included. Use git stash -u (also written --include-untracked) to include untracked files. Ignored files are excluded unless you use git stash -a or --all; that option can capture build artifacts and other ignored content, so use it deliberately.
Git typically records a stash as a set of commits that preserve the saved changes and their base. You do not need to manipulate those internal commits for routine restoration: the stash apply, stash pop, and stash list commands are the usual interface.
Apply or pop: which command should you use?
git stash applyrestores a stash but leaves it in the stash list. Use this when you may need to retry, compare, or apply the same saved work elsewhere.git stash popattempts to restore a stash and then removes it from the stash list if application succeeds. If conflicts prevent a clean application, Git generally keeps the entry so you can resolve the conflict without losing the saved version.
Neither command automatically commits the restored changes. After applying, they are ordinary working-tree changes again. If the stash included staged changes, Git can restore the index state as well; use --index when you want to request restoration of the original staging state explicitly. For example:
git stash apply --index stash@{1}applies a particular stash and asks Git to restore its index state.git stash pop --indexdoes the same for the newest stash and removes it only after successful application.
Find and restore the right stash
Stashes are numbered from the newest entry, so stash@{0} is normally the latest. List them before restoring if you have more than one:
git stash list
git stash show -p stash@{1}
git stash apply stash@{1}
The first command lists entries, often with the branch and message recorded when each was created. The second displays a patch for the selected entry, helping confirm that it contains the work you want. The third applies that entry without deleting it.
To make entries easier to identify, create a descriptive message when stashing, such as git stash push -m "unfinished search form". You can restore a particular entry by its reference even when it is not the newest. In shells that treat braces specially, quote the reference—for example, git stash apply 'stash@{1}'.
Restore only part of a stash
If you do not want every change from a stash, git stash apply is not the only option. You can use git stash show -p to inspect its patch, then restore selected paths with git restore and the stash as the source. For example:
git restore --source=stash@{0} -- path/to/file
This copies that path’s contents from the stash into your working tree. It does not automatically recreate the stash’s staging state, and it can overwrite changes currently present in the chosen path. Inspect your work first and avoid running it on files whose current edits you need to keep. For interactive selection of individual hunks, git stash apply followed by interactive staging or patch tools may be more suitable; the best approach depends on whether you need to preserve existing edits.
Resolve conflicts and avoid losing work
A stash can conflict when the branch has changed in the same places as the saved work. Git marks affected files as conflicted, much as it does during a merge. Open each file, choose or combine the intended lines, remove conflict markers, then stage resolved files with git add. Check the result with git status and review the diff before committing.
If git stash pop reports conflicts, do not assume the stash was deleted. Check git stash list; the entry is normally retained when Git cannot complete the pop. Resolve the files, verify the result, and drop the entry only if you no longer need it. If you are uncertain, prefer apply next time because it leaves the saved entry available during recovery.
Before a risky operation, inspect current changes with git status and consider making a commit or a separate backup. Applying a stash can mix its edits with existing work, and applying the same stash repeatedly can introduce duplicate or conflicting changes. A stash is useful short-term storage, not a substitute for a durable commit or backup.
Common mistakes and recovery options
- Applying the wrong entry: check
git stash listand inspect the patch withgit stash show -pbefore applying. - Expecting untracked files: a default stash does not include them. They are saved only if the stash was created with an option such as
-uor-a. - Expecting a commit: applying restores edits; it does not commit them. Review and commit them separately if appropriate.
- Deleting an entry too soon:
git stash dropremoves a stash reference. Confirm that the work is restored and no longer needed before dropping it.
If a stash was dropped or cleared, recovery is not guaranteed. Git may eventually prune unreachable objects, and finding a lost stash can require inspecting repository objects with commands such as git fsck. Stop making cleanup or pruning operations, preserve a copy of the repository, and seek Git-specific help if the changes are important. It is safer not to rely on this recovery path.
Frequently Asked Questions
How do I restore the latest Git stash?
Run git stash apply to restore the newest entry and keep it, or git stash pop to restore it and remove it after successful application.
How do I apply an older stash?
List entries with git stash list, then use its reference, such as git stash apply stash@{2}. Inspect the entry first if you are unsure what it contains.
Does git stash pop delete the stash?
It removes the entry after a successful application. When conflicts prevent completion, Git normally keeps the stash so you can resolve the changes.
How do I restore a stash without losing it?
Use git stash apply, not git stash pop. The stash remains listed until you explicitly remove it.
Can I restore one file from a stash?
Yes. Use git restore --source=stash@{0} -- path/to/file for the desired path, after checking that overwriting its current contents is safe.
Why are my untracked files missing after applying a stash?
The default stash does not save untracked files. They must have been included when the stash was created, for example with git stash -u.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- How to Rebrand a Tech Company Without Confusing Customers
- Starbound Spawn Creature Command Not Working? Fixes
- How to Use AI Coding Tools Without Losing Skills
- How to Read a Company’s Quarterly Results Preview
- Evolve Wraith Guide: How the Stealth Assassin Works
- How Foldable Phone Displays and Hinges Work