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

Re: Requesting `git stash --cached` or something similar

From
Brandon McCaig <bamccaig@gmail.com>
Date
Apr 14, 2015, 02:05 UTC
Message-ID
<CANUGeEbRyG4A2UdTYOBgtjDtqi_A1WnbkOBjH_h2AcEZT741jQ@mail.gmail.com>
In-Reply-To
<20150414014435.GC8601@tsaunders-iceball.corp.tor1.mozilla.com>
Trevor:
On Mon, Apr 13, 2015 at 9:44 PM, Trevor Saunders <tbsaunde@tbsaunde.org> wrote:
Show 6 quoted lines
> I would expect the answers to be it sets the working directories state
> to the state in HEAD, and leaves untracked files alone.  If that's what
> you want you can do git commit -m <message>; git reset --hard; git reset
> --soft; git stash save to get the effect you want I believe. That said
> it seems like a kind of odd thing to want to do, what are you actually
> trying to do?

That looks like a bad solution. git reset --hard is going to throw away any remaining changes to the working tree. The previous commit would have committed the staged changes, albeit, you should connect the commands with && instead of ; to account for errors. After a `git reset --hard' there's no point in doing a `git reset --soft' because hard does *everything*. --soft would try to reset the HEAD without touching the index or working tree, but both have already been reset with --hard.

The motivation is most likely stashing a few changes away so that you can commit others that are ready to be committed while keeping others around to continue working on them. This too is a good observation. It could mean that the OP is inexperienced with a commit-often workflow. You can use git -add -i or -p to commit the good stuff and keep the bad stuff out to work on it more. The great thing about Git is that the history is very malleable. You can also commit the bad and fix it after, rebase the history to clean it up, and end up with perfect history while still keeping your changes safely in history.

The OP should experiment with workflows because Git is already very good at this. Stash isn't really needed. That said, I had forgotten that --patch was added to stash some time ago so if that is what you want then it already exists. It's not quite as easy as --cached, but it still gives you some control. It's still not nearly as good as using the full power of Git with a regular commit on a branch though.

Regards,
-- 
Brandon McCaig <bamccaig@gmail.com> <bamccaig@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bambams.ca/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'
Previous: Trevor SaundersNext: Trevor Saunders
Message 4 of 5 in “Requesting `git stash --cached` or something similar”
  1. Quinn TaylorApr 13, 2015
  2. Brandon McCaigApr 14, 2015
  3. Trevor SaundersApr 14, 2015
  4. Brandon McCaigApr 14, 2015
  5. Trevor SaundersApr 14, 2015

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.