# Stage or discard a hunk at a time?

4 messages from 2019-01-16 to 2019-04-30. Participants: Cameron Steffen, Jeff King, David Aguilar, Johannes Schindelin.
Thread: https://gitlist.dev/t/50243

## Cameron Steffen, 2019-01-16 19:09

Subject: Stage or discard a hunk at a time?
Message-ID: <CAAVFnNminTx_z9Y=jhzDnT0n5_zGD_k2SA84HQqMvKHJSvKcSA@mail.gmail.com>
URL: https://gitlist.dev/e/CAAVFnNminTx_z9Y%3DjhzDnT0n5_zGD_k2SA84HQqMvKHJSvKcSA%40mail.gmail.com

```
Hello,

I have this feature idea for git. There should be a command that
effectively combines git add -p and git checkout -p so that I can
navigate changed hunks and either stage or discard them.

There is already a SO question asking about this exactly...
https://stackoverflow.com/questions/11538650/simultaneously-git-add-p-and-git-checkout-p

Has this been discussed before? Is this a reasonable request? If so, I
might look into contributing the change myself.

Cameron

```

## Jeff King, 2019-04-22 19:28

Subject: Re: Stage or discard a hunk at a time?
Message-ID: <20190422192821.GA22798@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20190422192821.GA22798%40sigill.intra.peff.net
In-Reply-To: <CAAVFnNminTx_z9Y=jhzDnT0n5_zGD_k2SA84HQqMvKHJSvKcSA@mail.gmail.com>

```
On Wed, Jan 16, 2019 at 01:09:03PM -0600, Cameron Steffen wrote:

> I have this feature idea for git. There should be a command that
> effectively combines git add -p and git checkout -p so that I can
> navigate changed hunks and either stage or discard them.
> 
> There is already a SO question asking about this exactly...
> https://stackoverflow.com/questions/11538650/simultaneously-git-add-p-and-git-checkout-p
> 
> Has this been discussed before? Is this a reasonable request? If so, I
> might look into contributing the change myself.

This is something I've sometimes wanted, too. I don't think it would be
_too_ hard to do by modifying the add-interactive code. Both of those
operations are driven by the same code; see the %patch_modes hash in
git-add--interactive.perl, which defines the various situations. You
would need to modify the actual code to handle the tri-state (there is
not just "yes, apply it" and "no, leave it alone", but now "apply /
discard / skip"). But it seems do-able.

If you do plan to work on it, be aware that the perl bits of
add--interactive are being re-written in C. So it might make sense to
target the new C implementation instead of modifying the perl.

-Peff

PS Sorry for the slow reply, and I hope you're still interested in the
   topic. Your message got throw onto my "interesting, should reply"
   pile and I just happened to see it while clearing out old mails. :)

```

## David Aguilar, 2019-04-28 08:47

Subject: Re: Stage or discard a hunk at a time?
Message-ID: <20190428084718.GA21060@gmail.com>
URL: https://gitlist.dev/e/20190428084718.GA21060%40gmail.com
In-Reply-To: <CAAVFnNminTx_z9Y=jhzDnT0n5_zGD_k2SA84HQqMvKHJSvKcSA@mail.gmail.com>

```
On Wed, Jan 16, 2019 at 01:09:03PM -0600, Cameron Steffen wrote:
> Hello,
> 
> I have this feature idea for git. There should be a command that
> effectively combines git add -p and git checkout -p so that I can
> navigate changed hunks and either stage or discard them.
> 
> There is already a SO question asking about this exactly...
> https://stackoverflow.com/questions/11538650/simultaneously-git-add-p-and-git-checkout-p
> 
> Has this been discussed before? Is this a reasonable request? If so, I
> might look into contributing the change myself.
> 
> Cameron

Install Git Cola.

It's a powerful Git GUI with a keyboard-centric workflow.
You can do this completely through keyboard shortcuts
(or a mouse if you prefer).

Typically:

	$ git cola

	ctrl-k: focus the status widget
	j,k: navigate up/down the file list
	ctrl-s: stage files
	ctrl-u: revert files to index

	ctrl-j: focus the diff widget to operate at the line/hunk level
	j, k: move the cursor up/down lines, hold shift to select lines
	s: to stage selection, or current hunk if no lines are selected
	ctrl-u: revert the selection, or current hunk, to match the index

https://github.com/git-cola/git-cola

https://git-cola.github.io/downloads.html
-- 
David

```

## Johannes Schindelin, 2019-04-30 23:19

Subject: Re: Stage or discard a hunk at a time?
Message-ID: <nycvar.QRO.7.76.6.1904301918230.45@tvgsbejvaqbjf.bet>
URL: https://gitlist.dev/e/nycvar.QRO.7.76.6.1904301918230.45%40tvgsbejvaqbjf.bet
In-Reply-To: <20190422192821.GA22798@sigill.intra.peff.net>

```
Hi,

On Mon, 22 Apr 2019, Jeff King wrote:

> On Wed, Jan 16, 2019 at 01:09:03PM -0600, Cameron Steffen wrote:
>
> > I have this feature idea for git. There should be a command that
> > effectively combines git add -p and git checkout -p so that I can
> > navigate changed hunks and either stage or discard them.
> >
> > There is already a SO question asking about this exactly...
> > https://stackoverflow.com/questions/11538650/simultaneously-git-add-p-and-git-checkout-p
> >
> > Has this been discussed before? Is this a reasonable request? If so, I
> > might look into contributing the change myself.
>
> This is something I've sometimes wanted, too. I don't think it would be
> _too_ hard to do by modifying the add-interactive code. Both of those
> operations are driven by the same code; see the %patch_modes hash in
> git-add--interactive.perl, which defines the various situations. You
> would need to modify the actual code to handle the tri-state (there is
> not just "yes, apply it" and "no, leave it alone", but now "apply /
> discard / skip"). But it seems do-able.
>
> If you do plan to work on it, be aware that the perl bits of
> add--interactive are being re-written in C. So it might make sense to
> target the new C implementation instead of modifying the perl.

The work on that is tracked in PRs 170-175 on
https://github.com/gitgitgadget/git, i.e.

- https://github.com/gitgitgadget/git/pull/170
- https://github.com/gitgitgadget/git/pull/171
- https://github.com/gitgitgadget/git/pull/172
- https://github.com/gitgitgadget/git/pull/173
- https://github.com/gitgitgadget/git/pull/174
- https://github.com/gitgitgadget/git/pull/175

Ciao,
Johannes

```
