When I type "git diff", I'd like to see a side-by-side diff, like with "diff -y", or like to display the diff in an interactive diff tool like "kdiff3". How can this be done?
相关问题
- Why does recursive submodule update from github fa
- Extended message for commit via Visual Studio Code
- Emacs shell: save commit message
- Can I organize Git submodules in a flat hierarchy?
- Upload file > 25 MB on Github
相关文章
- 请教Git如何克隆本地库?
- GitHub:Enterprise post-receive hook
- Git Clone Fails: Server Certificate Verification F
- SSIS solution on GIT?
- Is there a version control system abstraction for
- ssh: Could not resolve hostname git: Name or servi
- Cannot commit changes with gitextensions
- git: retry if http request failed
Several others already mentioned cdiff for git side-by-side diffing but no one gave a full implementation of it.
Setup cdiff:
Edit ~/.gitconfig inserting these lines:
The pager off is needed for cdiff to work with Diff, it is essentially a pager anyway so this is fine. Difftool will work regardless of these settings.
The show alias is needed because git show only supports external diff tools via argument.
The '#' at the end of the diff external command is important. Git's diff command appends a $@ (all available diff variables) to the diff command, but we only want the two filenames. So we call out those two explicitly with $2 and $5, and then hide the $@ behind a comment which would otherwise confuse sdiff. Resulting in an error that looks like:
Git commands that now produce side-by-side diffing:
Cdiff usage:
You now have side-by-side diff via git diff and difftool. And you have the cdiff python source code for power user customization should you need it.
Here's an approach. If you pipe through less, the xterm width is set to 80, which ain't so hot. But if you proceed the command with, e.g. COLS=210, you can utilize your expanded xterm.
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:
GIT_EXTERNAL_DIFF
and theGIT_DIFF_OPTS
environment variables.git config
See also:
git diff --help
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:
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
:you then need to make that script executable:
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:
Using
git config
use "git config" to define where your wrapper script can be found:
Editing your
~/.gitconfig
fileyou can edit your
~/.gitconfig
file to add these lines: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
then simply: