# Bug report for git stash push.

1 messages from 2022-03-13 to 2022-03-13. Participants: contact@your-diary.io.
Thread: https://gitlist.dev/t/57550

## contact@your-diary.io, 2022-03-13 12:20

Subject: Bug report for git stash push.
Message-ID: <aa8c4afddad08838584a5f9ec5c8e7ab@your-diary.io>
URL: https://gitlist.dev/e/aa8c4afddad08838584a5f9ec5c8e7ab%40your-diary.io

```
Dear, Git team

Hi.
I, ynn, am contacting you to report a counter-intuitive behavior of `git 
stash push -- <file(s)>`.
I believe this is a (at least documentation) bug.

Prior to this email, I've discussed this problem on Stack Overflow:
https://stackoverflow.com/questions/71411369/git-stash-push-file-doesnt-only-save-the-specified-file

---------------------------------------------------

# tl;dr

`git stash push -- <file(s)>` is expected to save only the specified 
`<file(s)>` and let the other files intact.
However, it doesn't.

---------------------------------------------------

# Expected Behavior

According to `man git-stash`, `git stash push` is explained as below.

ˋˋˋ
push [-p|--patch] [-S|--staged] [-k|--[no-]keep-index] 
[-u|--include-untracked] [-a|--all]
[-q|--quiet] [-m|--message <message>] [--pathspec-from-file=<file> 
[--pathspec-file-nul]] [--]
[<pathspec>...]
     Save your local modifications to a new stash entry and roll them 
back to HEAD (in the working
     tree and in the index). The <message> part is optional and gives the 
description along with
     the stashed state.
ˋˋˋ

where `<pathspec>` is defined as

ˋˋˋ
<pathspec>...

     This option is only valid for push command.

     The new stash entry records the modified states only for the files 
that match the pathspec.
     The index entries and working tree files are then rolled back to the 
state in HEAD only for
     these files, too, leaving files that do not match the pathspec 
intact.
ˋˋˋ

This definition of `<pathspec>` says two things:

(A) The new stash entry records the modified states only for the files 
that match the pathspec.

(B) The index entries and working tree files are then rolled back to the 
state in HEAD only for these files, too, leaving files that do not match 
the pathspec intact.

Let's confirm these two in the next section.

---------------------------------------------------

# Actual Behavior

## Preparations

ˋˋˋ
$ git init test1
$ cd test1
$ echo aaa > a.txt
$ echo bbb > b.txt
$ git add .
$ git commit -m 'H'
$ cd ..
$ cp -r test1 test2
ˋˋˋ

## Pattern1

When files are not staged, both (A) and (B) are satisfied.

ˋˋˋ
$ cd test1

$ echo aaa >> a.txt

$ echo bbb >> b.txt

$ git status
On branch master
Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git restore <file>..." to discard changes in working directory)
     modified:   a.txt
     modified:   b.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash push -- a.txt
Saved working directory and index state WIP on master: 9806f0c H

$ git status # (B) is satisfied
On branch master
Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git restore <file>..." to discard changes in working directory)
     modified:   b.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash show # (A) is satisfied
  a.txt | 1 +
  1 file changed, 1 insertion(+)

$ cd ..
ˋˋˋ

## Pattern2

When files are staged, only (B) is satisfied.

ˋˋˋ
$ cd test2

$ echo aaa >> a.txt

$ echo bbb >> b.txt

$ git add . # stages them

$ git status
On branch master
Changes to be committed:
   (use "git restore --staged <file>..." to unstage)
     modified:   a.txt
     modified:   b.txt

$ git stash push -- a.txt
Saved working directory and index state WIP on master: 9806f0c H

$ git status # (B) is satisfied
On branch master
Changes to be committed:
   (use "git restore --staged <file>..." to unstage)
     modified:   b.txt

$ git stash show # (A) is NOT satisfied.
  a.txt | 1 +
  b.txt | 1 +
  2 files changed, 2 insertions(+)
ˋˋˋ

Let's cite the explanation (A) again.

(A) The new stash entry records the modified states only for the files 
that match the pathspec.

However, in the example above, the modified states of `a.txt` AND 
`b.txt` are saved in the newly created stash entry.

---------------------------------------------------

# Conclusion

- Though I don't tell if the behavior is what the author of `git stash` 
intended to be, it seems counter-intuitive. Whilst I specify the 
pathspec `-- a.txt`, not only `a.txt` but also `b.txt` are saved.

- The behavior seems to contradict with the documentation, meaning at 
least the documentation should be fixed.

Thank you.

---------------------------------------------------

# Environments

Information from `git bugreport`:

ˋˋˋ
[System Info]
git version:
git version 2.35.1
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 5.16.11-arch1-1 #1 SMP PREEMPT Thu, 24 Feb 2022 02:18:20 
+0000 x86_64
compiler info: gnuc: 11.1
libc info: glibc: 2.35
$SHELL (typically, interactive shell): <unset>

[Enabled Hooks]
ˋˋˋ

---------------------------------------------------

```
