DEV Community

Cover image for Day 18/30 - git stash --patch – Stash only specific changes interactively
Ruqaiya Beguwala
Ruqaiya Beguwala

Posted on • Originally published at Medium

Day 18/30 - git stash --patch – Stash only specific changes interactively

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
Enter fullscreen mode Exit fullscreen mode

Interactive Stashing

git stash --patch
# or shorthand:
git stash -p
Enter fullscreen mode Exit fullscreen mode

Git will present each change (hunk) and prompt:

Stash this hunk [y,n,q,a,d,e,?]?
Enter fullscreen mode Exit fullscreen mode

Basic options:

  • y - Stash this change
  • n - Skip this change
  • q - Quit interactive mode

Real-World Example

Imagine you're working on:

  1. A bug fix in user-service.js
  2. An experimental feature in auth.js
  3. Some console logs in utils.js

You only want to stash the experimental feature:

git stash -p
Enter fullscreen mode Exit fullscreen mode

For each file:

  • n to user-service.js changes (keep working)
  • y to auth.js changes (stash experiment)
  • n to utils.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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Running git stash -p

When you run:

git stash -p
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Splitting the Hunk

  1. Git asks:
   Stash this hunk [y,n,q,a,d,s,e,?]?
Enter fullscreen mode Exit fullscreen mode
  1. You press s (split).
  2. 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}!"
Enter fullscreen mode Exit fullscreen mode
  • You can now choose y (stash) or n (skip).

Second Part (add function change)

   @@ -3,3 +3,3 @@
    def add(a, b):
-   return a + b
+   return a * b
Enter fullscreen mode Exit fullscreen mode
  • You can decide separately (y or n).

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.
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Ultimate precision in separating changes

3. Stash Specific File Types

git stash -p -- '*.js'  # Only JavaScript files
Enter fullscreen mode Exit fullscreen mode
  • Works with any git path specifier

4. Named Stashes

git stash -p save "navbar-refactor"
Enter fullscreen mode Exit fullscreen mode
  • Create descriptive stash names
  • Find later with git stash list

5. Stash Untracked Files

git stash -p --include-untracked
Enter fullscreen mode Exit fullscreen mode
  • Handles new files in your working directory

6. Partial Stash Undo

git stash show -p stash@{0} | git apply -R
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Handle merge conflicts in small chunks

8. Create Patch Files

git stash show -p stash@{1} > fix.patch
Enter fullscreen mode Exit fullscreen mode
  • 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)