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

Re: git commit -p with file arguments

From
Jakub Narębski <jnareb@gmail.com>
Date
Sep 9, 2016, 20:39 UTC
Message-ID
<23de5ffe-eaf8-2d62-2202-f1bf6087d44b@gmail.com>
In-Reply-To
<xmqq8tv1c5nb.fsf@gitster.mtv.corp.google.com>
W dniu 09.09.2016 o 20:03, Junio C Hamano pisze:
Show 21 quoted lines
> Jacob Keller <jacob.keller@gmail.com> writes:
> 
>> It wants to commit bar too because you already added bar before. It works like:
>>
>> "git add bar && git add -p foo && git commit" does it not?
>>
>> I fail to see why "git commit -p <path>" would unstage the bar you
>> already added? Or am I missing some assumption here?
> 
> Yes.
> 
> "git commit -p <pathspec>" were added originally for lazy people who
> do not want to type "git add -p <pathspec> && git commit", which
> matches your expectation.  If you already added "bar" that is
> outside of the <pathspec> given to "add -p", the final "git commit"
> step would record the latest contents of "bar" in it.
> 
> For obvious reasons, "git commit -p <pathspec>" cannot be a
> short-hand to "git add -p <pathspec> && git commit <pathspec>", so
> the current behaviour was the best they could do for those who aded
> "commit -p", I guess.
The 'obvious reasons' are that 
  $ git add -p <pathspec> && git commit <pathspec>

would not work as intended, that is it wouldn't create commit out of HEAD and changes to <pathspec> created interactively in the index. "git commit <pathspec>" is a shortcut to "git commit --only <pathspec>"; the git-commit(1) manpage explains (emphasis mine):

 -o
 --only
    Make a commit by taking the updated *working tree contents* of
    the paths specified on the command line, disregarding any contents
    that have been staged for other paths. [...]

Which means that with "git add -p <pathspec> && git commit <pathspec>", the "git add -p <pathspec>" would carefully craft the <pathspec> state in the index... and "git commit <pathspec>" would take worktree version of <pathspec> for commit, ignoring what was in the index :-(

Currently there is no way to create commit out of subset of the index, e.g. with "git commit :0:<path>"

Best,
-- 
Jakub Narębski
Previous: Junio C HamanoNext: Christian Neukirchen
Message 9 of 16 in “git commit -p with file arguments”
  1. Christian NeukirchenSep 5, 2016
  2. Duy NguyenSep 9, 2016
  3. Duy NguyenOct 5, 2016
  4. Christian NeukirchenOct 5, 2016
  5. Junio C HamanoOct 5, 2016
  6. Jacob KellerSep 9, 2016
  7. Christian NeukirchenSep 9, 2016
  8. Junio C HamanoSep 9, 2016
  9. Jakub NarębskiSep 9, 2016
  10. Christian NeukirchenSep 9, 2016
  11. Jakub NarębskiSep 10, 2016
  12. Junio C HamanoSep 11, 2016
  13. Jacob KellerSep 11, 2016
  14. Junio C HamanoSep 12, 2016
  15. Jacob KellerSep 12, 2016
  16. Jakub NarębskiSep 12, 2016

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.