From: Petr Baudis <>

"stash save" is about saving the local change to the working tree,
but also about restoring the state of the last commit to the working
tree.  When a local change is to turn a non-directory to a directory,
in order to restore the non-directory, everything in the directory
needs to be removed.

Which is fine when running "git stash save --include-untracked",
but without that option, untracked, newly created files in the
directory will have to be discarded, if the state you are restoring
to has a non-directory at the same path as the directory.

Introduce a safety valve to fail the operation in such case, using
the "ls-files --killed" which was designed for this exact purpose.

The "stash save" is stopped when untracked files need to be
discarded because their leading path ceased to be a directory, and
the user is required to pass --force to really have the data

Signed-off-by: Petr Baudis <>
Signed-off-by: Junio C Hamano <>

 * And this is the reverted patch ported on top of the "ls-files -k"
   miniseries I sent earlier.  The updates to the test in t3903
   compared to the original illustrates that the check implemented
   in the original did not protect once a path that was turned into
   a directory from a file gets added to the index, which this round
   also fixes by running "ls-files -k" against the state in the HEAD.

 Documentation/git-stash.txt | 11 +++++++++--                | 20 ++++++++++++++++++++
 t/            | 22 ++++++++++++++++++++++
 3 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index 711ffe1..61fadc5 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -14,7 +14,7 @@ SYNOPSIS
 'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
 'git stash' branch <branchname> [<stash>]
 'git stash' [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
-            [-u|--include-untracked] [-a|--all] [<message>]]
+            [-u|--include-untracked] [-a|--all] [-f|--force] [<message>]]
 'git stash' clear
 'git stash' create
@@ -43,7 +43,7 @@ is also possible).
-save [-p|--patch] [--[no-]keep-index] [-u|--include-untracked] [-a|--all] 
[-q|--quiet] [<message>]::
+save [-p|--patch] [--[no-]keep-index] [-u|--include-untracked] [-a|--all] 
[-q|--quiet] [-f|--force] [<message>]::
        Save your local modifications to a new 'stash', and run `git reset
        --hard` to revert them.  The <message> part is optional and gives
@@ -70,6 +70,13 @@ linkgit:git-add[1] to learn how to operate the `--patch` 
 The `--patch` option implies `--keep-index`.  You can use
 `--no-keep-index` to override this.
+In some cases, saving a stash could mean irretrievably removing some
+data - if a directory with untracked files replaces a tracked file of
+the same name, the new untracked files are not saved (except in case
+of `--include-untracked`) but the original tracked file shall be restored.
+By default, `stash save` will abort in such a case; `--force` will allow
+it to remove the untracked files.
 list [<options>]::
diff --git a/ b/
index bbefdf6..2d539f3 100755
--- a/
+++ b/
@@ -156,10 +156,19 @@ create_stash () {
        die "$(gettext "Cannot record working tree state")"
+# This helper MUST be run inside a subshell.
+list_killed_files () {
+       GIT_INDEX_FILE=$TMP-ls-files-k &&
+       export GIT_INDEX_FILE &&
+       git read-tree HEAD &&
+       git ls-files --killed
 save_stash () {
+       force=
        while test $# != 0
                case "$1" in
@@ -180,6 +189,9 @@ save_stash () {
+               -f|--force)
+                       force=t
+                       ;;
@@ -223,6 +235,14 @@ save_stash () {
                say "$(gettext "No local changes to save")"
                exit 0
+       if test -z "$untracked$force" &&
+          test -n "$(list_killed_files | head -n 1)"
+       then
+               say "$(gettext "The following untracked files would NOT be 
saved but need to be removed by stash save:")"
+               test -n "$GIT_QUIET" || (list_killed_files | sed 's/^/\t/')
+               say "$(gettext "Aborting. Consider using either the --force or 
--include-untracked option.")" >&2
+               exit 1
+       fi
        test -f "$GIT_DIR/logs/$ref_stash" ||
                clear_stash || die "$(gettext "Cannot initialize stash")"
diff --git a/t/ b/t/
index 5dfbda7..08ce23b 100755
--- a/t/
+++ b/t/
@@ -637,4 +637,26 @@ test_expect_success 'stash where working directory 
contains "HEAD" file' '
        test_cmp output expect
+test_expect_success 'stash a change to turn a non-directory to a directory' '
+       git reset --hard &&
+       >testfile &&
+       git add testfile &&
+       git commit -m "add testfile as a regular file" &&
+       rm testfile &&
+       mkdir testfile &&
+       >testfile/file &&
+       test_must_fail git stash save "recover regular file" &&
+       test -f testfile/file &&
+       git add testfile/file &&
+       test_must_fail git stash save "recover regular file after adding" &&
+       test -f testfile/file
+test_expect_success 'stash a change to turn a non-directory to a directory 
(forced)' '
+       git stash save --force "recover regular file (forced)" &&
+       ! test -f testfile/file &&
+       test -f testfile

To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to
More majordomo info at

Reply via email to