Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Merge old commit into current head version

Tags:

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?

like image 706
Noel Avatar asked Jan 16 '12 08:01

Noel


People also ask

Can I revert merge commit?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

Does git merge overwrite?

Usually git does not overwrite anything during merge.


1 Answers

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
like image 127
Mark Longair Avatar answered Sep 21 '22 09:09

Mark Longair