Skip to contentSkip to conversation

How to remove a specific hunk

DISCUSSION Pascal Obry 11 participants 15 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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.

Signature · 8 lines
--|------------------------------------------------------
--| 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
#1 lore raw

Pascal Obry, Fri, Oct 26, 2007 17:10:38 +0200:

Quoted 6 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.

#7 lore raw
In reply to Pascal Obry
Quoted 8 lines · 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 ?

Quoted 6 lines

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.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#8 lore raw
Quoted 8 lines · Andreas Ericsson

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

#9 lore raw
Quoted 9 lines · 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 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

#11 lore raw
In reply to Andreas Ericsson

Andreas,

Quoted 8 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.

Signature · 8 lines
--|------------------------------------------------------
--| 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
#12 lore raw
Quoted 5 lines · Pascal Obry

Andreas,

Quoted 3 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:

Quoted 8 lines
Quoted 5 lines
$ 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! :)

Signature · 2 lines
Benoit Sigoure aka Tsuna
EPITA Research and Development Laboratory
#13 lore raw

Hi,

Quoted 13 lines · Benoit SIGOURE
Quoted 11 lines · Pascal Obry

Andreas,

Quoted 8 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
Quoted 4 lines
Quoted 2 lines

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

#14 lore raw
In reply to Pascal Obry

Pascal,

Quoted 5 lines · Pascal Obry

Andreas,

Quoted 3 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:

Quoted 8 lines
Quoted 5 lines
$ 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

#2 lore raw
In reply to Alex Riesen
Quoted 10 lines · Alex Riesen

Pascal Obry, Fri, Oct 26, 2007 17:10:38 +0200:

Quoted 6 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.

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.

Signature · 2 lines
Benoit Sigoure aka Tsuna
EPITA Research and Development Laboratory
#3 lore raw

Hi,

Quoted 10 lines · Benoit SIGOURE
Quoted 8 lines · Alex Riesen

Pascal Obry, Fri, Oct 26, 2007 17:10:38 +0200:

Quoted 6 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 ?

Quoted 7 lines
Quoted 2 lines

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

#4 lore raw

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!

Quoted 20 lines · Johannes Schindelin

Hi,

Quoted 8 lines · Benoit SIGOURE
Quoted 6 lines · Alex Riesen

Pascal Obry, Fri, Oct 26, 2007 17:10:38 +0200:

Quoted 4 lines

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 ?

Quoted 7 lines
Quoted 2 lines

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.

Quoted 8 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.

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

#5 lore raw
In reply to Johannes Schindelin

* Johannes Schindelin:

Quoted 7 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.

Signature · 4 lines
Florian Weimer                <[email protected]>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99
#6 lore raw
Quoted 3 lines · 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.

Signature · 8 lines
--|------------------------------------------------------
--| 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
#10 lore raw
In reply to Jeff King
Quoted 4 lines · 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

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