Noel
Noel

Reputation: 2091

Git: Merge old commit into current head version

Got a file that has two commits of interest, both on the Master branch, both only modifying a single file foo: a previous commit AA, and the current version in HEAD. I would like to merge the two versions of the file, keeping bits of both, into HEAD on Master.

I did the simplest thing that I thought would work:

git checkout -b merge-purgatory AA
git commit -m "pulled foo back from previous commit for merging into HEAD."
git checkout master
git merge merge-purgatory

Which simply overwrites the current HEAD version of foo with AA version. Tried the more verbose git checkout -m as well, same result: a dumb overwrite.

How do I force git to treat the AA version of foo as a conflicting merge with the current HEAD version?

Upvotes: 23

Views: 42981

Answers (4)

Mahieddine M. Ichir
Mahieddine M. Ichir

Reputation: 603

I get rid of it using:

git checkout -f b855a13754fabf5cef6ff93ab00558608a839377 -- .

which forces the changes of the commit id into my current branch (master), then I created a new branch (checkout -b) from these applied changes and created a merge request from the latter.

None of the above worked for me :-(

Upvotes: 8

alexmogavero
alexmogavero

Reputation: 537

The previous answers do not keep the story of the change, so the fact that you used a previous commit to generate the new commit. The following procedure will keep this story.

git checkout -b merge-purgatory AA

here you need to slightly modify your file, for example you can add an empty line. Then

git commit "pulled foo back from previous commit for merging into HEAD."
git checkout master
git merge --no-commit merge-purgatory

In this way the merge will fail and then all you need to do is to solve the conflict. This worked for me.

Upvotes: 4

Mark Longair
Mark Longair

Reputation: 467031

If git's merge isn't doing what you want, you could do the following instead:

  1. Make sure that there are no uncommitted changes in the file foo, e.g. by making sure that git status is clean.
  2. Overwrite foo with the version from AA using: git show AA:foo > foo
  3. Selectively stage only the changes from foo that you want with: git add -p foo
  4. Discard all the other changes to foo with git checkout -- foo
  5. Commit the staged changes: git commit

Alternatively, if you'd rather use a graphical diff tool (such as meld), you could just do:

git show AA:foo > old-foo
meld old-foo foo

Upvotes: 19

VonC
VonC

Reputation: 1323343

git merge --no-commit merge-purgatory

would at least give you the opportunity to review:change the merge before committing it.
See also techniques proposed in "How do you merge selective files with git-merge?", based on cherry-picking or checkout.


Regarding forcing the manual merge, you could declare in a .gitatributes file, for that specific file, a merge policy set to unset.

Performing a three-way merge

merge

The attribute merge affects how three versions of a file is merged when a file-level merge is necessary during git merge, and other commands such as git revert and git cherry-pick.
Unset 

Take the version from the current branch as the tentative merge result, and declare that the merge has conflicts. This is suitable for binary files that does not have a well-defined merge semantics.

Upvotes: 2

Related Questions