Git: How do you merge a file with a version of itself from a previous commit?

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?

  • how to merge to a local branch after checking out to a previous commit
  • Git reset behavior
  • git merge doesn't update .gitignore
  • Git p4 merge pull request
  • Undo a git merge that has been pushed and redo the merge
  • How do I mark conflicts as resolved in the git GUI tool?
  • Git: definition of upward/downward directions between branches feels counterintuitive
  • SVN merge trunk into branch deleting files
  • One Solution collect form web for “Git: How do you merge a file with a version of itself from a previous commit?”

    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.

    Git Baby is a git and github fan, let's start git clone.