# How to remove a specific hunk

15 messages from 2007-10-26 to 2007-10-31. Participants: Pascal Obry, Andreas Ericsson, Jeff King, Benoit SIGOURE, Johannes Schindelin, Alex Riesen, Olivier Ramonat, Miles Bader, Florian Weimer, Peter Baumann.
Thread: https://gitlist.dev/t/10476

## Pascal Obry, 2007-10-26 15:10

Subject: How to remove a specific hunk
Message-ID: <4722036E.5030204@wanadoo.fr>
URL: https://gitlist.dev/e/4722036E.5030204%40wanadoo.fr

```

Hello,

I'm very new to Git... but start to love it :)

Before committing sometimes I want to remove a specific hunk. Say in
file a.txt I have in the diff 3 hunks, I want to revert/delete/remove
the second one. Is there a way to do that ?

I understand that I can git add interactive and select the hook I want
to commit, but this is not fully equivalent. I'm not yet ready to commit
I just want to undo a specific change and test the code without it...

Any idea ?

Thanks,
Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595

```

## Andreas Ericsson, 2007-10-26 15:28

Subject: Re: How to remove a specific hunk
Message-ID: <472207AA.8030100@op5.se>
URL: https://gitlist.dev/e/472207AA.8030100%40op5.se
In-Reply-To: <4722036E.5030204@wanadoo.fr>

```
Pascal Obry wrote:
> Hello,
> 
> I'm very new to Git... but start to love it :)
> 
> Before committing sometimes I want to remove a specific hunk. Say in
> file a.txt I have in the diff 3 hunks, I want to revert/delete/remove
> the second one. Is there a way to do that ?
> 
> I understand that I can git add interactive and select the hook I want
> to commit, but this is not fully equivalent. I'm not yet ready to commit
> I just want to undo a specific change and test the code without it...
> 
> Any idea ?
> 

Once you've added the other two hunks, they'll no longer show up in
git-diff, so you can do something like this:

$ git-add -i; # add the other two hunks to commit
$ git-diff > middle-hunk.patch
$ git-apply -R middle-hunk.patch
test, test, test
$ git-apply middle-hunk.patch

Completely untested, so take a copy before you try it.

-- 
Andreas Ericsson                   andreas.ericsson@op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231

```

## Pascal Obry, 2007-10-26 15:38

Subject: Re: How to remove a specific hunk
Message-ID: <47220A05.4040705@wanadoo.fr>
URL: https://gitlist.dev/e/47220A05.4040705%40wanadoo.fr
In-Reply-To: <472207AA.8030100@op5.se>

```
Andreas,

> Once you've added the other two hunks, they'll no longer show up in
> git-diff, so you can do something like this:
> 
> $ git-add -i; # add the other two hunks to commit
> $ git-diff > middle-hunk.patch
> $ git-apply -R middle-hunk.patch
> test, test, test
> $ git-apply middle-hunk.patch

Thanks, this will clearly work. I was expecting something more
integrated like a "git reset --interactive" or something like that :)

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595

```

## Jeff King, 2007-10-26 16:42

Subject: Re: How to remove a specific hunk
Message-ID: <20071026164207.GB19673@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20071026164207.GB19673%40coredump.intra.peff.net
In-Reply-To: <472207AA.8030100@op5.se>

```
On Fri, Oct 26, 2007 at 05:28:42PM +0200, Andreas Ericsson wrote:

> Once you've added the other two hunks, they'll no longer show up in
> git-diff, so you can do something like this:
>
> $ git-add -i; # add the other two hunks to commit
> $ git-diff > middle-hunk.patch
> $ git-apply -R middle-hunk.patch
> test, test, test
> $ git-apply middle-hunk.patch

That has the side effect of changing the index state. My preferred
method would be to just excise the hunk from the diff using an editor
(and this actually has nothing to do with git; you could be using
diff/patch):

git-diff file >patch
# remove every hunk except what you want to cut out
$EDITOR patch
git-apply -R patch

-Peff

```

## Jeff King, 2007-10-26 16:49

Subject: Re: How to remove a specific hunk
Message-ID: <20071026164926.GA21160@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20071026164926.GA21160%40coredump.intra.peff.net
In-Reply-To: <20071026164207.GB19673@coredump.intra.peff.net>

```
On Fri, Oct 26, 2007 at 12:42:08PM -0400, Jeff King wrote:

> That has the side effect of changing the index state. My preferred
> method would be to just excise the hunk from the diff using an editor
> (and this actually has nothing to do with git; you could be using
> diff/patch):
> 
> git-diff file >patch
> # remove every hunk except what you want to cut out
> $EDITOR patch
> git-apply -R patch

BTW, since this is inherently a non-git operation, there are other tools
that some may find friendlier than an editor. Kompare will let you
unapply differences, for example, and I would be shocked if emacs didn't
have some tool for this.

-Peff

```

## Benoit SIGOURE, 2007-10-26 17:03

Subject: Re: How to remove a specific hunk
Message-ID: <2669F76D-6FF2-4CCF-9337-639D84EE65E8@lrde.epita.fr>
URL: https://gitlist.dev/e/2669F76D-6FF2-4CCF-9337-639D84EE65E8%40lrde.epita.fr
In-Reply-To: <47220A05.4040705@wanadoo.fr>

```
On Oct 26, 2007, at 5:38 PM, Pascal Obry wrote:

> Andreas,
>
>> Once you've added the other two hunks, they'll no longer show up in
>> git-diff, so you can do something like this:
>>
>> $ git-add -i; # add the other two hunks to commit
>> $ git-diff > middle-hunk.patch
>> $ git-apply -R middle-hunk.patch
>> test, test, test
>> $ git-apply middle-hunk.patch
>
> Thanks, this will clearly work. I was expecting something more
> integrated like a "git reset --interactive" or something like that :)

That'd be great!  :)

-- 
Benoit Sigoure aka Tsuna
EPITA Research and Development Laboratory



```

## Johannes Schindelin, 2007-10-26 18:19

Subject: Re: How to remove a specific hunk
Message-ID: <Pine.LNX.4.64.0710261917270.4362@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0710261917270.4362%40racer.site
In-Reply-To: <2669F76D-6FF2-4CCF-9337-639D84EE65E8@lrde.epita.fr>

```
Hi,

On Fri, 26 Oct 2007, Benoit SIGOURE wrote:

> On Oct 26, 2007, at 5:38 PM, Pascal Obry wrote:
> 
> > Andreas,
> > 
> > > Once you've added the other two hunks, they'll no longer show up in
> > > git-diff, so you can do something like this:
> > > 
> > > $ git-add -i; # add the other two hunks to commit
> > > $ git-diff > middle-hunk.patch
> > > $ git-apply -R middle-hunk.patch
> > > test, test, test
> > > $ git-apply middle-hunk.patch
> > 
> > Thanks, this will clearly work. I was expecting something more
> > integrated like a "git reset --interactive" or something like that :)
> 
> That'd be great!  :)

I skipped over the beginnings of this thread because of time constraints, 
but would "git reset HEAD^ && git add -i" not helped you?  git add -i 
allows you to stage hunks, so by just _not_ staging _that_ hunk but 
everything else, should have worked for you, right?

There's also git-gui which does all that graphically for you (remember the 
right mouse button).

Ciao,
Dscho

```

## Alex Riesen, 2007-10-26 19:20

Subject: Re: How to remove a specific hunk
Message-ID: <20071026192034.GA3062@steel.home>
URL: https://gitlist.dev/e/20071026192034.GA3062%40steel.home
In-Reply-To: <4722036E.5030204@wanadoo.fr>

```
Pascal Obry, Fri, Oct 26, 2007 17:10:38 +0200:
> 
> I'm very new to Git... but start to love it :)
> 
> Before committing sometimes I want to remove a specific hunk. Say in
> file a.txt I have in the diff 3 hunks, I want to revert/delete/remove
> the second one. Is there a way to do that ?

Take a look at git-gui. Try right-clicking in the diff pane at the
bottom.

```

## Olivier Ramonat, 2007-10-26 21:59

Subject: Re: How to remove a specific hunk
Message-ID: <87prz1a65q.fsf@tah.ramonat.fr>
URL: https://gitlist.dev/e/87prz1a65q.fsf%40tah.ramonat.fr
In-Reply-To: <47220A05.4040705@wanadoo.fr>

```
Pascal,

Pascal Obry <pascal.obry@wanadoo.fr> writes:
> Andreas,
>
>> Once you've added the other two hunks, they'll no longer show up in
>> git-diff, so you can do something like this:
>> 
>> $ git-add -i; # add the other two hunks to commit
>> $ git-diff > middle-hunk.patch
>> $ git-apply -R middle-hunk.patch
>> test, test, test
>> $ git-apply middle-hunk.patch
>
> Thanks, this will clearly work. I was expecting something more
> integrated like a "git reset --interactive" or something like that :)

A solution could be :

git add -i 
--> Add the two "good" hunks

git checkout-index file_with_bad_hunk
--> Remove the "bad" hunk by getting the staged version

And then 
git reset HEAD file_with_bad_hunk 
if you want to unstage it.

Olivier

```

## Miles Bader, 2007-10-29 07:03

Subject: Re: How to remove a specific hunk
Message-ID: <buor6jeo11g.fsf@dhapc248.dev.necel.com>
URL: https://gitlist.dev/e/buor6jeo11g.fsf%40dhapc248.dev.necel.com
In-Reply-To: <20071026164926.GA21160@coredump.intra.peff.net>

```
Jeff King <peff@peff.net> writes:
> BTW, since this is inherently a non-git operation, there are other tools
> that some may find friendlier than an editor. Kompare will let you
> unapply differences, for example, and I would be shocked if emacs didn't
> have some tool for this.

M-x diff-mode
<move to bad hunk>
C-u C-c C-a

-Miles

-- 
Next to fried food, the South has suffered most from oratory.
  			-- Walter Hines Page

```

## Benoit SIGOURE, 2007-10-31 10:10

Subject: Re: How to remove a specific hunk
Message-ID: <13D08D3F-3647-4E20-AF80-8EC6FD14E66A@lrde.epita.fr>
URL: https://gitlist.dev/e/13D08D3F-3647-4E20-AF80-8EC6FD14E66A%40lrde.epita.fr
In-Reply-To: <20071026192034.GA3062@steel.home>

```
On Oct 26, 2007, at 9:20 PM, Alex Riesen wrote:

> Pascal Obry, Fri, Oct 26, 2007 17:10:38 +0200:
>>
>> I'm very new to Git... but start to love it :)
>>
>> Before committing sometimes I want to remove a specific hunk. Say in
>> file a.txt I have in the diff 3 hunks, I want to revert/delete/remove
>> the second one. Is there a way to do that ?
>
> Take a look at git-gui. Try right-clicking in the diff pane at the
> bottom.

This only allows you to stage a given hunk, not to remove one.  Right  
now I'm in a situation where I need to remove a specific hunk to  
compile and it's sad that git-gui doesn't provide an option so that  
you can right-click -> revert hunk.

-- 
Benoit Sigoure aka Tsuna
EPITA Research and Development Laboratory



```

## Johannes Schindelin, 2007-10-31 11:06

Subject: Re: How to remove a specific hunk
Message-ID: <Pine.LNX.4.64.0710311104450.4362@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0710311104450.4362%40racer.site
In-Reply-To: <13D08D3F-3647-4E20-AF80-8EC6FD14E66A@lrde.epita.fr>

```
Hi,

On Wed, 31 Oct 2007, Benoit SIGOURE wrote:

> On Oct 26, 2007, at 9:20 PM, Alex Riesen wrote:
> 
> > Pascal Obry, Fri, Oct 26, 2007 17:10:38 +0200:
> > > 
> > > I'm very new to Git... but start to love it :)
> > > 
> > > Before committing sometimes I want to remove a specific hunk. Say in
> > > file a.txt I have in the diff 3 hunks, I want to revert/delete/remove
> > > the second one. Is there a way to do that ?
> > 
> > Take a look at git-gui. Try right-clicking in the diff pane at the
> > bottom.
> 
> This only allows you to stage a given hunk, not to remove one.  Right 
> now I'm in a situation where I need to remove a specific hunk to compile 
> and it's sad that git-gui doesn't provide an option so that you can 
> right-click -> revert hunk.

You have seen that there are two different file lists, "staged changes" 
and "unstaged changes", right?  AFAIK if you click on the file in "staged 
changes", you can find the staged hunk and then remove it from the staged 
area.

"Revert hunk" would not make any sense, since the hunk disappears once you 
staged/unstaged it.

Ciao,
Dscho

```

## Florian Weimer, 2007-10-31 11:56

Subject: Re: How to remove a specific hunk
Message-ID: <82zlxzsdkf.fsf@mid.bfk.de>
URL: https://gitlist.dev/e/82zlxzsdkf.fsf%40mid.bfk.de
In-Reply-To: <Pine.LNX.4.64.0710311104450.4362@racer.site>

```
* Johannes Schindelin:

> You have seen that there are two different file lists, "staged
> changes" and "unstaged changes", right?  AFAIK if you click on the
> file in "staged changes", you can find the staged hunk and then
> remove it from the staged area.
>
> "Revert hunk" would not make any sense, since the hunk disappears
> once you staged/unstaged it.

I think Benoit wants to remove it from the working copy (and,
presumably, the index too).  "Revert hunk" and "Move hunk to stash"
might indeed be useful additions to citool.

-- 
Florian Weimer                <fweimer@bfk.de>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99

```

## Peter Baumann, 2007-10-31 12:00

Subject: Re: How to remove a specific hunk
Message-ID: <20071031120011.GI4435@xp.machine.xx>
URL: https://gitlist.dev/e/20071031120011.GI4435%40xp.machine.xx
In-Reply-To: <Pine.LNX.4.64.0710311104450.4362@racer.site>

```

spearce Cc'ed, because this seems like a nice feature to add to git-gui
In no way should this imply that YOU have to implement this!


On Wed, Oct 31, 2007 at 11:06:18AM +0000, Johannes Schindelin wrote:
> Hi,
> 
> On Wed, 31 Oct 2007, Benoit SIGOURE wrote:
> 
> > On Oct 26, 2007, at 9:20 PM, Alex Riesen wrote:
> > 
> > > Pascal Obry, Fri, Oct 26, 2007 17:10:38 +0200:
> > > > 
> > > > Before committing sometimes I want to remove a specific hunk. Say in
> > > > file a.txt I have in the diff 3 hunks, I want to revert/delete/remove
> > > > the second one. Is there a way to do that ?
> > > 
> > > Take a look at git-gui. Try right-clicking in the diff pane at the
> > > bottom.
> > 
> > This only allows you to stage a given hunk, not to remove one.  Right 
> > now I'm in a situation where I need to remove a specific hunk to compile 
> > and it's sad that git-gui doesn't provide an option so that you can 
> > right-click -> revert hunk.
> 
> You have seen that there are two different file lists, "staged changes" 
> and "unstaged changes", right?  AFAIK if you click on the file in "staged 
> changes", you can find the staged hunk and then remove it from the staged 
> area.
> 
> "Revert hunk" would not make any sense, since the hunk disappears once you 
> staged/unstaged it.
> 

On the other hand, something like this would allow you to remove bogus
hunks like debug statements you want to lose, because you tested it and
all works now.

Now you first have to stage all the things you want to keep, and then
select "Commit->Revert Changes" in the menu to remove a hunk forever which
is a little clumsy if you have many hunks and just want to remove one of
them. Seems like a nice feature to add to git gui if you throw in an extra
Yes-No question for the user IFF he realy wants to remove the hunk forever.

-Peter

```

## Pascal Obry, 2007-10-31 12:24

Subject: Re: How to remove a specific hunk
Message-ID: <4728741A.90503@obry.net>
URL: https://gitlist.dev/e/4728741A.90503%40obry.net
In-Reply-To: <82zlxzsdkf.fsf@mid.bfk.de>

```
Florian Weimer a écrit :
> I think Benoit wants to remove it from the working copy (and,
> presumably, the index too).  "Revert hunk" and "Move hunk to stash"
> might indeed be useful additions to citool.

That was not Benoit but me who asked this question. I want indeed the
hunk to be reverted/removed on the working copy. I had many answers to
do this but I still think a "git reset --interactive" with the
possibility to revert some hunks would be easier.

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595

```
