Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get my git merge conflicts into a diff-like format?

I'm merging two git changesets (or perhaps - in the process of git rebase'ing, which merges changesets) involving file foo, and have some merge conflicts. The file looks like so:

text appearing in all changesets
<<<<<<< HEAD
text added on the head
=======
something else added on another changeset
>>>>>>> 1babf8ed... commit comment here
more text appearing in all changesets
added text, no conflicts
yet more appearing in all changesets

My question:

For a specific file (like foo above) How can I display the conflicts contents so that the HEAD revision shows up as removed text, and the other changeset (in my example) shows up as added text? In other words, how can I make some tool treat the part between <<<<< and ===== as prefixed by - and the part between >>>>> and ===== as prefixed by +, or the other way around?

Notes:

  • The best thing would be just diff-like output to the standard output stream, because I could pipe that into another tool. But something with more tie-in to git's diff-related tooling is also relevant. I would rather not have to download some custom utility/app if I can reasonably avoid it.
  • A side-by-side display of the HEAD and other-commit versions is also useful
like image 280
einpoklum Avatar asked Nov 06 '22 22:11

einpoklum


2 Answers

Conflicts aren't diff as it is not additions on a side, and deletion on the other one.

That being said, tools like meld, kdiff3, or p4merge are able to display conflicts in "side by side" view to help you fix them.

Edit:
For example:

Given the situation created through this script:

#!/usr/bin/env bash

git init demoRepo
cd demoRepo
git commit --allow-empty -m "Init"
for i in {1..10}; do
  echo $i >> myFile.txt
  git commit -m "Add ${i} to my file" -- myFile.txt
done
git checkout -b branch2 HEAD~5
for i in A B C D E; do
  echo $i >> myFile.txt
  git commit -m "Add ${i} to my file" -- myFile.txt
done
git merge master

We have a conflict inside myFile.txt.

Set meld to be your mergetool with:

git config --global merge.tool meld
# Use `--local` if you want this setting to be only in current repository

Then call it with:

git mergetool
# You may also call it without (pre)defining the tool in config, like:
#   git mergetool --tool=meld

Meld should know display you conflict with a "side by side" view.

meld for conflict resolution

like image 51
mab Avatar answered Nov 15 '22 05:11

mab


Edit: git diff will show all diffs between the two revisions, which may include a lot of diffs that don't cause a merge conflict. If you want to show merge conflict diffs only, I don't have a solution yet.

If you're willing to see all diffs between the two revisions, you can simply use git diff. You'll need to pass it:

  • Treeish (eg, the SHA1 handle) of the commit on one side of the merge (to treat as "-")
  • Treeish of the commit on the other side of the merge (to treat as "+")
  • Optionally, the filename to diff
  • Optionally, -U999999 to show the entire file (by telling it you want that many lines of context around any difference)

Example: git diff a829c71 d98ef2a -U999999 example.py

like image 31
krubo Avatar answered Nov 15 '22 04:11

krubo