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

Re: stashing only unstaged changes?

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jun 24, 2022, 13:09 UTC
Message-ID
<220624.86tu8ai4mr.gmgdl@evledraar.gmail.com>
In-Reply-To
<20220621142618.239b02cd@bigbox.attlocal.net>
On Tue, Jun 21 2022, Tim Chase wrote:
Show 30 quoted lines
> I recently had composed a commit with some `git add -p` leaving some
> portions unstaged. I wanted to stash the unstaged changes to make
> sure that the staged code ran as expected, so I did  a `git stash`
> only to find that it unstaged my staged changes and stashed
> *everything*.
>
> Using `git stash --saved` does the opposite of what I want (stashing
> the index, not the difference between the index and the working-copy)
>
> So I carefully re-`git add -p`'ed everything and tried `git stash
> --keep-index` which sounded promising (my index remained the same),
> but popping my stash ended up causing conflicts because it had
> stashed the diff of HEAD..working-copy, not INDEX..working-copy.  A
> `git stash show -p` confirmed that the stash included things that I
> had already staged.
>
> So I carefully re-`git add -p`ed everything yet again, but then got
> stuck trying to convince `stash` to save a snapshot of only the diff
> in my working directory. To work around it, I did a `git diff >
> temp.patch` to obtain the stuff I'd wanted to stash, a `git reset
> --staged` to clear out those changes, ran my code to verify
> (eventually committing it), and then applied the `temp.patch` back on
> top of my changes.  It worked, but felt convoluted.
>
> I did see the `git stash -p` option, to manually choose the inverse
> bits, but for what I was doing, it was more sensible to `git add -p`
> and try to stash the rest.
>
> So is there some option I've missed to tell `git stash` to stash only
> the delta between the uncommitted-index and the working-copy?
Is what you want equivalent to:
    # save the "git add -p"'d chunks
    git stash push --staged
    # save the "uncommitted"
    git stash push
    # pop the previously staged
    git stash pop --index stash@{1}
?

I.e. this (ab)uses the stash itself to juggle the two around. I don't think there's a way to do this in one step, but I'm not very familiar with git-stash.

If that is what you want (and we don't have a way to do it) perhaps we should have a a:

    git stash push --unstaged

Which could start out as an alias for the above sequence, with e.g. an optional "--include-untracked" being passed to the second "git stash push" command above.

I also found this past thread (CC'd the author, in case it helps), which seems to be asking the same question: https://lore.kernel.org/git/CAC4jX8GEg5=9BPepYLntGRG7n_84ju7rTSYO82SQyuiiff0UcQ@mail.gmail.com/

Previous: Tim ChaseNext: Erik Cervin Edin
Message 7 of 10 in “stashing only unstaged changes?”
  1. Tim ChaseJun 21, 2022
  2. RetoJun 24, 2022
  3. Tim ChaseJun 24, 2022
  4. Konstantin KhomoutovJun 24, 2022
  5. Erik Cervin EdinJun 24, 2022
  6. Tim ChaseJun 24, 2022
  7. Ævar Arnfjörð BjarmasonJun 24, 2022
  8. Erik Cervin EdinJun 24, 2022
  9. Ævar Arnfjörð BjarmasonJun 24, 2022
  10. Junio C HamanoJun 24, 2022

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.