- Use git diff to view unstaged changes in the working directory, or git diff --cached (or --staged) to see changes staged for the next commit. 2. To compare a specific file between two commits, run git diff <commit1> <commit2> -- <filename>, such as git diff HEAD~3 HEAD -- app.js, or use branch names like git diff main feature/auth -- config/routes.rb. 3. Compare all changes between two branches with git diff main..feature or git diff main feature, where the order determines the direction of change—from the first argument to the second. 4. Enhance output with options: --color-words for word-level differences within lines, --stat for a summary of insertions and deletions per file, --name-only to list only changed filenames, and --summary to include info on new, deleted, or renamed files. 5. For advanced comparisons, use revision syntax like HEAD~n for past commits, main@{yesterday} for reflog-based references, or tags like v1.0.0 to compare releases, for example, git diff HEAD~2..HEAD~1 to see changes in a specific commit or git diff v1.0.0 v1.1.0 -- src/ to compare directories across versions. Using git diff effectively provides precise, scriptable insight into changes, enabling accurate reviews and clean version control without relying on a GUI.
Comparing file versions is one of the most common tasks when working with Git, and git diff
is the go-to command for spotting changes between commits, branches, or working states. Whether you're reviewing your own changes before committing or checking what differs between two branches, understanding how to use git diff
effectively can save time and prevent errors.
Here’s how to use git diff
to compare file versions in practical scenarios.
1. View Changes in the Working Directory
If you’ve made edits to a file but haven’t staged them yet, use:
git diff
This shows unstaged changes — differences between your working directory and the staging area. It’s useful for reviewing what you’ve changed before running git add
.
For example, if you modified
index.js
but didn’t stage it,git diff
will show line-by-line additions (in green) and deletions (in red).
To see staged changes (what will be included in the next commit), use:
git diff --cached
or
git diff --staged
This compares the staging area to the latest commit.
2. Compare Specific Files Between Commits
To see how a particular file has changed between two commits:
git diff <commit1> <commit2> -- <filename>
For example:
git diff HEAD~3 HEAD -- app.js
This shows changes to app.js
from three commits ago to the current HEAD
.
You can also use branch names:
git diff main feature/auth -- config/routes.rb
This compares routes.rb
between the main
and feature/auth
branches.
3. Compare Branches or Commits
To see all changes between two branches:
git diff main..feature
or simply:
git diff main feature
This outputs all file changes between the tips of main
and feature
. It's helpful before merging to understand what’s being introduced.
Note: The order matters.
git diff A B
shows what needs to be added to A to get B — i.e., changes from A to B.
4. Useful Options for Better Output
--color-words
: Shows word-level changes within lines, not just whole-line diffs.git diff --color-words
--stat
: Summarizes changes per file, showing how many insertions/deletions.git diff --stat HEAD~5 HEAD
-p
or no flag: Default patch view, shows context and actual code changes.--name-only
: Lists only filenames that differ.git diff --name-only main feature
--summary
: Adds info about new, deleted, or renamed files.
5. Compare Specific Ranges (Advanced)
You can use special revision syntax:
HEAD~1
: One commit before HEADHEAD~5
: Five commits backmain@{yesterday}
: Wheremain
pointed yesterday (reflog-based)
Example:
git diff HEAD~2..HEAD~1
Shows changes introduced in the second-to-last commit.
Or use tags:
git diff v1.0.0 v1.1.0 -- src/
Compares all files in src/
between two releases.
Using git diff
smartly helps you catch bugs early, understand team changes, and ensure clean commits. You don’t need a GUI — the command line gives you precise, scriptable control over version comparisons.
Basically, if something changed, git diff
can show you exactly what — no guesswork needed.
The above is the detailed content of Comparing File Versions with Git Diff. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Packfile is an efficient mechanism used by Git to package, compress and transfer repository objects. When you execute gitpush, gitfetch or gitclone, what Git actually transmits is the packfile; 1. It is initially generated by loose objects through gitgc or gitrepack commands and stored in the .git/objects/pack/ directory; 2. The packfile not only contains object data, but also records the delta relationship between objects, and achieves rapid search with index file (.idx). 3. This design reduces the transmission volume and improves synchronization efficiency; 4. A large number of small packfiles may affect performance, and can be used through gitgc or git

To view Git commit history, use the gitlog command. 1. The basic usage is gitlog, which can display the submission hash, author, date and submission information; 2. Use gitlog--oneline to obtain a concise view; 3. Filter by author or submission information through --author and --grep; 4. Add -p to view code changes, --stat to view change statistics; 5. Use --graph and --all to view branch history, or use visualization tools such as GitKraken and VSCode.

To delete a Git branch, first make sure it has been merged or no retention is required. Use gitbranch-d to delete the local merged branch. If you need to force delete unmerged branches, use the -D parameter. Remote branch deletion uses the gitpushorigin-deletebranch-name command, and can synchronize other people's local repositories through gitfetch-prune. 1. To delete the local branch, you need to confirm whether it has been merged; 2. To delete the remote branch, you need to use the --delete parameter; 3. After deletion, you should verify whether the branch is successfully removed; 4. Communicate with the team to avoid accidentally deleting shared branches; 5. Clean useless branches regularly to keep the warehouse clean.

ToswitchGitbranches,firstupdatethelocalrepowithgitfetch,checkexistingbrancheswithgitbranchcommands,thenusegitcheckoutorgitswitchtochangebranches,handlinguncommittedchangesbycommitting,stashing,ordiscardingthem.WhenswitchingGitbranches,ensureyourlocal

To discard the modifications in the Git working directory and return to the state of the last commit, 1. For the modifications of the tracked files, use gitcheckout-- or gitcheckout--. Discard all modifications; 2. For new files that are not tracked, use gitclean-f to delete the files. If the directory is included, use gitclean-fd. Before execution, use gitclean-fd to preview the delete content; 3. If you need to reset all changes (including the temporary storage area and the working directory), use gitreset-hard. This command will reset the working directory and the temporary storage area. Be sure to operate with caution. These methods can be used individually or in combination to achieve the purpose of cleaning up the working directory.

To add a subtree to a Git repository, first add the remote repository and get its history, then merge it into a subdirectory using the gitmerge and gitread-tree commands. The steps are as follows: 1. Use the gitremoteadd-f command to add a remote repository; 2. Run gitmerge-srecursive-no-commit to get branch content; 3. Use gitread-tree--prefix= to specify the directory to merge the project as a subtree; 4. Submit changes to complete the addition; 5. When updating, gitfetch first and repeat the merging and steps to submit the update. This method keeps the external project history complete and easy to maintain.

Git hooks are used to automatically run scripts before and after commits, pushes and other operations to execute tasks. Specific uses include: 1. Run code checks or tests before submission; 2. Forced submission information format; 3. Send notifications after push. They help unify team specifications and reduce manual steps, such as preventing submissions when tests fail. Git hooks are located in the .git/hooks/ directory in the repository and are not shared by default. They need to be copied manually or used tools such as Husky for team collaboration. Writing a basic hook requires creating an executable file and naming the corresponding event, such as pre-commit, and writing logical judgments there to block or allow operations.

Soundstageafafileiititwittingchatcase, USEGITIZEADTORDOREMEVOME FROMARNINGAREAILACT.TOUNDACT Rungit Reset.ForPartialStialing, Usgit rests-PtointelavEevstehuncificisshunissehunissue
