
A successful git stash message does not mean every file in your folder was saved. If a new script is still there after stashing, Git may be doing exactly what its default rules say. The useful question is not whether the command worked, but which category that file belongs to.
I separate tracked changes, untracked files and ignored files before choosing a stash option. That avoids treating a clean-looking status screen as proof that every local file has a recoverable copy.
1. Inspect the boundaries before moving anything
From your repository root, run this read-only check:
git status --short --untracked-files=all --ignored
In this output, ?? identifies an untracked path and !! identifies an ignored path. A tracked file can have staged or unstaged changes, shown in the status columns. An ignored file is often a cache or local configuration file, but its name alone does not prove that it is disposable.
Default git stash push records tracked changes from the working tree and index. A new file already staged with git add is in the index, so do not assume that every newly created file is still untracked.
2. Choose the scope deliberately
git stash push: saves tracked changes; leaves untracked and ignored files alone.git stash push -u: also saves untracked files, then removes those saved untracked files from the working tree. Ignored files remain.git stash push -a: also includes ignored files. This is broader, not automatically safer.
For example, after reviewing the status output and making any separate backup you need, you might save tracked changes and untracked files with a recognizable message:
git stash push -u -m "before parser experiment"
Do not add -a just to make the folder look empty. Ignored paths can contain large build outputs, dependency directories or private local settings. Including them stores that content in the local Git repository and removes the saved files from the working tree. Stashing is not encryption or a substitute for a separate backup.
3. Verify the entry, including new files
Check the new entry and the working tree separately:
git stash list
git stash show --include-untracked --stat 'stash@{0}'
git status --short --untracked-files=all --ignored The show command above explicitly includes untracked content in the summary. A default stash summary can omit it. Match the entry's message before trusting stash@{0}: it means the most recent entry, and that position changes when you create or remove stashes.
If Git reports that there were no local changes to save, do not assume it created a new entry. An older stash may still occupy position zero. Review the output and list before applying anything.
4. Restore without immediately deleting the stash
When you are ready, inspect the destination working tree first. After identifying the right entry, I prefer applying it while keeping the stash available:
git stash apply 'stash@{0}' Unlike a successful pop, apply keeps the entry in the stash list. Check the restored files and run the relevant tests before deliberately dropping that specific entry. Applying can conflict or fail, including when a restored untracked path would collide with a file that now exists. Read the result instead of repeating the command blindly.
If you need the original staged state too, investigate apply --index; plain apply is not a promise to recreate that staging layout. This guide covers ordinary files, not a complete backup strategy for submodules or nested repositories.
What I checked
In isolated temporary repositories with Git 2.34.1, I used one modified tracked file, one untracked file and one ignored file. Default stash left both extra files, -u left only the ignored file, and -a saved both. Applying each entry restored the saved content and kept the entry. These were synthetic local checks, not tests against a production project.
Sources: Git stash manual and Git status manual.
Comments
Post a Comment