Getting the difference between two repositories

GitGit Diff

Git Problem Overview


How can we get the difference between two git repositories?

The scenario: We have a repo_a and repo_b. The latter was created as a copy of repo_a. There have been parallel development in both the repositories afterwards. Is there a way we can list the differences of the current versions of these two repositories?

Git Solutions


Solution 1 - Git

In repo_a:

git remote add -f b path/to/repo_b.git
git remote update
git diff master remotes/b/master
git remote rm b

Solution 2 - Git

Meld can compare directories:

meld directory1 directory2

Just use the directories of the two git repos and you will get a nice graphical comparison:

enter image description here

When you click on one of the blue items, you can see what changed.

Solution 3 - Git

You can add other repo first as a remote to your current repo:

git remote add other_name PATH_TO_OTHER_REPO

then fetch brach from that remote:

git fetch other_name branch_name:branch_name

this creates that branch as a new branch in your current repo, then you can diff that branch with any of your branches, for example, to compare current branch against new branch(branch_name):

git diff branch_name

Solution 4 - Git

Once you have both branches in one repository you can do a git diff. And getting them in one repository is as easy as

git fetch /the/other/repo/.git refs/heads/*:refs/remotes/other/*

Solution 5 - Git

git diff master remotes/b

That's incorrect. remotes/b is a remote, but not a branch.

To get it to work, I had to do:

git diff master remotes/b/master

Solution 6 - Git

I use PyCharm which has great capabilities to compare between folders and files.

Just open the parent folder for both repos and wait until it indexes. Then you can use right click on a folder or file and Compare to... and pick the corresponding folder / file on the other side.

It shows not only what files are different but also their content. Much easier than command line.

Solution 7 - Git

See http://git.or.cz/gitwiki/GitTips, section "How to compare two local repositories" in "General".

In short you are using GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable to have access to object database of the other repository, and using git rev-parse with --git-dir / GIT_DIR to convert symbolic name in other repository to SHA-1 identifier.

Modern version would look something like this (assuming that you are in 'repo_a'):

GIT_ALTERNATE_OBJECT_DIRECTORIES=../repo_b/.git/objects 
git diff $(git --git-dir=../repo_b/.git rev-parse --verify HEAD) HEAD
where ../repo_b/.git is path to object database in repo_b (it would be repo_b.git if it were bare repository). Of course you can compare arbitrary versions, not only HEADs.


Note that if repo_a and repo_b are the same repository, it might make more sense to put both of them in the same repository, either using "git remote add -f ..." to create nickname(s) for repository for repeated updates, or obe off "git fetch ..."; as described in other responses.

Solution 8 - Git

An easy way to do that without touching your remotes config. From repo A, in master (assume you want to compare master branches):

git fetch path/to/repo_b.git master
git diff FETCH_HEAD

Solution 9 - Git

You can use the following command:

diff -x .git -r repo-A repo-B

or for the side by side you can use:

diff -x .git -W200 -y -r repo-A repo-B

In case of Colorizing every diff file, you can use:

diff -x .git -W200 -y -r repo-A repo-B | sed -e "s/\(^diff .*\)/\x1b[31m\1\x1b[0m/"

Solution 10 - Git

Your best bet is to have both repos on your local machine and use the linux diff command with the two directories as parameters:

diff -r repo-A repo-B

Solution 11 - Git

Reminder to self... fetch first, else the repository has not local hash (I guess).

step 1. Setup the upstream remote and above^

diffing a single file follows this pattern :

git diff localBranch uptreamBranch --spacepath/singlefile

git diff master upstream/nameofrepo -- src/index.js

Solution 12 - Git

To follow up @iamamac answer to have a nice summary after this:

git remote add -f b path/to/repo_b.git
git remote update

I would use diff-tree:

git diff-tree master remotes/b/master --compact-summary

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsanjayavView Question on Stackoverflow
Solution 1 - GitiamamacView Answer on Stackoverflow
Solution 2 - GitMartin ThomaView Answer on Stackoverflow
Solution 3 - GitM.KouchiView Answer on Stackoverflow
Solution 4 - GitMichael Krelin - hackerView Answer on Stackoverflow
Solution 5 - GitJamesView Answer on Stackoverflow
Solution 6 - GitMrEView Answer on Stackoverflow
Solution 7 - GitJakub NarębskiView Answer on Stackoverflow
Solution 8 - Gitjorisv92View Answer on Stackoverflow
Solution 9 - GitGerardView Answer on Stackoverflow
Solution 10 - GitAndrew HeekinView Answer on Stackoverflow
Solution 11 - GitHillsieView Answer on Stackoverflow
Solution 12 - GitPrzemek NowickiView Answer on Stackoverflow