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

Re: stashing only unstaged changes?

From
KKKonstantin Khomoutov <kostix@bswap.ru>
Date
Jun 24, 2022, 09:55 UTC
Message-ID
<20220624095558.hegsfj4e4nbqyooo@carbon>
In-Reply-To
<20220621142618.239b02cd@bigbox.attlocal.net>
On Tue, Jun 21, 2022 at 02:26:18PM -0500, Tim Chase wrote:
Show 5 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*.

I tend to rely on reflogs for this. Basically, I roll like this:

 1. Stage the necessary changes, commit.
 
 2. Stage the remaining changes - what you've left unstaged in your case, -
    commit.
 
 3. Go to the commit I need to test; in this simple case that'd be
      $ git checkout HEAD~
    Then test the changes.
 4. Go back to the previous state using the HEAD's reflog:
     $ git checkout HEAD@{1}

If you now need to have the situation where the changes committed on step 2 are left only in the work tree, run

  $ git reset HEAD~

so that you have the HEAD and the index reset to the commit recorded on step 1 with the changes from the commit 2 left in the work tree.

Having said that, I'd note that I tend to do work on the detached HEAD but you could apply the same logic to working on a a branch.

Previous: Tim ChaseNext: Erik Cervin Edin
Message 4 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.