17

I have two repos that I've downloaded to my local machine (as .zip files that I unzipped) that are in separate folders. The contents are virtually the same, but there are thousands of files in them.

Is it possible to use Git to compare across them and find those minute changes? I suspect there are a handful of changes across 5-6 files but I need to find them.

I do have them both in Github if working with the uploaded versions is easier. If it matters (and I suspect it doesn't), my local environment is a Mac.

Note: Neither of these repos is a fork of the other. (One is a fork of a friend's repo; both repos share a recent common origin that we forked from)

zahabba
  • 2,921
  • 5
  • 20
  • 33
  • @NickVolynkin: Yes, that's right - corrected. :) – zahabba Jun 11 '15 at 05:33
  • Looks like my edit was rejected as too drastic. What I meant was to make it more readable; I removed info that, in my opinion, was not related to the problem. Maybe I was wrong with it. Could you please have a look? http://stackoverflow.com/review/suggested-edits/8402061 – Nick Volynkin Jun 11 '15 at 07:27

2 Answers2

19

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?

Community
  • 1
  • 1
Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
  • This for some reason didn't work. Both proj1 and proj2 are in the same directory. When in proj1, I tried: `git diff --work-tree=../proj2` and it said `error:invalid option --work-tree=../proj2`. (When I moved the `diff` to after the flag like in your example, it didn't do anything) – zahabba Jun 11 '15 at 05:19
  • 1
    @zahabba I've tried this on my machine and it worked. What's your Git version? Anyway, try the second option with `--no-index` – Nick Volynkin Jun 11 '15 at 05:21
  • v2.3.2 (Apple Git-55). But it did work when, inside proj1, I did `git diff --no-index . ../proj2`. Do you know how to save the output as a text file? (It's longer than I thought) Thank you!!! – zahabba Jun 11 '15 at 05:24
  • 1
    @zahabba sure ) http://stackoverflow.com/questions/4600445/git-log-output-to-xml-json-or-yaml – Nick Volynkin Jun 11 '15 at 05:27
  • 1
    @zahabba btw, if you only need a list of changed files, there are `diff` options for it. – Nick Volynkin Jun 11 '15 at 05:36
  • 1
    I tried this - it took ages because it's diffing objects in .git as well. Is there a way to avoid this? – Lou Oct 03 '22 at 16:42
3

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
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265