git 获取两个存储库之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1968512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:34:34  来源:igfitidea点击:

Getting the difference between two repositories

gitgit-diff

提问by sanjayav

How can we get the difference between two git repositories?

我们如何获得两个 git 存储库之间的差异?

The scenario: We have a repo_a and repo_b. The latter was created as a copy of repo_a. There have been parallel development in both the repositories afterwards. Is there a way we can list the differences of the current versions of these two repositories?

场景:我们有一个 repo_a 和 repo_b。后者是作为 repo_a 的副本创建的。之后两个存储库都进行了并行开发。有没有办法列出这两个存储库当前版本的差异?

回答by iamamac

In repo_a:

在 repo_a 中:

git remote add -f b path/to/repo_b.git
git remote update
git diff master remotes/b/master
git remote rm b

回答by Martin Thoma

Meldcan compare directories:

Meld可以比较目录:

meld directory1 directory2

Just use the directories of the two git repos and you will get a nice graphical comparison:

只需使用两个 git repos 的目录,您就会得到一个很好的图形比较:

enter image description here

在此处输入图片说明

When you click on one of the blue items, you can see what changed.

当您单击蓝色项目之一时,您可以看到发生了什么变化。

回答by M.Kouchi

You can add other repo first as a remote to your current repo:

您可以先将其他存储库作为远程存储库添加到当前存储库:

git remote add other_name PATH_TO_OTHER_REPO

then fetch brach from that remote:

然后从那个遥控器获取brach:

git fetch other_name branch_name:branch_name

this creates that branch as a new branch in your current repo, then you can diff that branch with any of your branches, for example, to compare current branch against new branch(branch_name):

这会将该分支创建为当前存储库中的新分支,然后您可以将该分支与任何分支进行比较,例如,将当前分支与新分支(branch_name)进行比较:

git diff branch_name

回答by James

git diff master remotes/b

That's incorrect. remotes/bis a remote, but not a branch.

那是不正确的。remotes/b是一个远程,但不是一个分支。

To get it to work, I had to do:

为了让它工作,我必须这样做:

git diff master remotes/b/master

回答by Michael Krelin - hacker

Once you have both branches in one repository you can do a git diff. And getting them in one repository is as easy as

一旦您在一个存储库中拥有两个分支,您就可以执行git diff. 将它们放在一个存储库中就像

git fetch /the/other/repo/.git refs/heads/*:refs/remotes/other/*

回答by Jakub Nar?bski

See http://git.or.cz/gitwiki/GitTips, section "How to compare two local repositories" in "General".

请参阅http://git.or.cz/gitwiki/GitTips,“常规”中的“如何比较两个本地存储库”部分。

In short you are using GIT_ALTERNATE_OBJECT_DIRECTORIES environment variable to have access to object database of the other repository, and using git rev-parsewith --git-dir/ GIT_DIR to convert symbolic name in other repository to SHA-1 identifier.

简而言之,您正在使用 GIT_ALTERNATE_OBJECT_DIRECTORIES 环境变量来访问其他存储库的对象数据库,并使用git rev-parse--git-dir/ GIT_DIR 将其他存储库中的符号名称转换为 SHA-1 标识符。

Modern version would look something like this (assuming that you are in 'repo_a'):

现代版本看起来像这样(假设您在“repo_a”中):

GIT_ALTERNATE_OBJECT_DIRECTORIES=../repo_b/.git/objects \
   git diff $(git --git-dir=../repo_b/.git rev-parse --verify HEAD) HEAD

where ../repo_b/.gitis path to object database in repo_b (it would be repo_b.git if it were bare repository). Of course you can compare arbitrary versions, not only HEADs.

其中../repo_b/.git是在repo_b路径对象数据库(这将是repo_b.git如果它是纯仓库)。当然,您可以比较任意版本,而不仅仅是 HEAD。



Note that if repo_a and repo_b are the same repository, it might make more sense to put both of them in the same repository, either using "git remote add -f ..." to create nickname(s) for repository for repeated updates, or obe off "git fetch ..."; as described in other responses.

请注意,如果 repo_a 和 repo_b 是同一个存储库,则将它们放在同一个存储库中可能更有意义,要么使用“ git remote add -f ...”为存储库创建昵称以进行重复更新,要么关闭“ git fetch ...”;如其他回复中所述。

回答by MrE

I use PyCharm which has great capabilities to compare between folders and files.

我使用 PyCharm,它具有在文件夹和文件之间进行比较的强大功能。

Just open the parent folder for both repos and wait until it indexes. Then you can use right click on a folder or file and Compare to...and pick the corresponding folder / file on the other side.

只需打开两个 repos 的父文件夹并等待它建立索引。然后,您可以右键单击文件夹或文件,Compare to...然后在另一侧选择相应的文件夹/文件。

It shows not only what files are different but also their content. Much easier than command line.

它不仅显示了哪些文件不同,还显示了它们的内容。比命令行容易得多。

回答by Andrew Heekin

Your best bet is to have both repos on your local machine and use the linux diff command with the two directories as parameters:

最好的办法是在本地机器上同时拥有两个 repos,并使用 linux diff 命令将两个目录作为参数:

diff -r repo-A repo-B

diff -r repo-A repo-B

回答by jorisv92

An easy way to do that without touching your remotes config. From repo A, in master (assume you want to compare master branches):

一个简单的方法来做到这一点,而无需触及您的遥控器配置。从 repo A 中,在 master 中(假设您要比较 master 分支):

git fetch path/to/repo_b.git master
git diff FETCH_HEAD

回答by Gerard

You can use the following command:

您可以使用以下命令:

diff -x .git -r repo-A repo-B

or for the side by side you can use:

或并排,您可以使用:

diff -x .git -W200 -y -r repo-A repo-B

In case of Colorizing every diff file, you can use:

在为每个差异文件着色的情况下,您可以使用:

diff -x .git -W200 -y -r repo-A repo-B | sed -e "s/\(^diff .*\)/\x1b[31m\x1b[0m/"