Git Version Comparison: Practical Tips for Viewing Code Changes with the diff Command
The `git diff` command in Git is used to view code changes between versions, a fundamental and practical tool. It can compare differences between the working directory and the staging area, the staging area and historical commits, historical versions, and different branches. Core scenarios and corresponding commands are as follows: `git diff <filename>` for comparing the working directory with the staging area; `git diff --staged` for comparing the staging area with historical commits; `git diff <hash1> <hash2>` for comparing historical versions; and `git diff <branch1> <branch2>` for comparing different branches. Useful parameters include: `-w` to ignore whitespace changes, `--name-only` to only display filenames, `--stat` to show line modification statistics, and `--binary` to compare binary files. In the output, `+` indicates added content and `-` indicates deleted content. Mastering `diff` enables efficient management of code changes and helps avoid accidental commits.
Read More