mmr
mmr

Reputation: 14929

How do I recover from overwriting a merge file in git?

I just made a mistake with my git repo (stupid programming while Dayquil'd). My steps were:

  1. start editing file.txt
  2. did a "git pull" on the repo containing file.txt. File.txt was part of a merge conflict.
  3. did some edits on the version of file.txt I opened in step 1.
  4. saved the file I opened in step 1, overwriting the merge.
  5. ???

I need to resolve the merge from step 2, and then merge that result with the edits made in step 2. How can I do this?

Upvotes: 1

Views: 1119

Answers (2)

Lily Ballard
Lily Ballard

Reputation: 185851

It sounds like your problem is simply that you overwrote the file and lost the merge conflict markers. The simplest way to deal with this is to use git mergetool, which will use a GUI merge tool to deal with conflicts. This tool doesn't rely on the working copy version of the file with conflict markers, it actually re-merges the original files using the GUI tool of your choice. I highly recommend it as your normal go-to solution for merge conflicts.

If you really do want the file back with the conflict markers, you can just use git checkout -m -- file.txt. This will re-create the merge conflict version of the file. Note that any changes you made in steps 3 and 4 will be lost.

If you want to save the local changes you made, you can use git diff :2:file.txt file.txt to view a diff between your pre-merge copy of file.txt and the copy you saved to disk. You can pipe this to a file in order to use later to re-create your changes after you solve the merge, or even use git apply with the file to apply it as a patch (as long as it doesn't touch any hunks that the merge changed).

Upvotes: 3

manojlds
manojlds

Reputation: 301577

Try doing:

git checkout --merge -- file.txt

Upvotes: 1

Related Questions