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:
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
Run the fetch before comparing, then diff branches directly (double-check the branch names):
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:
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:
Compare two patch series with git range-diff
When the repositories hold two competing versions of a branch series, compare the commit sequences:
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 logwith themain...other/mainsyntax. - Branch-series review:
git range-diff.
Related reading
- git-checkout older revision of a file under a new name
- Git-svn create push a new branch/tag?
- Git - Difference Between 'assume-unchanged' and 'skip-worktree
- Git - fatal Unable to create '/path/my_project/.git/index.lock' File exists
- Git - How to undo a checkout of unstaged files which discards local changes
- Git - Ignore files during merge
- Git - Ignore node_modules folder everywhere
- git - pulling from specific branch
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.