Git: how to use stash -p to stash specific files?

Use

git stash --patch

git will then show a dialog like the following, for every hunk in your possible commit:

diff --git files over files
index e69de29..ac4f3b3 100644
--- a/file.txt
+++ b/file.txt
@@ -0,0 +1 @@
+you did awesome stuff!
Stash this hunk [y,n,q,a,d,/,e,?]?

A hunk is a coherent diff of lines as git-diff produces it. To select a single file you'll have to decline adding hunks as long as you reach that file, then you might add all hunks from that file.

You're also able to choose a single hunk by answering the question with yes. If the hunk seems to be too big, you even might split it. It is also possible to edit the current hunk.


Using the --patch-option is possible on different git commands (f.e. stash, commit and add).

This is the detailed explanation of the --patch-function, which i grabbed from the developers documentation:

This lets you choose one path out of a 'status' like selection.
After choosing the path, it presents the diff between the index
and the working tree file and asks you if you want to stage
the change of each hunk.  You can select one of the following
options and type return:

   y - stage this hunk
   n - do not stage this hunk
   q - quit; do not stage this hunk or any of the remaining ones
   a - stage this hunk and all later hunks in the file
   d - do not stage this hunk or any of the later hunks in the file
   g - select a hunk to go to
   / - search for a hunk matching the given regex
   j - leave this hunk undecided, see next undecided hunk
   J - leave this hunk undecided, see next hunk
   k - leave this hunk undecided, see previous undecided hunk
   K - leave this hunk undecided, see previous hunk
   s - split the current hunk into smaller hunks
   e - manually edit the current hunk
   ? - print help

If you're interested in a more comfortable way to handle this task, you may also take a look on git gui tools like LazyGit. I use this one to split up my daily work into atomic commits.


In the most recent versions of Git (> v2.13) you can stage the specific files you want to stash using

git stash push -- <pathspec>

For example, if you stage the specific files you want to stash you can use this command to specifically stash only those file(s):

git stash push -- example/file/path/and/file.html

You can save yourself some typing by using a globbing pattern to stash the specific files in the path

git stash push -- **/path/**

You'll want to stage only the files you want to stash since any other staged files will be stashed with the specific files, but also remain staged after stashing so if you git stash pop you would get conflicts.


Use git stash -p without arguments. You will then be able to interactively select changes that you want to stash:

diff --git a/test b/test
index acbd8ae..662d47a 100644
--- a/test
+++ b/test
@@ -10,6 +10,7 @@ test
(...)
+    test
(...)
Stash this hunk [y,n,q,a,d,/,e,?]?

Unfortunately it's not possible to select files, only hunks.

Tags:

Git

Git Stash