NeuroAgent

How to Stash Only One File in Git: Complete Guide

Learn how to stash only specific files in Git when multiple files are modified. Complete guide with commands, examples, and best practices for precise version control.

Question

How to stash only one file when multiple files have been modified in Git?

NeuroAgent

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

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.

bash
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:

bash
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.

bash
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:

bash
git stash push -- file1.txt file2.txt

The Stack Overflow documentation shows that this method also supports pattern matching with wildcards:

bash
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.

bash
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 hunk
  • n - don’t stash this hunk
  • q - quit without stashing
  • a - stash all hunks in this file
  • d - don’t stash any hunks in this file
  • e - 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.

bash
git stash push -m "Descriptive message" -- <file>

For example, as shown in the phoenixNAP KB:

bash
git stash push -m "Made edits to the readme file" readme.md

Or for multiple files:

bash
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:

bash
# 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:

bash
# 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:

bash
# 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:

bash
# 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:

bash
# If files are staged, unstage them first
git reset HEAD file.txt
git stash push -- file.txt

Best Practices

  1. Use descriptive messages when stashing multiple files or working with team members
  2. Check your status before stashing to confirm which files are modified vs. staged
  3. Consider using branch-specific stashes for complex workflows
  4. Regularly clean up old stashes with git stash drop or git stash clear
  5. 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

  1. Git - git-stash Documentation
  2. How do I stash only one file out of multiple files that have changed? - Stack Overflow
  3. How to Stash a Specific File or Multiple Files in Git? - GeeksforGeeks
  4. How can I git stash a specific file? - Stack Overflow
  5. Stashing Selected Files and Changes in a Git Working Tree | Baeldung on Ops
  6. How can I git stash a specific file? - DesignGurus.io
  7. How to Git Stash Specific Files | phoenixNAP KB
  8. How to Git Stash Specific Files? - Tutorialspoint
  9. How to Stash Only One File out of Multiple Files that Have Changed in Git - W3Docs
  10. git stash specific files: How to stash a specific file or multiple files? - DEV Community