0
0
Gitdevops~5 mins

Stashing specific files in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to save only some changes in your project temporarily without committing them. Git stash lets you do this, and you can choose to stash just specific files instead of everything.
When you are working on multiple files but want to save changes only from one file to switch branches quickly.
When you want to test something else but keep some file changes safe without committing.
When you accidentally modified files but want to stash only a few before running a build or test.
When you want to share your work but keep some changes private temporarily.
When you want to clean your working directory but keep some changes saved for later.
Commands
This command saves changes only from file1.txt into a new stash with a message describing it.
Terminal
git stash push -m "stash only file1.txt" -- file1.txt
Expected OutputExpected
Saved working directory and index state WIP on main: abc1234 Commit message
-m - Adds a descriptive message to the stash for easy identification
Shows all saved stashes so you can see the one you just created.
Terminal
git stash list
Expected OutputExpected
stash@{0}: On main: stash only file1.txt
Displays the exact changes saved in the stash number 0, so you can verify what was stashed.
Terminal
git stash show -p stash@{0}
Expected OutputExpected
diff --git a/file1.txt b/file1.txt index e69de29..d95f3ad 100644 --- a/file1.txt +++ b/file1.txt @@ -0,0 +1 @@ +Example change in file1.txt
Applies the stashed changes back to your working directory and removes the stash from the list.
Terminal
git stash pop stash@{0}
Expected OutputExpected
On branch main Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: file1.txt Dropped stash@{0} (abc1234 Commit message)
Key Concept

If you remember nothing else from this pattern, remember: git stash push lets you save changes from specific files only, not the whole project.

Common Mistakes
Running 'git stash' without specifying files when you want to stash only some files.
This stashes all changes, not just the specific files you want, which can cause confusion or loss of control.
Use 'git stash push' followed by the file names to stash only those files.
Trying to stash files that are not modified or staged.
Git will not stash unmodified files, so the stash will be empty or not include those files.
Make sure the files you want to stash have changes before running the stash command.
Forgetting to specify the stash name or message, making it hard to identify later.
Without a message, it is difficult to know what changes are in each stash.
Always use the -m flag with a clear message describing the stashed files or purpose.
Summary
Use 'git stash push -m "message" -- file1.txt' to stash changes from specific files only.
Check your stashes with 'git stash list' and see details with 'git stash show -p'.
Apply stashed changes back with 'git stash pop' to continue working on them.