# How to effectively undo a part of a commit

2 messages from 2007-03-01 to 2007-03-01. Participants: Bill Lear, Simon 'corecode' Schubert.
Thread: https://gitlist.dev/t/7040

## Bill Lear, 2007-03-01 16:33

Subject: How to effectively undo a part of a commit
Message-ID: <17895.72.525085.734138@lisa.zopyra.com>
URL: https://gitlist.dev/e/17895.72.525085.734138%40lisa.zopyra.com

```
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

```

## Simon 'corecode' Schubert, 2007-03-01 16:51

Subject: Re: How to effectively undo a part of a commit
Message-ID: <45E704AE.6000904@fs.ei.tum.de>
URL: https://gitlist.dev/e/45E704AE.6000904%40fs.ei.tum.de
In-Reply-To: <17895.72.525085.734138@lisa.zopyra.com>

```
Bill Lear wrote:
> 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?

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

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

> 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

-- 
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   / \


```
