threads / discuss / 10476

How to remove a specific hunk

Subject: How to remove a specific hunk

## tl;dr

15 messages between Oct 26, 2007 and Oct 31, 2007.

replies: 14people: 10as markdown or json

Pascal Obry· Oct 26, 2007, 15:10 UTC · lore
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· Oct 26, 2007, 15:28 UTC · re: Pascal Obry · lore

Re: How to remove a specific hunk

Pascal Obry wrote:
Show 14 quoted lines
> 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· Oct 26, 2007, 15:38 UTC · re: Andreas Ericsson · lore

Re: How to remove a specific hunk

Andreas,
Show 8 quoted lines
> 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
Benoit SIGOURE· Oct 26, 2007, 17:03 UTC · re: Pascal Obry · lore

Re: How to remove a specific hunk

On Oct 26, 2007, at 5:38 PM, Pascal Obry wrote:
Show 13 quoted lines
> 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· Oct 26, 2007, 18:19 UTC · re: Benoit SIGOURE · lore

Re: How to remove a specific hunk

Hi,
On Fri, 26 Oct 2007, Benoit SIGOURE wrote:
Show 17 quoted lines
> 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

Olivier Ramonat· Oct 26, 2007, 21:59 UTC · re: Pascal Obry · lore

Re: How to remove a specific hunk

Pascal,
Pascal Obry <pascal.obry@wanadoo.fr> writes:
Show 13 quoted lines
> 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
Jeff King· Oct 26, 2007, 16:42 UTC · re: Andreas Ericsson · lore

Re: How to remove a specific hunk

On Fri, Oct 26, 2007 at 05:28:42PM +0200, Andreas Ericsson wrote:
Show 8 quoted lines
> 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· Oct 26, 2007, 16:49 UTC · re: Jeff King · lore

Re: How to remove a specific hunk

On Fri, Oct 26, 2007 at 12:42:08PM -0400, Jeff King wrote:
Show 9 quoted lines
> 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
Miles Bader· Oct 29, 2007, 07:03 UTC · re: Jeff King · lore

Re: How to remove a specific hunk

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
Alex Riesen· Oct 26, 2007, 19:20 UTC · re: Pascal Obry · lore

Re: How to remove a specific hunk

Pascal Obry, Fri, Oct 26, 2007 17:10:38 +0200:
Show 6 quoted lines
> 
> 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.

Benoit SIGOURE· Oct 31, 2007, 10:10 UTC · re: Alex Riesen · lore

Re: How to remove a specific hunk

On Oct 26, 2007, at 9:20 PM, Alex Riesen wrote:
Show 10 quoted lines
> 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· Oct 31, 2007, 11:06 UTC · re: Benoit SIGOURE · lore

Re: How to remove a specific hunk

Hi,
On Wed, 31 Oct 2007, Benoit SIGOURE wrote:
Show 17 quoted lines
> 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· Oct 31, 2007, 11:56 UTC · re: Johannes Schindelin · lore

Re: How to remove a specific hunk

* Johannes Schindelin:
Show 7 quoted lines
> 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
Pascal Obry· Oct 31, 2007, 12:24 UTC · re: Florian Weimer · lore

Re: How to remove a specific hunk

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
Peter Baumann· Oct 31, 2007, 12:00 UTC · re: Johannes Schindelin · lore

Re: How to remove a specific hunk

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:
Show 28 quoted lines
> 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

← back to recent threads