How to stash only one file when multiple files have been modified in Git?
To stash only one file when multiple files have been modified in Git, you can use the git stash push -- <file> command, which allows you to specify exactly which files to stash while leaving others unchanged. This modern Git syntax lets you select individual files, multiple files, or use patterns to precisely control what gets stashed without affecting your working directory’s other modified files.
Contents
- Basic Method for Stashing Individual Files
- Stashing Multiple Specific Files
- Using Patch Mode for Granular Control
- Adding Custom Messages to Stashes
- GUI Methods for Visual Selection
- Common Use Cases and Examples
- Troubleshooting and Best Practices
Basic Method for Stashing Individual Files
The simplest way to stash only specific files is using the git stash push command with the -- separator followed by the file paths. This syntax works in modern Git versions (2.13.2 and later) and is the most straightforward approach.
git stash push -- <path/to/file>
For example, if you have modified src/app.js, docs/README.md, and package.json but only want to stash the changes to src/app.js:
git stash push -- src/app.js
This command will stash only the changes to src/app.js, leaving the other modified files untouched in your working directory. The -- separator is important as it tells Git that what follows are file paths, not command options.
Note: The
--separator prevents conflicts if any of your filenames might otherwise be interpreted as command-line flags.
Stashing Multiple Specific Files
You can also stash multiple specific files at once by listing them after the -- separator. This is useful when you want to stash several files but not all modified files.
git stash push -- file1.txt file2.txt file3.txt
According to the GeeksforGeeks guide, you might have modified three files (file1.txt, file2.txt, and file3.txt) but only want to stash the changes in file1.txt and file2.txt. The command would be:
git stash push -- file1.txt file2.txt
The Stack Overflow documentation shows that this method also supports pattern matching with wildcards:
git stash push -- welcome.*ml
This would stash any files starting with “welcome.” and ending with “ml”.
Using Patch Mode for Granular Control
When you need more granular control over which changes to stash within a file, you can use the --patch (or -p) flag. This allows you to interactively select hunks of changes to stash.
git stash push --patch
As Baeldung on Ops demonstrates, this opens an interactive prompt:
$ git stash push --patch
diff --git a/trackedfile b/trackedfile
new file mode 100644
index 0000000..a1b2c3d
--- /dev/null
+++ b/trackedfile
@@ -0,0 +1 @@
+Sun Feb 22 10:02:10 AM EST 2024
Stash this hunk [y,n,q,a,d,e,?]?
You can then choose to:
y- stash this hunkn- don’t stash this hunkq- quit without stashinga- stash all hunks in this filed- don’t stash any hunks in this filee- manually edit the hunk?- show help
This method is particularly useful when you’ve made multiple changes to a single file and only want to stash some of them.
Adding Custom Messages to Stashes
To make your stashes easier to identify later, you can add custom messages using the -m flag. This is especially helpful when you have multiple stashes and need to distinguish between them.
git stash push -m "Descriptive message" -- <file>
For example, as shown in the phoenixNAP KB:
git stash push -m "Made edits to the readme file" readme.md
Or for multiple files:
git stash push -m "Stashing file1 and file2" -- file1.txt file2.txt
Custom messages help you quickly identify the purpose of each stash when you list them with git stash list.
GUI Methods for Visual Selection
If you prefer using a graphical interface, many Git GUI tools support visual selection of files to stash. For example, in VS Code:
- Hold the shift key and then select the files you want to stash
- Right-click and choose “stash changes” option
This visual approach can be more intuitive when working with many modified files, as you can see exactly what you’re selecting.
Common Use Cases and Examples
Working with Directory Structures
When working with organized projects, you might want to stash entire directories or specific file patterns:
# Stash all files in the js directory
git stash push js/
# Stash all JavaScript files in the src directory
git stash push src/**/*.js
Handling Different File Types
Different scenarios might require different approaches:
# Stash a single file
git stash push -- src/app.js
# Stash multiple files from different directories
git stash push -- src/app.js docs/README.md tests/utils.js
# Stash all modified files except one
git stash push -- all
git stash drop
git stash pop readme.md # Then restore the one you didn't want to stash
Temporary Branch Creation
Sometimes you want to stash changes temporarily to work on something else:
# Stash specific files before switching branches
git stash push -- feature.js components/Header.jsx
git checkout main
# Later, return to your work
git checkout feature-branch
git stash pop
Troubleshooting and Best Practices
Common Issues
1. “fatal: ambiguous argument ‘file’”
This occurs when Git interprets your filename as a command option. Always use -- to separate options from file paths:
# Wrong
git stash push file.txt
# Right
git stash push -- file.txt
2. Changes not showing up in stash
Ensure the files are not already staged. Use git status to check:
# If files are staged, unstage them first
git reset HEAD file.txt
git stash push -- file.txt
Best Practices
- Use descriptive messages when stashing multiple files or working with team members
- Check your status before stashing to confirm which files are modified vs. staged
- Consider using branch-specific stashes for complex workflows
- Regularly clean up old stashes with
git stash droporgit stash clear - Test your stash commands in a safe environment first when working with important changes
Conclusion
Stashing specific files in Git is a powerful technique that gives you precise control over your working directory state. The git stash push -- <file> syntax provides the most straightforward approach for modern Git versions, allowing you to stash individual or multiple files while leaving others untouched. For more granular control, the --patch mode lets you select specific changes within files. Adding custom messages with -m helps organize your stashes, and GUI tools offer visual alternatives for those who prefer mouse-based workflows. By mastering these techniques, you can maintain a clean working directory while preserving only the changes you need, making your Git workflow more efficient and organized.
Sources
- Git - git-stash Documentation
- How do I stash only one file out of multiple files that have changed? - Stack Overflow
- How to Stash a Specific File or Multiple Files in Git? - GeeksforGeeks
- How can I git stash a specific file? - Stack Overflow
- Stashing Selected Files and Changes in a Git Working Tree | Baeldung on Ops
- How can I git stash a specific file? - DesignGurus.io
- How to Git Stash Specific Files | phoenixNAP KB
- How to Git Stash Specific Files? - Tutorialspoint
- How to Stash Only One File out of Multiple Files that Have Changed in Git - W3Docs
- git stash specific files: How to stash a specific file or multiple files? - DEV Community