Version Control
Git Repositories
Code Comparison
Repository Management
Developer Tools

Git diff between two repositories: compare files and history

Codemia Knowledge Hub ·

To compare the files in two repositories, run git diff --no-index on the two directories. To compare branches and commits, add one repository as a remote of the other, fetch it, and use git diff or git log against the fetched branch. Which you need depends on whether you care about the current code snapshot or about how the histories diverged.

Compare two working trees directly

git diff --no-index does not need shared history:

bash
git diff --no-index /path/to/repoA /path/to/repoB

It treats the directories as plain trees and shows added, removed and modified files. This suits unrelated repositories or cases where only the current file state matters. You get no commit-level insight, since you are comparing snapshots.

Add one repository as a remote when history matters

bash
git remote add other /path/to/other-repo
git fetch other

Run the fetch before comparing, then diff branches directly (double-check the branch names):

bash
git diff main other/main

Unlike a raw directory diff, this lets Git work with renames, binary files and exact commit references, and you can inspect divergence between branches rather than only final content.

List the commits unique to each side

Use the symmetric difference form with git log:

bash
git log --left-right --graph --oneline main...other/main

Commits shown on the left belong only to main, and commits on the right only to other/main. This tells you whether the repositories are truly out of sync or reached similar code through different commits.

Add --cherry-pick to hide commits that differ in hash but apply the same change:

bash
git log --left-right --cherry-pick --oneline main...other/main

Compare two patch series with git range-diff

When the repositories hold two competing versions of a branch series, compare the commit sequences:

bash
git range-diff origin/main...topic other/main...other-topic

This compares one sequence of commits with another, not files. It is a strong choice for rebased branches, reviewed patch stacks, or work ported across related repositories, where treating the histories as unrelated would hide how they correspond.

Content difference vs history difference

Repositories can differ in several ways at once. They may have identical files but different histories because of rebasing, share history but have different untracked local files, or be unrelated repositories with similar content.

  • Snapshot comparison: git diff --no-index, or compare fetched branch tips.
  • Commit divergence: git log with the main...other/main syntax.
  • Branch-series review: git range-diff.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions