首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何比较两个本地存储库

如何比较两个本地存储库
EN

Stack Overflow用户
提问于 2015-06-11 12:59:35
回答 2查看 16.1K关注 0票数 16

我已经下载了两个repos到我的本地机器上(作为我解压的.zip文件),它们分别位于不同的文件夹中。内容实际上是相同的,但其中有数千个文件。

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

如果使用上传的版本更容易,我确实在Github中有这两个版本。如果这很重要(我怀疑它并不重要),那么我的本地环境就是Mac。

注意:这两个repos都不是另一个的分支。(一个是一个朋友的repo的分支;两个repo共享一个最近的共同来源,我们是从这个分支派生的)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-11 13:08:26

有一个专门为这种情况设计的diff参数--no-index

代码语言:javascript
复制
git diff [options] [--no-index] [--] <path> <path>

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

代码语言:javascript
复制
cd path/to/project1
git --work-tree=path/to/project2 diff [options]

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

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

票数 17
EN

Stack Overflow用户

发布于 2020-10-22 00:49:36

man git diff (在Linux Ubuntu20.04上):

index git diff -- no-

--此格式用于比较文件系统上给定的两个路径。在由Git控制的工作树中运行该命令时,或者在由Git控制的工作树外运行该命令时,可以省略--no-index选项。这种形式意味着--exit-code。

所以,很明显,像这样的东西是可行的:

代码语言:javascript
复制
git diff --no-index -- /some/path1 /some/path2

使用以下命令将输出存储到文件中:

代码语言:javascript
复制
git diff --no-index -- /some/path1 /some/path2 > mydiff.txt

具有通过ANSI色码输出的颜色的...or:

代码语言:javascript
复制
git diff --no-index --color=always -- /some/path1 /some/path2 > mydiff.txt

您还可以手动将这两组文件放入新的git代码库中,以查看它们之间的区别:

代码语言:javascript
复制
# 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. The @Nick Volynkin的其他答案:How to diff two local repositories
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30771920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档