Git - Diff And Merge - Settings and Tools

These are various tools that we can use when viewing the differences and to resolve merge conflicts. This takes a little bit of set-up. If using the terminal or the default tools for viewing diffs is not comfortable for you, these may help. If you cannot resolve the merge conflicts either manually or using other tools like vim-fugitive, these tools may help.

Checking status and differences for files

command description
git diff filename.txt command to show changes in a specific file. command to see what you haven’t “git add"ed yet
git diff --word-diff myfile.txt Instead of showing you two lines for each change, Git allows you to highlight changes in a line explicitly. (The result is usually colored nicely, the removed part being red and the added text green.)
git diff --cached myfile.txt command to see already “-add” ed changes
git status -u Show untracked files

See the differences between two branches

https://stackoverflow.com/questions/9834689/how-can-i-see-the-differences-between-two-branches

To show the difference between commits

git diff main remotes/origin/main

How to see file differences (Not commit differences)? TODO

diff settings

diff on long lines

When doing a diff on a long line, this can be very helpful but you’ll still get a less-like scrolling output that can be unhandy to use. You maybe just want the diff put into your terminal:

PAGER='' git diff --word-diff myfile.txt

colorMoved setting

This configuration adds extra colors when running git diff to show blocks of lines that remain unchanged but have moved in the file.

git config --global diff.colorMoved zebra

By default, the diff expresses the changes as additions and deletions, with green and red denoting the operation done to a line. The additional colors help differentiate actual changes and lines moving around due to those changes.

Since turning on this configuration, I’ve found diffs much easier to read. I’m able to focus on the lines of code that actually changed and brush over blocks of code that are just moving around. Diffs are much more meaningful when I have a way to focus on what really changes.

How to see the file differences between a local branch and a remote branch

Needless to say, before doing this, fetch all the updates from the remote first.

git diff <local branch> <remote branch>

diff-so-fancy

A tool you can tell git to use to render diffs: https://github.com/so-fancy/diff-so-fancy

It makes git diff alot easier to read on the command line.

To use kdiff3 as the diff and merge tool with Git

GIT needs to know that KDiff3 should be used as the preferred diff/merge tool. For this, we need to make a simple change in the .gitconfig file. This file can be found under your home directory.

Open the .gitconfig file in your favorite text editor. Add the following lines to the file :

[merge]
        tool = kdiff3
[diff]
        tool = kdiff3
[mergetool "kdiff3"]
    path = C:/Program Files/KDiff3/kdiff3.exe
    keepBackup = false
    trustExitCode = false

[difftool "kdiff3"]
    path = C:/Program Files/KDiff3/kdiff3.exe
    keepBackup = false
    trustExitCode = false

The path config property under the mergetool and difftool, should point to the installation path of KDiff3 tool on your machine.

NOTE: please use forward slash “/” as the path separator even on windows machines. Using back slash “\” will not work! The above config tells GIT to use the KDiff3 tool as the external diff/merge tool.

From git bash, type this command to launch kdiff3 : ‘git difftool’ GIT will launch the KDiff3 for all the files that have changes since the last commit.

If you feel annoyed about GIT asking your permission for showing the KDiff3 for each changed file, use the following command : ‘git difftool -y’

To use KDiff as the merge tool use the following command : ‘git mergetool’ Hitting enter will launch the KDiff3 as the merge tool.

KDiff3 shows nice GUI to do the merge easily. It shows the original file on the leftmost window called “A” or “Base”, local file in the middle called “B” or “Local” and remote file in the rightmost window called “C” or “Remote”.

To use DiffMerge as the diff and merge tool with Git

If you want to use DiffMerge as the diff and merge tool with git, this is how the git confile should look like :

[merge]
        tool = DiffMerge
[diff]
        tool = DiffMerge
[mergetool "DiffMerge"]
    path = C:/Program Files/SourceGear/Common/DiffMerge/sgdm.exe
    keepBackup = false
    trustExitCode = false

[difftool "DiffMerge"]
    path = C:/Program Files/SourceGear/Common/DiffMerge/sgdm.exe
    keepBackup = false
    trustExitCode = false

To use diffuse as the diff and merge tool with Git

To use diffuse as the merge tool and kdiff3 as the diff tool :

[merge]
        tool = diffuse
[diff]
        tool = kdiff3
[mergetool "diffuse"]
    path = C:/Program Files (x86)/Diffuse/diffuse.exe
    keepBackup = false
    trustExitCode = false
[difftool "kdiff3"]
    path = C:/Program Files/KDiff3/kdiff3.exe
    keepBackup = false
    trustExitCode = false

Links to this note