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

"git stash push" behaves weirdly when files is added in index and removed (and the other way around)

From
Piotr Siupa <piotrsiupa@gmail.com>
Date
Aug 21, 2024, 19:09 UTC
Message-ID
<CAPM0=yBx4LiL+5vhCGgm30ThtsN99-zWeeQ7qhtwRR-N-2DmHw@mail.gmail.com>

I'm writing a script that is based on "git stash" and I test it extensively, which leads to finding a lot of weird corner cases in the behavior of "git stash".

I've reported one bug already and now I've found two "inconsistencies" that I'm less sure about. (and that may be related to each other, which is why I write about them in the same email)

OK, the first one occurs when a file (that existed in the previous
commits) is removed in index but after that re-added without informing
Git about it. (So it's untracked.)
    git rm file
    echo content >file
    git stash push
The untracked file is pushed to the stash despite the flag
"--include-untracked" not being set. It even works like that with
"--no-include-untracked".
This looks like a bug for sure but I found a test "stash rm then
recreate" in "t/t3903-stash.sh", which test for this scenario, so
apparently it isn't for some reason? (Just in case, I'm asking for
confirmation that it's intended. Also, is there a way to disable this
behavior so my script won't trip over it?)
The second scenario is the exact opposite in the sense that a file
(that hasn't existed before) is added in the index and removed in the
workspace.
    echo content >file
    git add file
    rm file
    git stash push
After that, the information about the file deletion is lost. The stash
contains the file both in stash@{0} and in stash@{0}^1 and the
workspace is clean after the operation (the file is restored).
This, I'm quite sure, is a real bug and not an intended behavior
(although, I have a tiny doubt after finding the test for the other
situation).
I went ahead and tried to fix it on my own but I don't understand the
intricacies of Git's code well enough so I failed. I did write a test
case, though, which I'll send in the next email (because if I
understand it right, patches are supposed to not have any added text
in their email messages).
Piotr
Message 1 of 1 in “"git stash push" behaves weirdly when files is added in index and removed (and the other way around)”
  1. Piotr SiupaAug 21, 2024

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.