如何区分两个本地存储库

时间:2015-06-11 04:59:35

标签: git github

我有两个已经下载到本地计算机的存储库(作为我解压缩的.zip文件)位于不同的文件夹中。内容几乎相同,但其中有数千个文件。

是否可以使用Git对它们进行比较并找到那些微小的变化?我怀疑5-6个文件中有一些变化,但我需要找到它们。

如果使用上传的版本更容易,我在Github中都有它们。如果它很重要(我怀疑它没有),我的本地环境就是Mac。

注意:这些回购都不是另一个的分支。 (一个是朋友回购的分支;两个回购共享最近我们分享的共同起源)

2 个答案:

答案 0 :(得分:7)

特别为此案例设计的差异参数--no-index

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

也可以使用--work-tree参数完成。它告诉Git使用不同的工作树(但是使用相同的存储库)。

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

通过将> filename.log添加到命令行,输出可能很大且can be saved到文件。

这将显示文件中的差异,而不是提交。有关提交,请参阅How do I compare two git repositories?

答案 1 :(得分:1)

man git diff(在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.

因此,显然这样会起作用:

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 repo中,以查看它们之间的区别:

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