How can I get a side-by-side diff when I do "git diff"?
Try git difftool
Use git difftool
instead of git diff
. You'll never go back.
UPDATE to add an example usage:
Here is a link to another stackoverflow that talks about git difftool
: How do I view 'git diff' output with my preferred diff tool/ viewer?
For newer versions of git
, the difftool
command supports many external diff tools out-of-the-box. For example vimdiff
is auto supported and can be opened from the command line by:
cd /path/to/git/repo
git difftool --tool=vimdiff
Other supported external diff tools are listed via git difftool --tool-help
here is an example output:
'git difftool --tool=<tool>' may be set to one of the following:
araxis
kompare
vimdiff
vimdiff2
The following tools are valid, but not currently available:
bc3
codecompare
deltawalker
diffuse
ecmerge
emerge
gvimdiff
gvimdiff2
kdiff3
meld
opendiff
tkdiff
xxdiff
Although Git has an internal implementation of diff, you can set up an external tool instead.
There are two different ways to specify an external diff tool:
- setting the
GIT_EXTERNAL_DIFF
and theGIT_DIFF_OPTS
environment variables. - configuring the external diff tool via
git config
ymattw
's answer is also pretty neat, using ydiff
See also:
- https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
git diff --help
- http://www.pixelbeat.org/programming/diffs/
When doing a git diff
, Git checks both the settings of above environment variables and its .gitconfig
file.
By default, Git passes the following seven arguments to the diff program:
path old-file old-hex old-mode new-file new-hex new-mode
You typically only need the old-file and new-file parameters. Of course most diff tools only take two file names as an argument. This means that you need to write a small wrapper-script, which takes the arguments which Git provides to the script, and hands them on to the external git program of your choice.
Let's say you put your wrapper-script under ~/scripts/my_diff.sh
:
#!/bin/bash
# un-comment one diff tool you'd like to use
# side-by-side diff with custom options:
# /usr/bin/sdiff -w200 -l "$2" "$5"
# using kdiff3 as the side-by-side diff:
# /usr/bin/kdiff3 "$2" "$5"
# using Meld
/usr/bin/meld "$2" "$5"
# using VIM
# /usr/bin/vim -d "$2" "$5"
you then need to make that script executable:
chmod a+x ~/scripts/my_diff.sh
you then need to tell Git how and where to find your custom diff wrapper script. You have three choices how to do that: (I prefer editing the .gitconfig file)
-
Using
GIT_EXTERNAL_DIFF
,GIT_DIFF_OPTS
e.g. in your .bashrc or .bash_profile file you can set:
GIT_EXTERNAL_DIFF=$HOME/scripts/my_diff.sh export GIT_EXTERNAL_DIFF
-
Using
git config
use "git config" to define where your wrapper script can be found:
git config --global diff.external ~/scripts/my_diff.sh
-
Editing your
~/.gitconfig
fileyou can edit your
~/.gitconfig
file to add these lines:[diff] external = ~/scripts/my_diff.sh
Note:
Similarly to installing your custom diff tool, you can also install a custom merge-tool, which could be a visual merging tool to better help visualizing the merge. (see the progit.org page)
See: http://fredpalma.com/518/visual-diff-and-merge-tool/ and https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
You can also try git diff --word-diff
.
It's not exactly side-by-side, but somehow better, so you might prefer it to your actual side-by-side need.
ydiff
Formerly called cdiff
, this tool can display side by side, incremental, and colorful diff.
Instead of doing git diff
, do:
ydiff -s -w0
This will launch ydiff
in side-by-side display mode for each of the files with differences.
Install with:
python3 -m pip install --user ydiff
-or-
brew install ydiff
For git log
, you can use:
ydiff -ls -w0
-w0
auto-detects your terminal width. See the ydiff
GitHub repository page for detail and demo.
Tested in Git 2.18.0, ydiff 1.1.
You can do a side-by-side diff
using sdiff
as follows:
$ git difftool -y -x sdiff HEAD^ | less
where HEAD^
is an example that you should replace with whatever you want to diff against.
I found this solution here where there are a couple of other suggestions also. However, this one answer's the OP's question succinctly and clearly.
See the man git-difftool for an explanation of the arguments.
Taking the comments on board, you can create a handy git sdiff
command by writing the following executable script:
#!/bin/sh
git difftool -y -x "sdiff -w $(tput cols)" "${@}" | less
Save it as /usr/bin/git-sdiff
and chmod +x
it. Then you'll be able to do this:
$ git sdiff HEAD^
Extra Tip
As suggested in comments you can use icdiff
to do what sdiff
does with colored output:
$ more /usr/bin/git-sdiff
#!/bin/sh
git difftool -y -x "icdiff --cols=$(tput cols)" "${@}" | less