Discussion:
How to remove a specific hunk
Pascal Obry
2007-10-26 15:10:38 UTC
Permalink
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:42 UTC
Permalink
Post by Pascal Obry
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 ***@op5.se
OP5 AB www.op5.se
Tel: +46 8-230225 Fax: +46 8-230231
Pascal Obry
2007-10-26 15:38:45 UTC
Permalink
Andreas,
Post by Andreas Ericsson
Once you've added the other two hunks, they'll no longer show up in
$ 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
2007-10-26 17:03:49 UTC
Permalink
Post by Pascal Obry
Andreas,
Post by Andreas Ericsson
Once you've added the other two hunks, they'll no longer show up in
$ 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:32 UTC
Permalink
Hi,
Post by Benoit SIGOURE
Post by Pascal Obry
Andreas,
Post by Andreas Ericsson
Once you've added the other two hunks, they'll no longer show up in
$ 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
2007-10-26 21:59:45 UTC
Permalink
Pascal,
Post by Pascal Obry
Andreas,
Post by Andreas Ericsson
Once you've added the other two hunks, they'll no longer show up in
$ 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
2007-10-26 16:42:08 UTC
Permalink
Post by Andreas Ericsson
Once you've added the other two hunks, they'll no longer show up in
$ 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:26 UTC
Permalink
Post by Jeff King
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
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
2007-10-29 07:03:23 UTC
Permalink
Post by Jeff King
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
2007-10-26 19:20:34 UTC
Permalink
Post by Pascal Obry
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
2007-10-31 10:10:34 UTC
Permalink
Post by Alex Riesen
Post by Pascal Obry
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:18 UTC
Permalink
Hi,
Post by Benoit SIGOURE
Post by Alex Riesen
Post by Pascal Obry
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:00 UTC
Permalink
Post by 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.

--=20
=46lorian Weimer <***@bfk.de>
BFK edv-consulting GmbH http://www.bfk.de/
Kriegsstra=DFe 100 tel: +49-721-96201-1
D-76133 Karlsruhe fax: +49-721-96201-99
Pascal Obry
2007-10-31 12:24:58 UTC
Permalink
Post by Florian Weimer
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.

--=20

--|------------------------------------------------------
--| 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
2007-10-31 12:00:11 UTC
Permalink
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!
Post by Johannes Schindelin
Hi,
Post by Benoit SIGOURE
Post by Alex Riesen
Post by Pascal Obry
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
Loading...