English | 简体中文
gcmp (Git Compare) is a powerful command-line script that leverages Beyond Compare (or other graphical diff tools) to visually compare any two "areas" within a Git repository. It seamlessly combines the flexibility of git diff with the user-friendliness of a GUI tool, dramatically improving the efficiency of code reviews and change analysis.
- Intuitive Keywords: Use simple keywords like
work,stage, andstashto represent the working directory, staging area, and the latest stash. - Full-Spectrum Comparison: Supports comparison between the Working Directory, Staging Area, Stash, and any commit, branch, or tag.
- Live Editing & Performance: When comparing the
workdirectory,gcmpuses your actual project folder. This allows you to edit files directly in your diff tool and enjoy instant comparisons without any copying overhead. - Smart Auto-Upgrading: The script is smart! If you provide a reference like
HEADor a branch name, and it matches your clean working directory,gcmpautomatically uses the live directory for comparison. You get the performance and editing benefits without explicitly typingwork. - Cross-Platform Support: Automatically detects OS type and configures the appropriate comparison tool, defaulting to Beyond Compare.
- Highly Customizable: Easily switch the default
bcompareto your favorite diff tool (likekdiff3,meld,vscode, etc.). - Safe and Reliable: References that don't qualify for live comparison are safely exported to temporary directories that are automatically cleaned up on exit, protecting your repository's state.
Standard git diff is powerful, but its terminal-based, file-by-file output can make it difficult to grasp the big picture for large or scattered changes.
gcmp solves the following pain points:
- The Big Picture: Want to know exactly which files were changed, added, or deleted in
feature-Acompared tomain?gcmppresents this clearly as a directory tree. - Simplified Complex Comparisons: An operation like
git diff work stagedoesn't exist natively. Butgcmp work stagemakes it trivial, clearly showing which changes in your working directory have not yet been staged. - GUI Advantage: For complex changes within a single file, a GUI tool's side-by-side scrolling, syntax highlighting, and inline diffs are far more intuitive than the
+and-lines in a terminal.
We recommend placing the gcmp script in your system's PATH for easy access from any repository.
- Download
gcmp. - Give it execute permissions in your terminal:
chmod +x gcmp. - Move this file to a directory included in your
PATHenvironment variable, such as~/binor/usr/local/bin.
gcmp [repo_path] <ref1> <ref2>
repo_path(Optional): The path to the Git repository. If omitted, the current directory is used.<ref1>&<ref2>: The two Git references or keywords to compare.
This is the core magic of gcmp, making complex comparisons simple.
| Keyword | Represents Git Area | Description |
|---|---|---|
work |
Working Directory | All tracked files as they currently exist. This is a live view¡ªchanges you make in the diff tool are saved directly to your files! |
stage |
Staging Area (Index) | Content that has been added with git add for the next commit. |
stash |
The Latest Stash | Represents stash@{0}, your most recent stash. |
In addition to these keywords, <ref1> and <ref2> can be any reference Git understands (a "commit-ish"), such as:
- Commit HASH:
abc123d,7a4e3f81 - Branch Name:
main,my-feature - Tag Name:
v1.0.2 - Relative Ref:
HEAD,HEAD~2,main~1 - Other Stashes:
'stash@{1}'(note the quotes)
See what changes haven't been staged yet
gcmp work stageIt answers: "What changes will be added to the index if I run git add . right now?"
Pro-Tip: You can edit your files on the work side of the comparison, save them, and they are immediately updated in your actual working directory, ready for you to git add.
Preview what you are about to commit
gcmp stage HEADIt answers: "What will the snapshot look like if I run git commit right now?" (This is the graphical equivalent of git diff --staged)
Pro-Tip: On a clean repository, you can run gcmp HEAD HEAD~1. gcmp will automatically use your live working directory for HEAD, giving you the fastest possible comparison.
See all local changes since the last commit (both staged and unstaged)
gcmp work HEADIt answers: "What have I changed since my last commit?" (This is the graphical equivalent of git diff HEAD)
Decide whether to pop or apply a stash
gcmp stash HEADIt answers: "How does my stashed work differ from the current state of my branch? Will applying it cause major conflicts?"
Essential for code reviews or pre-merge checks
gcmp main feature-branchIt answers: "What changes has feature-branch introduced compared to main?"
gcmp work v1.2.0It answers: "How does my current work differ from the v1.2.0 release?"
Useful for debugging a code regression
gcmp abc123d def456agcmp ~/projects/my-app main developBy default, gcmp uses bcompare. It now also automatically detects your operating system to configure the appropriate comparison tool. You can change this to your preferred tool by editing the DIFF_TOOL variable at the top of the script.
# gcmp.sh
# --- Global Configuration ---
# You can change this to your favorite diff tool command
DIFF_TOOL="bcompare" Some popular alternatives:
- Visual Studio Code:
DIFF_TOOL="code --diff" - KDiff3:
DIFF_TOOL="kdiff3" - Meld:
DIFF_TOOL="meld" - P4Merge:
DIFF_TOOL="p4merge"
Ensure your chosen tool's command is in your system PATH and that it supports receiving two directory paths as arguments for comparison.
- Parse Arguments: The script first determines the repository path and the two references (
ref1andref2) from the command-line arguments. - Prepare Comparison Environment: It creates a unique temporary parent directory and sets a
trapcommand to ensure this directory is automatically deleted when the script exits. - Intelligently Resolve Sources: For each of the two references, the script decides the best way to present it:
- Live Directory: If the reference is the
workkeyword, OR if it's a reference (likeHEAD) that matches a clean working directory, the script uses the direct path to your repository. This is the fastest method and enables live editing. - Temporary Snapshot: If a reference doesn't qualify for live comparison (e.g., it's an old commit,
stage, orstash), it is exported to a temporary subdirectory using the most appropriate Git command (git checkout-indexforstage,git archivefor others).
- Live Directory: If the reference is the
- Launch Diff Tool: Before launching, it performs a final check to ensure the two resolved paths are not identical (preventing self-comparison). It then passes the paths for each side to your configured
DIFF_TOOL, using a-waitflag to pause until you close the tool. - Automatic Cleanup: Once you close the diff tool, the script resumes, and the
trapcommand is triggered on exit, completely removing any temporary directories created during the process.
bash(v4.0+ recommended)git- A graphical directory comparison tool (e.g., Beyond Compare, VS Code, Meld, etc.) with its executable in your system
PATH.
This project is licensed under the MIT License.