Git Stash Specific Files: How To
Yes, you can stash specific files in Git. Use git stash push -- <file> (or git stash push -m "message" -- <file>) to stash only the changes in the listed files, leaving all other modifications untouched. This is a core Git feature that lets you temporarily set aside work on a few files without hiding everything else in your working tree.
Background: What Git Stash Does
Git stash is a built-in mechanism that saves your uncommitted changes (both staged and unstaged) into a temporary stack, then reverts your working directory to match the last commit (HEAD). The main use case is when you need to switch branches, pull updates, or start a different task, but you aren’t ready to commit your current work. The stash lets you save that work and restore it later.
By default, git stash stashes everything that Git tracks and has been modified. That includes new files you’ve added with git add, but not untracked files unless you pass -u (or --include-untracked). This all-or-nothing behavior is exactly what trips people up: sometimes you only want to put aside changes in one or two files, not the whole working tree.
How to Stash Specific Files
The command to stash specific files is straightforward:
git stash push -- path/to/file1 path/to/file2
You can list as many paths as you want, separated by spaces. The -- separator tells Git that everything after it is a file path, not an option. This is important if your filename starts with a dash (rare but possible).
You can also add a message to help you remember what’s in the stash:
git stash push -m "WIP: fix login bug" -- src/auth.js tests/auth.test.js
If you want to include only staged changes (those you’ve already run git add on) for a specific file, you can use git stash push --staged -- <file> (Git 2.35 and later). This stashes only the staged portion of that file, leaving unstaged changes in the working tree.
Choosing Which Files to Stash
You can specify files by their relative path from the repository root. Common ways to reference them:
- Exact path:
src/App.js - Directory:
src/(stashes all changes in that directory) - Glob pattern (if your shell expands it):
*.cssorsrc/**/*.js
If you’re not sure which files are modified, run git status first. It shows the list of changed files. You can copy the exact paths from there.
Note that Git doesn’t allow you to stash a file that has no changes. If you try to stash an unmodified file, Git will simply ignore it and stash nothing (or error if none of the listed files have changes).
Common Problems and Solutions
Here are the typical issues people run into when stashing specific files, and how to fix them:
- Nothing gets stashed: If you get “No local changes to save,” double-check that the file paths are correct and that the files actually have modifications. Use
git statusto confirm. - Untracked files not stashed: By default, stash only saves tracked files. If you have a new file that hasn’t been added, use
git stash push -u -- <file>to include untracked files. - Stashed too much: If you accidentally stashed files you didn’t mean to, you can restore them with
git stash pop(orgit stash apply) and then re-stash the correct set. The stash is not lost until you drop it. - Merge conflicts when popping: If you stashed changes and then made other edits to the same files, popping the stash may cause conflicts. Resolve them like any merge conflict, then commit or continue.
Stash vs. Commit vs. Branch
Stashing specific files is a temporary workaround. If you need to keep the changes for a long time, consider a more permanent approach:
- Commit: Make a commit on a new branch with just those files. This gives you a full history entry and is easier to share or merge.
- Branch: Create a new branch, commit the specific files there, and switch back to your original branch. This is cleaner for long-running work.
- Stash: Best for short interruptions (a few minutes or hours). Stashes are easy to lose track of, and they don’t have commit messages that show in history.
If you want to move changes between branches without committing, stash is a good choice. But if you need to collaborate or keep a record, a commit on a branch is safer.
Safety and Best Practices
Stashing is generally safe because it doesn’t delete your changes; it saves them in a stash entry. However, there are a few risks to be aware of:
- Stash is not a backup: If you drop a stash (with
git stash drop) or if your repository is deleted, the changes are gone. For important work, commit to a branch instead. - Stash applies to the whole working tree: When you
git stash pop, it tries to apply all stashed changes, not just the files you originally stashed. If you stashed only specific files, only those files are affected, but if you stashed everything, popping will restore everything. - Check your stash list: Use
git stash listto see all your stashes. Each entry has an index likestash@{0}. You can apply a specific one withgit stash apply stash@{2}. - Clean up: After you’ve applied a stash and confirmed it works, drop it with
git stash dropto keep your stash list tidy.
Alternatives to Stashing Specific Files
If stashing specific files doesn’t fit your workflow, here are other ways to temporarily set aside changes:
- Git worktree: Create a separate working directory for a different branch. You can leave your changes in one worktree and switch to another without stashing.
- Patch files: Use
git diff > changes.patchto save your changes to a file, thengit checkout -- .to discard them. Later, apply the patch withgit apply changes.patch. This works well for sharing changes. - Interactive staging: If you only want to commit some files, use
git add -pto stage hunks selectively, then commit. This doesn’t stash the rest; it just leaves them uncommitted.
Frequently Asked Questions
How do I stash only unstaged changes?
By default, git stash push stashes both staged and unstaged changes for the files you specify. To stash only unstaged changes, you can use git stash push --keep-index -- <file>. This stashes the unstaged changes while keeping the staged ones in your working tree. Alternatively, stage the changes you want to keep, then use git stash push --staged (Git 2.35+) to stash only the staged part, leaving unstaged changes untouched.
Can I stash a file that is untracked?
Yes, but you need to use the -u or --include-untracked flag: git stash push -u -- <file>. Without that flag, Git ignores untracked files entirely. Note that this will also stash any other untracked files in the repository unless you specify paths, which is why it’s safer to list the exact file.
How do I see what’s in a stash before applying it?
Use git stash show -p stash@{0} to see the full diff of the stash. Replace stash@{0} with the appropriate index from git stash list. This shows exactly which files and changes are stored, so you can decide if you want to apply it.
What’s the difference between git stash pop and git stash apply?
git stash pop applies the stash and then removes it from the stash list. git stash apply applies the stash but keeps it in the list. Use pop when you’re sure you want to restore and discard the stash; use apply if you might want to apply the same changes to multiple branches or if you want to keep it for later.
Can I stash specific files on a different branch?
Stashes are global to the repository, not tied to a branch. You can stash changes on one branch, switch to another, and then pop the stash there. However, if the file paths differ between branches (e.g., files were renamed), Git may have trouble applying the stash cleanly. In that case, you might need to manually resolve conflicts.
Does git stash work with binary files?
Yes, Git stash handles binary files just like text files. It stores the complete file content. There’s no issue with stashing binary files, but be aware that the stash diff might be large. When you pop, Git will restore the binary file exactly as it was.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.