How to diff two local repositories

From man git diff (on Linux Ubuntu 20.04):

git diff [<options>] --no-index [--] <path> <path>
    This form is to compare the given two paths on the
    filesystem. You can omit the --no-index option when
    running the command in a working tree controlled by Git
    and at least one of the paths points outside the
    working tree, or when running the command outside a
    working tree controlled by Git. This form implies
    --exit-code.

So, apparently something like this would work:

git diff --no-index -- /some/path1 /some/path2

Store the output into a file with:

git diff --no-index -- /some/path1 /some/path2 > mydiff.txt

...or with color output via ANSI color codes:

git diff --no-index --color=always -- /some/path1 /some/path2 > mydiff.txt

You could also just manually force both sets of files into a new git repo to see how they differ:

# make a new git repo
mkdir newrepo
cd newrepo
git init

# copy the first set of files into it (manually delete the .git
# folder in this set of files **before copying them**, if it has one,
# as you do NOT want to overwrite the destination .git folder)
cp -r /some/path1 .

# add them to the repo
git add -A
git commit


# manually delete everything in the repo except the .git folder


# copy 2nd set of files into repo (manually delete the .git
# folder in this set of files **before copying them**, if it has one,
# as you do NOT want to overwrite the destination .git folder)
cp -r /some/path2 .

# add them to the repo
git add -A
git commit


# compare the two sets of files by comparing your previous 
# commit (with files from "/some/path1") to your current
# commit (with files from "/some/path2").
git diff HEAD~..HEAD
# OR (same thing):
git diff HEAD~

References:

  1. man git diff
  2. The other answer by @Nick Volynkin: How to diff two local repositories

There's a diff parameter --no-index designed especially for this case

git diff [options] [--no-index] [--] <path> <path>

Also it can be done with --work-tree parameter. It tells Git to use a different working tree (but the same repository).

cd path/to/project1
git --work-tree=path/to/project2 diff [options]

The output may be large and can be saved to a file by adding > filename.log to the command line.

This shows you the differencies in files, not in commits. For commits, see How do I compare two git repositories?

Tags:

Git

Github