Introduction
git stash --patch
(or -p
) is Git's interactive stashing mode that lets you:
- Selectively stash specific changes
- Split large changes into smaller logical units
- Test code without certain modifications
- Maintain cleaner working directories
Unlike regular git stash
which saves everything, -p
gives you control over what to stash and what not to stash.
Basic Usage
Stashing All Changes
git stash
Interactive Stashing
git stash --patch
# or shorthand:
git stash -p
Git will present each change (hunk) and prompt:
Stash this hunk [y,n,q,a,d,e,?]?
Basic options:
-
y
- Stash this change -
n
- Skip this change -
q
- Quit interactive mode
Real-World Example
Imagine you're working on:
- A bug fix in
user-service.js
- An experimental feature in
auth.js
- Some console logs in
utils.js
You only want to stash the experimental feature:
git stash -p
For each file:
-
n
touser-service.js
changes (keep working) -
y
toauth.js
changes (stash experiment) -
n
toutils.js
logs (keep debugging)
Git Stash --patch: Splitting Hunks with s
When Git shows you a "hunk" (a block of changes), it sometimes groups multiple unrelated changes together. Instead of stashing or skipping the entire hunk, you can split it into smaller parts and decide what to do with each.
Let’s say you have a file example.py
with the following changes:
Original File (example.py
)
def greet(name):
return f"Hello, {name}!"
def add(a, b):
return a + b
Modified File (example.py
)
def greet(name):
return f"Hi, {name}!" # Changed "Hello" to "Hi"
def add(a, b):
return a * b # Changed "+" to "*" (intentional bug for testing)
Running git stash -p
When you run:
git stash -p
Git shows this hunk:
diff --git a/example.py b/example.py
index abc123..def456 100644
--- a/example.py
+++ b/example.py
@@ -1,5 +1,5 @@
def greet(name):
- return f"Hello, {name}!"
+ return f"Hi, {name}!"
def add(a, b):
- return a + b
+ return a * b
Splitting the Hunk
- Git asks:
Stash this hunk [y,n,q,a,d,s,e,?]?
- You press
s
(split). - Git now breaks the hunk into two smaller parts:
First Part (greet function change)
@@ -1,3 +1,3 @@
def greet(name):
- return f"Hello, {name}!"
+ return f"Hi, {name}!"
- You can now choose
y
(stash) orn
(skip).
Second Part (add function change)
@@ -3,3 +3,3 @@
def add(a, b):
- return a + b
+ return a * b
- You can decide separately (
y
orn
).
Possible Outcome
- If you stash only the
greet
change (y
for first part,n
for second):- The
greet
modification goes into the stash. - The
add
modification stays in your working directory.
- The
- If you stash both, they go into the stash as separate changes.
Advanced Tips & Tricks
1. Stash While Keeping Staged Changes
git stash -p --keep-index
- Keeps your staged files intact
- Perfect for testing commits without unstaged changes
2. Visual Staging Combo
git add -p # Stage changes interactively
git stash -k # Stash everything else
- Ultimate precision in separating changes
3. Stash Specific File Types
git stash -p -- '*.js' # Only JavaScript files
- Works with any git path specifier
4. Named Stashes
git stash -p save "navbar-refactor"
- Create descriptive stash names
- Find later with
git stash list
5. Stash Untracked Files
git stash -p --include-untracked
- Handles new files in your working directory
6. Partial Stash Undo
git stash show -p stash@{0} | git apply -R
- Reverses a stash without popping it
- Safer alternative to
git stash pop
7. Stash Conflict Resolution
git stash -p
# Resolve conflicts
git stash apply --index
- Handle merge conflicts in small chunks
8. Create Patch Files
git stash show -p stash@{1} > fix.patch
- Share stashes between machines/repos
Conclusion
git stash -p
is your secret weapon for cleaner, more controlled Git workflows. Whether you're:
- Saving partial changes without messy commits
- Debugging by temporarily removing code
- Organising work into logical chunks
- This interactive tool gives you precision where regular git stash falls short. Start with the basics, experiment with splitting hunks (s), and gradually incorporate the advanced tricks.
Up Next in the Series: git clean -fd
– Remove untracked files & directories
Daily advance GIT tips in your inbox---worth starting? Respond to my poll here🚀
For more useful and innovative tips and tricks, Let's connect on Medium
Top comments (0)