threads / discuss / 25234

merge removed content back into current content

Subject: merge removed content back into current content

## tl;dr

5 messages between Sep 25, 2010 and Sep 28, 2010.

replies: 4people: 3as markdown or json

Neal Kreitzinger· Sep 25, 2010, 03:06 UTC · lore

How do I tell git to merge a single program from an old commit into the current version of that program in the HEAD commit? In this scenario, I want to get back some removed code but still keep the new code.

e.g.
Commit-1 = initial commit containing Program-A, and other programs
Commit-2 = add Changeset-1 to Program-A , and make changes to other programs

Commit-3 = remove Changeset-1 from Program-A, then add Changeset-2 to Program-A, and make changes to other programs

*desired* Commit-4 = only merge Program-A from Commit-2 into Program-A of Commit-3, and don't change any other programs (in other words, get my old changes from Commit-2 back, but don't loose the new changes from Commit-3)

v/r, Neal

Elijah Newren· Sep 25, 2010, 23:43 UTC · re: Neal Kreitzinger · lore

Re: merge removed content back into current content

On Fri, Sep 24, 2010 at 9:06 PM, Neal Kreitzinger <neal@rsss.com> wrote:
Show 17 quoted lines
> How do I tell git to merge a single program from an old commit into the
> current version of that program in the HEAD commit?  In this scenario, I
> want to get back some removed code but still keep the new code.
>
> e.g.
>
> Commit-1 = initial commit containing Program-A, and other programs
>
> Commit-2 = add Changeset-1 to Program-A , and make changes to other programs
>
> Commit-3 = remove Changeset-1 from Program-A, then add Changeset-2 to
> Program-A, and make changes to other programs
>
> *desired* Commit-4 = only merge Program-A from Commit-2 into Program-A of
> Commit-3, and don't change any other programs
> (in other words, get my old changes from Commit-2 back, but don't loose the
> new changes from Commit-3)
Does something like:
  git diff commit1 commit2 -- ProgramA | git apply
do what you need?
If diff+apply doesn't work[1], you can try
  git cat-file -p commit1:ProgramA > base
  git cat-file -p commit2:ProgramA > other
  git cat-file -p commit3:ProgramA > current
  git merge-file current base other

[1] e.g. would cause conflicts -- btw, does anyone know how to force git apply to proceed and add conflict markers if necessary rather than just bailing out?

Eric Raible· Sep 27, 2010, 18:31 UTC · re: Elijah Newren · lore

Re: Re: merge removed content back into current content

On 11:59 AM, Elijah Newren wrote:
> btw, does anyone know how to force
> git apply to proceed and add conflict markers if necessary rather than
> just bailing out?

It's not quite the same, and you probably already know about it, but there's always: git apply --reject

Neal Kreitzinger· Sep 28, 2010, 16:26 UTC · re: Elijah Newren · lore

Re: merge removed content back into current content

Show 27 quoted lines
>"Elijah Newren" <newren@gmail.com> wrote in message 
> >news:AANLkTimxHbCktv=kaq0UfV+u1kH1Pb2LYA2Xi=qkgduW@mail.gmail.com...
>On Fri, Sep 24, 2010 at 9:06 PM, Neal Kreitzinger <neal@rsss.com> wrote:
>> How do I tell git to merge a single program from an old commit into the
>> current version of that program in the HEAD commit? In this scenario, I
>> want to get back some removed code but still keep the new code.
>>
>> e.g.
>>
>> Commit-1 = initial commit containing Program-A, and other programs
>>
>> Commit-2 = add Changeset-1 to Program-A , and make changes to other 
>> programs
>>
>> Commit-3 = remove Changeset-1 from Program-A, then add Changeset-2 to
>> Program-A, and make changes to other programs
>>
>> *desired* Commit-4 = only merge Program-A from Commit-2 into Program-A of
>> Commit-3, and don't change any other programs
>> (in other words, get my old changes from Commit-2 back, but don't loose 
>> the
>> new changes from Commit-3)
>
>Does something like:
>  git diff commit1 commit2 -- ProgramA | git apply
>do what you need?
>

All of our source conflicts because we add a user/datestamp to the first line. We do this to insure the programmer reviews the merge. However, I never used git apply before so that was interesting to learn.

Show 6 quoted lines
>If diff+apply doesn't work[1], you can try
>  git cat-file -p commit1:ProgramA > base
>  git cat-file -p commit2:ProgramA > other
>  git cat-file -p commit3:ProgramA > current
>  git merge-file current base other
>

This method is also something new to me, but it seems too manual. It also does not do the normal git merge where the results are in the index. An easier method is to do this: git difftool commit1 commit2 -- ProgramA and then use kdiff3's merge option. this does not include the common ancestor as a reference like a normal git merge conflict followed by git mergetool (kdiff3), but is user friendly. However, I'm not crazy about it because I'd rather have the merge results in the index and use git mergetool like I do for commit merges.

>[1] e.g. would cause conflicts -- btw, does anyone know how to force
>git apply to proceed and add conflict markers if necessary rather than
>just bailing out? 
Neal Kreitzinger· Sep 28, 2010, 16:49 UTC · re: Neal Kreitzinger · lore

Re: merge removed content back into current content

"Neal Kreitzinger" <neal@rsss.com> wrote in message news:i7jp1a$kc0$1@dough.gmane.org...

Show 19 quoted lines
> How do I tell git to merge a single program from an old commit into the 
> current version of that program in the HEAD commit?  In this scenario, I 
> want to get back some removed code but still keep the new code.
>
> e.g.
>
> Commit-1 = initial commit containing Program-A, and other programs
>
> Commit-2 = add Changeset-1 to Program-A , and make changes to other 
> programs
>
> Commit-3 = remove Changeset-1 from Program-A, then add Changeset-2 to 
> Program-A, and make changes to other programs
>
> *desired* Commit-4 = only merge Program-A from Commit-2 into Program-A of 
> Commit-3, and don't change any other programs
> (in other words, get my old changes from Commit-2 back, but don't loose 
> the new changes from Commit-3)
>

Here is a method for invoking git merge and git mergetool, but its very onerous and I'm hoping there's a more straightforward method to merge using the index so let me know if you have something better. I've published these steps to my coworkers to answer their request for merging just two programs (as opposed to 2 commits), but it seems pretty ridiculous to have to go thru all of this just to merge one version of a program into another.

Using git diff commit-2 commit-3 -- Program-A and then using kdiff3's merge option is pretty easy, but is not using the index, conflicts, or git-mergetool (common ancestor 3-way merge) so I'm not crazy about it, but I have to recommend it over the cherry-pick because its so straightforward and the cherry-pick is so convoluted. I feel like I'm going around git when I use git difftool (kdiff3 merge) to do this and that I'm going thru git when I use the cherry-pick methods below, but they are way more work!

Cherry-pick methods:

Possibly a more correct way to perform the merge is to let git run the merge and generate conflicts for you to resolve like any other git merge with git mergetool utilizing the common ancestor history.

Scenario:  use git to merge an old program version into the current program 
version and git-mergetool to resolve conflicts.  The old version does not 
reside in a commit by itself so therefore that commit cannot be cherry 
picked.

Assuming you are on already on a branch that contains the old version and new version of program in its history.

(1)  In gitk view the changes made to the program in question
$ gitk 12.5/Generic/Base -- SRC/SALES/SALEOUT

(2) Right click on the commit containing the old version of the program and tag it, e.g. WKTG/MRG/Theirs/SALEOUT

(in a merge, "ours" = version in current branch HEAD, "theirs" = version from other branch being merged into current branch)

Decision:
If commit containing old version contains a few other objects with changes:
(3) Cherry-pick the commit onto your branch.

$ git cherry-pick WKTG/MRG/Theirs/SALEOUT (Cherry pick will inform you of conflicts)

(4) Discard the cherry-picked objects you don't want.
$ git checkout --ours otherobject1 otherobject2 otherobject3 etc.
(5) Merge your program with git-mergetool.
$ git mergetool (Merge the stuff using kdiff3)
$ git add SRC/SALES/SALEOUT

$ git commit -c WKTG/MRG/Theirs/SALEOUT (Cherry pick will tell you exactly how to do the git commit -c)

You're Done.

If commit containing old version contains "too many" other objects with changes, it may be faster to do this:

(3) Create a branch that points to MARK/MRG/Theirs/SALEOUT
$ git checkout -b WKBR/MRG/Theirs/SALEOUT WKTG/MRG/Theirs/SALEOUT

(4) Split the commit to isolate the "cherry" (the old version of the program) that you want to pick.

$ git rebase -i --onto WKBR/MRG/Theirs/SALEOUT~1 WKBR/MRG/Theirs/SALEOUT~1 WKBR/MRG/Theirs/SALEOUT

(a) Change "pick" to "edit" for the commit in the rebase editor and save it. Rebase will inform you that it stopped after that commit.

(b) "Undo" the commit:

$ git reset HEAD^ (this will rollback the HEAD and index to the prior commit, but leave the commit changes in your working tree)

(c) Stage the old version of the program and commit it.
$ git add SRC/SALES/SALEOUT
$ git commit
(d) Commit the rest of the stuff.
$ git add .
$ git commit
(5) Tag the commit containing the old version of the program.
$ git logr (copy the sha1 of the second to last commit)
$ git tag <sha1-of-2nd-to-last-commit> WKTG/MRG/Cherry/SALEOUT

(6) Cherry pick that cherry commit onto your branch. Cherry-pick will inform you of the conflict (user/datestamp will at least conflict).

$ git checkout <project-branch>
$ git cherry-pick --no-commit WKTG/MRG/Cherry/SALEOUT
(7) Merge using git mergtool.
$ git mergetool (Merge the stuff using kdiff3)
$ git add SRC/SALES/SALEOUT

$ git commit -c WKTG/MRG/Theirs/SALEOUT (Cherry pick will tell you exactly how to do the git commit -c)

You're Done.
v/r,
Neal

← back to recent threads