git-difftool - Linux
Overview
git difftool is a powerful tool for visually comparing and merging changes between two files or commits. It provides an intuitive interface to resolve conflicts and streamline code reviews.
Syntax
git difftool [options] [commit | branch]... [<file>...]
Options/Flags
- -y: Automatically accept all changes without prompting.
- -m: Enable interactive merging mode.
- –tool=
: Specify the difftool to use (e.g., meld, kdiff3). - –no-prompt: Suppress interactive prompting.
- –cached: Compare the working tree against the staged changes.
Examples
- Compare the current working tree with the latest commit:
git difftool HEAD
- Resolve conflicts between two branches:
git difftool branch1 branch2
- Use ‘meld’ as the difftool:
git difftool --tool=meld HEAD~1 HEAD
- Automatically accept all changes:
git difftool -y HEAD~2 HEAD
Common Issues
- Difftool not found: Make sure the desired difftool is installed and on the PATH.
- Merge conflicts: Use interactive merging mode (-m) to manually resolve conflicts.
- Incorrect comparison: Verify that the specified commit/branch and file arguments are correct.
Integration
- Git merge: Use
git difftool
to visualize and resolve conflicts during merge operations. - Commit hooks: Add
git difftool
to pre-commit or pre-push hooks to enforce code style or prevent errors. - External editors: Integrate
git difftool
with your preferred code editor for seamless diffing and merging.
Related Commands
- git mergetool: Invokes an external merge tool for resolving conflicts.
- git diff: Shows differences between files or commits in text format.
- git commit: Commits changes after resolving conflicts.