Version Control
Git Repositories
Code Comparison
Repository Management
Developer Tools

Getting the difference between two repositories

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Comparing two repositories can mean two different things: comparing their file contents or comparing their commit histories. The right command depends on whether the repositories share history, whether they are local directories, and whether you care about commit-level divergence or just the final code snapshot. In Git, it is usually best to make that distinction explicit before you start diffing.

Compare Two Working Trees Directly

If you simply want to know how two directories differ on disk, git diff --no-index is the quickest option. It does not require a shared repository history.

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

This treats the directories as plain trees and shows added, removed, and modified files. It is useful when the repositories are unrelated or when you only care about the current file state.

The limitation is that you get no commit-level insight. You are comparing snapshots, not development history.

Add One Repository as a Remote When Histories Matter

If the repositories are related, or at least need to be compared using Git objects and references, add one as a remote to the other and fetch it.

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

Now you can compare branches directly.

bash
git diff main other/main

This is more useful than a raw directory diff when you want Git to understand renames, binary files, and exact commit references. It also lets you inspect divergence between branches rather than only final file content.

Compare Histories, Not Just Files

When you want to know which commits exist in one line of development but not the other, 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. Commits on the right belong only to other/main. This view is often more informative than a file diff because it tells you whether the repositories are truly out of sync or simply arrived at similar code through different commits.

For patch-equivalent histories, add --cherry-pick to suppress commits that differ in hash but apply the same change.

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

Use git range-diff for Patch-Series Review

If the repositories contain two competing versions of a branch series, git range-diff is extremely useful.

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

This is not a generic file diff. It is designed to compare one sequence of commits with another sequence of commits. That makes it a strong choice for rebased branches, reviewed patch stacks, or ported work across related repositories.

Be Precise About What You Mean by "Difference"

Two repositories can differ in several ways at once. They may have identical files but different commit histories because of rebasing. They may share history but have different untracked files locally. They may also be unrelated repositories with similar content.

That is why there is no single best command. For snapshot comparison, use git diff --no-index or compare fetched branch tips. For commit divergence, use git log with the symmetric difference syntax. For branch-series review, use git range-diff.

The clearer the question, the more useful the result.

Common Pitfalls

  • Using a file diff when the real question is which commits diverged.
  • Assuming two repositories must share history before Git can compare their directory contents.
  • Adding a remote and diffing the wrong branch names.
  • Forgetting to run git fetch before comparing against the newly added remote.
  • Treating rebased histories as unrelated when git range-diff would show the relationship clearly.

Summary

  • Decide first whether you need a content diff or a history diff.
  • Use git diff --no-index for unrelated directories or snapshot-only comparisons.
  • Add one repository as a remote when you want branch and commit-aware comparisons.
  • Use git log main...other/main to see commits unique to each side.
  • Use git range-diff when comparing two patch series or rebased branches.

Course illustration
Course illustration

All Rights Reserved.