1

There are some changes I made to a file in a commit A, then I undid the changes by mistake and went on further to make changes in commits B and C.

I want the changes in commit A to be in my file, but the changes in B and C should not be lost either.

Let's say my branch is now at C.

I don't want to

$ git checkout --patch 

because I want the file to contain the changes I made in B and C and doing a checkout of the file from commit A will rewrite the file in both the index and the working tree.

I can't do a cherry-pick because commit A is a merge commit(there are two contributors to the repository and I deleted the changes my mentor made by mistake in the subsequent commits after I merged them) and I might end up with a mess if I specified either parent.

Apart from manually copying the changes I want in the file, is there any other way to accomplish this?

eebbesen
  • 5,070
  • 8
  • 48
  • 70
rivendell
  • 442
  • 1
  • 5
  • 12

1 Answers1

5

You can create a patch and attempt to apply the patch. If you experience issues with conflicts such as

error: patch failed: <file_name>:1
error: <file_name>: patch does not apply

the merge will require manual intervention.

Here's an example:

$ git format-patch -n <sha_from_commit_you_want_to_merge> > patch_file.patch
$ git apply patch_file.patch

It is possible this strategy will still be problematic because of the nature of commit A. You cannot separate constituent components of a merge commit without access to the branch from whence the individual commits were made. Your branch/repo just sees that as one commit.

Of course if you do have access to the original branch you can just create a patch file from there, even if they are in different repositories.

Community
  • 1
  • 1
eebbesen
  • 5,070
  • 8
  • 48
  • 70
  • I hope there is a more natural way! – rivendell Jul 11 '13 at 02:33
  • I don't know how much simpler it can get than two commands to merge code (yes, one command would be easier :)). If there are irreconcilable merge issues there I don't know of a version control system that can help you any better than Git does. If you have issues with this approach edit your post or add a comment and we'll see what we can do. – eebbesen Jul 11 '13 at 02:37
  • I am sorry for a sloppy response! No excuse but I was occupied with fixing bugs in the code and found this git issue to be an interference! Well, I actually thought `git-merge-file` would do something similar to solving my problem here but turns out it is for merging two files that base themselves from the same file! Your method works perfectly fine though! Thank you very much! – rivendell Jul 11 '13 at 19:11