To compare the committed contents of two Git branches, give both names to git diff:
git diff main feature/login --
You can run this while staying on your current branch. The comparison uses the commits named by main and feature/login, rather than your current working files. The git diff reference documents this two-commit form.
Check which branches you are comparing
git status --short
git branch --show-current
git branch --list main feature/login
Replace the example names with branches that exist in your repository. The branch listing lets you check the names before running the comparison. As the git branch documentation explains, --show-current prints your current branch; it prints nothing when HEAD is detached.
A dirty working tree does not require a branch switch for this comparison. Keep the initial status output as a record of any staged, unstaged or untracked work. The status reference describes those separate categories.
Compare the two branch tips
Start with a summary, then inspect the patch for a particular path:
git diff --stat main feature/login --
git diff main feature/login -- settings.txt
Here, main is the before side and feature/login is the after side. Minus lines belong to the first snapshot; plus lines belong to the second. The two-dot spelling, git diff main..feature/login --, makes the same endpoint comparison.
Consider this small history. A is the shared starting commit. B adds a line to README.md on main. C changes color=blue to color=green in settings.txt on feature/login.
B main
/
A---+
\
C feature/login
In the verified practice repository, this command listed two paths:
git diff --name-only main feature/login --
README.md
settings.txt
README.md appears because B and C contain different versions of it: C does not contain B's added line. That output does not mean the feature commit edited both files. The command is comparing snapshots, not listing commits.
Show the feature side since the shared ancestor
For the changes between the shared starting point and the feature tip, use three dots:
git diff --name-only main...feature/login --
settings.txt
In this example, that compares A with C, leaving out B's later README edit. You can inspect the common ancestor with:
git merge-base main feature/login
The merge-base reference explains how Git finds a shared ancestor. The history here has one merge base; more complicated histories can have several.
Argument order matters. In this same repository, git diff --name-only feature/login...main -- lists README.md because the comparison now follows A to B. Three dots are not simply a shorter way to request the endpoint diff.
What happened to the uncommitted work?
The command checks used a staged red version of settings.txt, an unstaged purple version, and an untracked scratch.txt. The committed branch comparison still showed blue changing to green. After all comparisons, the current branch, HEAD, index contents and both local files matched their initial state.
That result is specific to the branch-to-branch commands above. Plain git diff examines a different comparison. If you need to change what is staged, the guide to unstaging a file while keeping its edits covers that operation separately.
These commands inspect references already present locally. They do not contact a remote server or guarantee that a branch name reflects its latest remote state. If a name is missing, check the repository and available branches before trying a different command.
No comments:
Post a Comment