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.
- Git rebase instead of merge, the correct way to do it?
- Migrate commits from another branch, without common parent
- Fastest way to merge branches via gitlab (or git)?
- Using git subtree merging, while also merging in all branches of all merged subtrees
- git-diffall and git difftool --dir-diff not working with Araxis Merge
- Force to atomically rebase and non-fast-forward merge using Gerrit/GitLab
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?
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.