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

Stashing just index..working-copy rather than HEAD..working-copy?

From
TCTim Chase <git@tim.thechases.com>
Date
Apr 24, 2024, 13:44 UTC
Message-ID
<ZikMqXeDnOqK_wlq@thechases.com>

A while back[1] I'd encountered a situation and sparred my way around it, but was hoping there was a better solution.

I'd done a
  $ git add -p

to selectively add things that I wanted in the next commit. So I wanted to stash the changes that appeared in

  $ git diff
and test just the changes I was about to commit so I did a
  $ git stash

However, that reset my index and stashed everything HEAD..working-copy. Okay, my fault. There's a --keep-index that isn't default, so I carefully re-staged my commit with another

  $ git add -p
and did
  $ git stash --keep-index
to keep the index.  Great.  My index was still good.  But when I went to
  $ git stash pop

as described in `git help stash` under the "Testing partial commits" it generated conflicts because it had still stashed HEAD..working-copy (as confirmed with a `git stash show -p`) rather than index..working-copy and some of those popped changes were already in the working-copy/index.

To work around it, I re-staged my index yet again:
  $ git add -p
and then did
  $ git diff > temp.diff
  $ git reset --staged

did my testing, and then re-applied the temp.diff patch to the working-copy to get back to where I'd been. Conflict-free as expected.

As a slight improvement, /u/splettnet suggested actually committing a dummy-commit:

  $ git add -p
  $ git commit --allow-empty-message
  $ git stash
at which point I could build/run/test and then resetting to uncommit:
  $ git stash pop
  $ git reset --soft HEAD~1

which I've been using since. However, I was wondering if there was a better way to instruct git-stash to stash index..working-copy instead of HEAD..working-copy (and leave the index alone in the process) in the first place.

Thanks,
-tkc

[1] https://www.reddit.com/r/git/comments/vchu83/stashing_only_unstaged_changes/

Next: Chris Torek
Message 1 of 4 in “Stashing just index..working-copy rather than HEAD..working-copy?”
  1. Tim ChaseApr 24, 2024
  2. Chris TorekApr 24, 2024
  3. Junio C HamanoApr 24, 2024
  4. Tim ChaseApr 27, 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.