如何比较两个本地代码仓库的差异?

17

我有两个代码库,它们分别在不同的文件夹中,是我从本地机器上下载的(以.zip文件形式并解压)。它们的内容几乎相同,但其中有成千上万个文件。

是否可能使用Git跨库比较并查找那些微小的更改?我怀疑在5-6个文件中有少量的更改,但我需要找到它们。

如果使用上传版本更容易处理,我确实已将它们都上传至Github。如果有影响的话(我怀疑没有),我的本地环境是Mac。

注意:这两个代码库都不是彼此的分支(其中一个是朋友的代码库的分支;两个代码库共享最近的公共起源,我们都是从那里fork出来的)


@NickVolynkin:是的,没错 - 已经更正了。 :) - zahabba
看起来我的编辑被拒绝了,因为太过激进了。我的意思是要使它更易读; 我删除了一些在我看来与问题无关的信息。也许我错了。你能否请看一下?http://stackoverflow.com/review/suggested-edits/8402061 - Nick Volynkin
2个回答

20

针对这种情况,有一个名为--no-index的diff参数专门设计而来。

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

此外,也可以使用--work-tree参数来完成。它告诉Git使用不同的工作树(但是相同的存储库)。
cd path/to/project1
git --work-tree=path/to/project2 diff [options]

输出可能很大,可以通过在命令行中添加> filename.log将其保存到文件中。(参考链接) 这将显示文件之间的差异,而不是提交。有关提交,请参见如何比较两个git存储库?

由于某种原因,这个方法没有起作用。proj1和proj2都在同一个目录下。当我在proj1中尝试使用git diff --work-tree=../proj2时,它会显示error:invalid option --work-tree=../proj2。(当我将diff移动到标志之后,就像你的示例一样,它什么也没做) - zahabba
1
@zahabba 我在我的电脑上尝试过这个方法,它可以正常工作。你的 Git 版本是多少?无论如何,请尝试第二个选项并使用 --no-index 参数。 - Nick Volynkin
v2.3.2(Apple Git-55)。但是,在proj1内执行git diff --no-index . ../proj2时它能够正常工作。你知道如何将输出保存为文本文件吗?(比我想象的要长)谢谢! - zahabba
1
@zahabba 当然可以) https://dev59.com/1G445IYBdhLWcg3w_PAn - Nick Volynkin
1
顺便提一下,如果您只需要一个更改文件的列表,则有相应的“diff”选项。 - Nick Volynkin
1
我尝试过这个 - 花了很长时间,因为它也在 .git 中比较对象。有没有办法避免这种情况? - Lou

3

来自Linux Ubuntu 20.04上 man git diff 的说明:

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.

所以,显然像这样的东西可以起作用:

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

使用以下方式将输出存储到文件中:

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

...或通过ANSI颜色代码输出带有颜色的内容:

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

您也可以将两组文件手动强制合并到一个新的git仓库中,以查看它们的差异:

# 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~

参考资料:

  1. man git diff
  2. @Nick Volynkin的另一个答案:如何对比两个本地仓库

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接