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

Re: Why not git reset --hard <path>?

From
GSGeorge Spelvin <linux@horizon.com>
Date
Sep 28, 2015, 23:52 UTC
Message-ID
<20150928235209.29725.qmail@ns.horizon.com>
In-Reply-To
<xmqqr3lib189.fsf@gitster.mtv.corp.google.com>
Junio C Hamano <gitster@pobox.com> wrote:
Show 7 quoted lines
> "George Spelvin" <linux@horizon.com> writes:
>> "git checkout <tree-ish> <pathspec>" modifies the index?
>>
>> Damn, I've been using git for years and I never knew that.
>
> ... which would be an indication that the behaviour is most likely
> the most natural one.

I think it's more that often, staging a file to the index is pretty harmless, so it doesn't actually matter.

Both of the commonest forms of commit ("git commit -a" and "git commit [-o] <paths>") aren't affected, and if I'm doing something trickier, I'll usually examine the status and make sure I've got it right.

So I could have encountered it and put it down to fat-fingering on my part.

>> But I just tested, and it does.  Damn, now I have to figure out
>> how to "leapfrog" a file from history into the working tree without
>> overwriting the index; that's occasionally useful.
Show 8 quoted lines
> ... and indeed it is useful in some rare cases.  Either
>
>    git diff <tree-ish> <pathspec> | git apply -R
>
> or
> 
>    git checkout <tree-ish> <pathspec> &&
>    git reset <pathspec>
The former would work, and thanks for the idea, but for a single file
I'd probably do one of
	git show <tree-ish>:<path> > <path>
	git cat-file blob <tree-ish>:<path> > <path>

The checkout/reset wouldn't work in the case I'm thinking about, which is when I want to import a small piece (say, helper function that got deleted) from an old or other-branch version of a file. I.e. a partial revert or cherry-pick.

If I had some current changes to merge with, I'd stage them, pull the *other* version into the tree, and use something like git-gui to add the hunk I want to the staged version.

The whole point is that the staged state is important and I don't want it overwritten.

There are other ways, of course:
- Show or cat-file the other version into a temporary file and manually
  copy and paste.  No need to stage anything.
- Commit the current change, then add the additional changes and amend the
  commit.
Previous: Junio C Hamano
Message 11 of 11 in “Why not git reset --hard <path>?”
  1. George SpelvinSep 28, 2015
  2. Junio C HamanoSep 28, 2015
  3. Jacob KellerSep 28, 2015
  4. Junio C HamanoSep 28, 2015
  5. Theodore Ts'oSep 28, 2015
  6. Jacob KellerSep 29, 2015
  7. George SpelvinSep 29, 2015
  8. Philip OakleySep 29, 2015
  9. George SpelvinSep 28, 2015
  10. Junio C HamanoSep 28, 2015
  11. George SpelvinSep 28, 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.