Comparing Git branches before a merge shows which files and lines differ between the target branch and the work being reviewed. This catches unrelated changes, missing files, and review-scope drift before a pull request, release merge, or handoff.
The command git diff compares tree states. The two-dot form, such as main..feature/docs, compares the current tips of both branches, while the three-dot form, such as main...feature/docs, compares the merge base with the second branch.
Use branch names or remote-tracking refs that exist in the repository. Fetch remote refs before comparing them, and remember that committed branch comparisons do not include uncommitted working-tree edits.
Related: How to merge a Git branch into main
Related: How to view Git commit history
Steps to compare two Git branches with diff:
- Confirm that both branch references exist in the repository.
$ git branch --list main feature/docs feature/docs * main
Replace main and feature/docs with the branch names being compared. Use remote-tracking names such as origin/main after fetching from the remote.
- Compare the current branch tips when the exact end-state difference matters.
$ git diff --name-status main..feature/docs M docs/guide.md A docs/release-notes.md D docs/security.md
git diff main..feature/docs is the same branch-tip comparison as git diff main feature/docs. The D row means the file exists at the tip of main but not at the tip of feature/docs.
- Compare the feature branch from its merge base with the target branch.
$ git diff --name-status main...feature/docs M docs/guide.md A docs/release-notes.md
Use the three-dot form for the usual review view of what feature/docs introduced since it split from main.
- Print a compact summary of the reviewed branch changes.
$ git diff --stat main...feature/docs docs/guide.md | 3 ++- docs/release-notes.md | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-)
- Review the full patch for the branch or a specific path.
$ git diff main...feature/docs -- docs/guide.md diff --git a/docs/guide.md b/docs/guide.md index 073e1ba..692f29f 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -1,3 +1,4 @@ Title: Deployment guide -Status: draft +Status: reviewed Owner: platform +Review checklist: compare branches before merge
The -- separator tells Git that the remaining argument is a path, which helps when a file name could also be parsed as a revision.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.