Today I learned how to stash specific files in Git instead of stashing all changes.
The Problem
Sometimes you’re working on multiple changes but only want to stash some of them. The standard git stash
command stashes all modified files, which isn’t always what you want.
The Solution
You can use the git stash push
command with specific file paths:
git stash push -m "My partial stash" path/to/file1.txt path/to/file2.js
This will only stash the changes to the specified files, leaving other modified files in your working directory.
Retrieving the Stash
You can retrieve the stash as usual:
git stash apply stash@{0}
Or pop it:
git stash pop stash@{0}
This technique is incredibly useful when you’re working on multiple features simultaneously and need to switch contexts quickly.