git/list[1] front-page[2] threads[3] people[4] search[5] about
 

git stash --include-untracked possible destructive behavior

From
Marc Strapetz <marc.strapetz@syntevo.com>
Date
Jun 8, 2017, 11:52 UTC
Message-ID
<2b695c73-9de2-5502-6d19-ba4ad0114978@syntevo.com>

One of our users has just discovered a destructive behavior of git stash --include-untracked: ignored files within non-ignored directories will not be stashed as untracked files (expected), but will still be removed from the working tree. The problem is reproducible by following command sequence:

   $ git init
   $ echo "1" > file.txt
   $ mkdir Data
   $ echo "1" > Data/ignored.txt
   $ echo "Data/*" > .gitignore
   $ git add .
   $ git commit -m "initial import"
   $ echo "2" > file.txt
   $ git stash save --include-untracked
   Saved working directory and index state WIP on master: 6ce5303 initial
   import
   $ git stash show stash@{1}
    file.txt | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)
   $ ls Data
   ls: cannot access 'Data': No such file or directory

As you can see, after saving the stash, stash@{1} doesn't contain Data/ignored.txt, but the entire Data directory is still gone from the working tree. As a consequence, applying the stash won't bring back Data/ignored.txt, too.

   $ git stash apply
   On branch master
   Changes not staged for commit:
     (use "git add <file>..." to update what will be committed)
     (use "git checkout -- <file>..." to discard changes in working
     directory)
           modified:   file.txt
   no changes added to commit (use "git add" and/or "git commit -a")
   $ ls Data
   ls: cannot access 'Data': No such file or directory

Note that when changing .gitignore to "Data/" instead of "Data/*", --include-untracked works as expected.

Tested with: git version 2.13.0.windows.1
-Marc
Next: Joel Teichroeb
Message 1 of 5 in “git stash --include-untracked possible destructive behavior”
  1. Marc StrapetzJun 8, 2017
  2. Joel TeichroebJun 8, 2017
  3. Brandon WilliamsJun 8, 2017
  4. Joel TeichroebJun 8, 2017
  5. Marc StrapetzJun 9, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.