Skip to contentSkip to conversation

How to effectively undo a part of a commit

DISCUSSION Bill Lear 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore

Bill Lear

author
#0 lore raw

A colleague made a change to a file and committed it. Then, he made another change to the file and somehow undid the previous work, then he committed the file. Now, he would like to get the first change he made and reapply it.

So, the changes look like this:

F1 -> delta 1 -> F2
F2 -> delta 2 -> F3

So, starting with F, he applies delta 1 to get F2. Then, he applies delta 2 to get F3.

He says that using cvs he would do something like this:

% cvs update -j F1 -j F2

To apply delta 1 to F3.

We tried using git to get the delta 1 as a patch --- that went fine. Then we used git-apply to apply the patch, but it refused, and it was obvious that the line numbers of the patch no longer corresponded to the line numbers in the file in his working tree.

Is there a way in git to do this, or is this an inherently unworkable problem, as for some reason, I suspect?

Bill

#1 lore raw
Quoted 2 lines · Bill Lear

So, starting with F, he applies delta 1 to get F2. Then, he applies delta 2 to get F3.

And delta 2 reverts delta 1 again, right?

Quoted 5 lines

He says that using cvs he would do something like this:

% cvs update -j F1 -j F2

To apply delta 1 to F3.

try git-cherry-pick -n F2.

Quoted 4 lines

We tried using git to get the delta 1 as a patch --- that went fine. Then we used git-apply to apply the patch, but it refused, and it was obvious that the line numbers of the patch no longer corresponded to the line numbers in the file in his working tree.

patches don't care about line numbers (at least not so much). you probably have overlapping changes, so apply can't work without generating a collision. try git-apply --reject and then merge the .rej file.

Quoted 2 lines

Is there a way in git to do this, or is this an inherently unworkable problem, as for some reason, I suspect?

there are plenty of ways. i think you could also do something like this (untested):

git checkout F3
git branch fixF3 F1
git reset --soft fixF3
git commit
git checkout F2
git cherry-pick fixF3

which first transplants F3 back on F1, removing the delta which reverted F2. then, having a nice patch now, applies it onto F2.

cheers
simon

Signature · 4 lines
Serve - BSD     +++  RENT this banner advert  +++    ASCII Ribbon   /"\
Work - Mac      +++  space for low €€€ NOW!1  +++      Campaign     \ /
Party Enjoy Relax   |   http://dragonflybsd.org      Against  HTML   \
Dude 2c 2 the max   !   http://golden-apple.biz       Mail + News   / \