gitgit-mergemerge-conflict-resolution

During a Git merge conflict resolution, can I check out the conflicting state of a file?


When git merge has conflicts, the files that are in a conflicting state contain markers showing the differences.

Suppose that while trying to resolve a conflict, I've made changes, perhaps:

But then I have second thoughts, and I want to put the file back the way it was at the start of the merge, showing the conflict markers, and start over trying to resolve the conflicts.

I'm guessing I can run git checkout [something] somefile to get back to the conflicting state. What would it be?


Solution

  • Use the --merge option (-m in short form) for git checkout or git restore (both commands behave the same here)

    # the legacy way
    git checkout -m <path>
    
    # the (not so) new way
    git restore -m <path>
    

    With -m, changes made to the working tree file can be discarded to re-create the original conflicted merge result.