0
0
GitHow-ToBeginner · 3 min read

How to Stash Specific File in Git: Simple Commands

To stash a specific file in Git, use git stash push <file_path>. This command saves changes of only that file, leaving other changes unstashed.
📐

Syntax

The basic syntax to stash a specific file is:

  • git stash push <file_path>: Stashes changes only for the specified file.
  • git stash push -m "message" <file_path>: Adds a message to the stash for easier identification.

This command saves the changes of the given file and leaves other files untouched.

bash
git stash push <file_path>
git stash push -m "message" <file_path>
💻

Example

This example shows how to stash changes only for app.js while keeping other changes in the working directory.

bash
git stash push app.js

git stash list
Output
stash@{0}: WIP on main: <commit-hash> Stashed changes for app.js
⚠️

Common Pitfalls

One common mistake is using git stash without specifying files, which stashes all changes. Another is trying to stash untracked files without the --include-untracked option.

Also, specifying multiple files requires listing each file explicitly.

bash
## Wrong: stashes all changes
 git stash

## Right: stash only specific file
 git stash push app.js

## To stash untracked files too
 git stash push --include-untracked app.js
📊

Quick Reference

CommandDescription
git stash push Stash changes of a specific file
git stash push -m "message" Stash with a descriptive message
git stash push --include-untracked Stash including untracked files
git stash listShow all stashed changes
git stash popApply the latest stash and remove it from stash list

Key Takeaways

Use git stash push <file_path> to stash changes of a specific file only.
Add -m "message" to label your stash for easy identification.
Include untracked files with --include-untracked if needed.
Always check your stash list with git stash list before applying.
Avoid using plain git stash if you want to stash only certain files.