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

Re: Feature request - git add --patch should have an option to discard or drop a hunk Inbox

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jan 8, 2021, 15:21 UTC
Message-ID
<nycvar.QRO.7.76.6.2101081614440.2213@tvgsbejvaqbjf.bet>
In-Reply-To
<X/ghgg+IdZYri1KV@coredump.intra.peff.net>
Hi Peff,
On Fri, 8 Jan 2021, Jeff King wrote:
Show 39 quoted lines
> On Thu, Jan 07, 2021 at 11:43:45AM -0500, Jon Sagotsky wrote:
>
> > I frequently make changes to a number of files.  I use `git add -p` to
> > select a number of changes and wrap them up in a commit.  Then I run
> > it again to build another commit.  Wash, rinse, repeat.
> >
> > Sometimes I make a change I don't intend to keep.  Maybe it's a typo,
> > or some debug code, or my linter does something naughty.  As
> > described, I have to skip that hunk once per commit I'm building.
> > Usually this is merely inconvenient, but each pass through `git add
> > -p` runs the risk of me mistakenly adding unintended changes to my
> > commit.
>
> I do something similar and have run into the same problem. There's an
> old discussion here going into some ideas:
>
>   https://lore.kernel.org/git/EE89F0A1-1C07-4597-B654-035F657AD09F@me.com/
>
> The interesting bits are I think:
>
>   - some tools let you do this already (magit was mentioned there, and
>     I'd expect vim plugins like fugitive can probably do the same; tig
>     can also do so from its "stage" interface)
>
>   - it probably wouldn't be _too_ hard to implement, because we already
>     drive those actions from the same code that is invoked by "add -p"
>     and "git checkout -p"; the difference is just which program we feed
>     the hunks to.
>
>   - in the most general form of the tool, it would let you take a pass
>     through the hunks and annotating them. The simple common form is two
>     annotations: stage these ones, discard those ones. There may be room
>     for a more generalized tool, or it may just be over-complicating
>     things.
>
>     The generalized form probably shouldn't be "add -p". The simplified
>     one (just adding "discard this hunk from the worktree") could
>     perhaps be, though it does feel a little weird for "git add" to
>     modify working tree files.

We do have `git stash -p`, which I used sometimes to discard such unwanted pieces. However, `git stash -p` suffers from a couple implementation details (see e.g. https://lore.kernel.org/git/20200731165140.29197-1-alban.gruin@gmail.com/)

More often than not, I am actually using `git add -p` to commit those changes instead, using the commit message `TO-DROP` and later using `rebase -i` to actually drop them.

I could imagine that it should not be _too_ hard to implement an action in `git add -p` to allow stashing a given hunk. The biggest challenge there would be to make sure that potentially edited, to-be-stashed changes won't interfere with staging what was intended (because `git add -p` batches the changing per-file, and if the index changes while picking what to stage, things might conflict).

Maybe an Outreachy project?

Ciao, Dscho

Previous: Jeff KingNext: Jeff King
Message 3 of 4 in “Feature request - git add --patch should have an option to discard or drop a hunk Inbox”
  1. Jon SagotskyJan 7, 2021
  2. Jeff KingJan 8, 2021
  3. Johannes SchindelinJan 8, 2021
  4. Jeff KingJan 8, 2021

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.