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

发布于 2024-08-16 08:24:28 字数 121 浏览 13 评论 0原文

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

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

How can we get the difference between two git repositories?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

念﹏祤嫣 2024-08-23 08:24:28

在 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

In 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
提赋 2024-08-23 08:24:28

Meld 可以比较目录:

meld directory1 directory2

只需使用两个 git 存储库的目录,你就会得到一个很好的图形比较:

在此处输入图像描述

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

Meld can compare directories:

meld directory1 directory2

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

enter image description here

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

橘亓 2024-08-23 08:24:28

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

git remote add other_name PATH_TO_OTHER_REPO

然后从该远程获取分支:

git fetch other_name branch_name:branch_name

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

git diff branch_name

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:

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):

git diff branch_name
叶落知秋 2024-08-23 08:24:28

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

只需打开两个存储库的父文件夹并等待它建立索引。然后您可以右键单击文件夹或文件并比较...并选择另一侧相应的文件夹/文件。

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

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

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.

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

惯饮孤独 2024-08-23 08:24:28

这是一种无需修改遥控器配置即可轻松完成此操作的方法。
从 repo A 的 master 中(假设您想要比较 master 分支):

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

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

git fetch path/to/repo_b.git master
git diff FETCH_HEAD
无法回应 2024-08-23 08:24:28

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

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

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 fetch /the/other/repo/.git refs/heads/*:refs/remotes/other/*
铁轨上的流浪者 2024-08-23 08:24:28
git diff master remotes/b

这是不正确的。 remotes/b 是远程服务器,但不是分支。

为了让它发挥作用,我必须这样做:

git diff master remotes/b/master
git diff master remotes/b

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

To get it to work, I had to do:

git diff master remotes/b/master
无力看清 2024-08-23 08:24:28

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

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

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

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

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


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

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

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-parse with --git-dir / GIT_DIR to convert symbolic name in other repository to SHA-1 identifier.

Modern version would look something like this (assuming that you are in '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/.git is 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.


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.

—━☆沉默づ 2024-08-23 08:24:28

您可以使用以下命令:

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

或者对于并排您可以使用:

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

如果对每个差异文件进行着色,您可以使用:

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

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\1\x1b[0m/"
诠释孤独 2024-08-23 08:24:28

为了跟进 @iamamac 的回答,在此之后有一个很好的摘要

git remote add -f b path/to/repo_b.git
git remote update

我会使用diff-tree

git diff-tree master remotes/b/master --compact-summary

To follow up @iamamac answer to have a nice summary after this:

git remote add -f b path/to/repo_b.git
git remote update

I would use diff-tree:

git diff-tree master remotes/b/master --compact-summary
我早已燃尽 2024-08-23 08:24:28

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

diff -r repo-A repo-B

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

diff -r repo-A repo-B

情感失落者 2024-08-23 08:24:28

提醒自己...首先获取,否则存储库没有本地哈希(我猜)。

步骤 1. 设置上游远程和以上 ^

比较单个文件遵循以下模式:

git diff localBranch uptreamBranch --spacepath/singlefile

git diff master upper/nameofrepo -- src/index.js

Reminder to self... fetch first, else the repository has not local hash (I guess).

step 1. Setup the upstream remote and above^

diffing a single file follows this pattern :

git diff localBranch uptreamBranch --spacepath/singlefile

git diff master upstream/nameofrepo -- src/index.js

梦在夏天 2024-08-23 08:24:28
  1. 如果您的计算机上有这两个存储库,则可以使用 WinMerge 来实现此目的 - https://winmerge.org/? lang=en
  2. 它有一个很好的用户界面并且很容易看到,合并
    文件或文件夹的差异或任何您喜欢的任务
  3. 打开 WinMerge,然后选择文件 -> 。打开命令
    https://i.sstatic.net/XhKxv.png
  4. 选择两个或多个存储库位于您的机器中
    https://i.sstatic.net/NoA2V.png
  5. 然后你就可以看到文件中的更改(如果有)(它们将被标记为红色)。
  6. 您只需双击所需的文件即可比较存储库之间的差异。
  7. 它有很多很酷的功能,我真的向大家推荐这个。
  1. You can use WinMerge for this if you have both the repositories in your machine- https://winmerge.org/?lang=en
  2. It has a good user interface and very easy to see, merge the
    difference or any task you like for the files or folders
  3. Open WinMerge and then select file -> Open command
    https://i.sstatic.net/XhKxv.png
  4. Select the two or more repositories which that is located in your machine
    https://i.sstatic.net/NoA2V.png
  5. Then you will be able to see the changes in the file if there are any(They will be marked as red).
  6. You can compare the difference between repositories by just double clicking the the required file.
  7. It has so many cool features and I really recommend this to you all.
债姬 2024-08-23 08:24:28

对于周围的任何 Meld 或 Pycharm 用户,

  1. 添加您想要比较的遥控器并从中获取
git fetch fork
git fetch downstream
  1. 运行 git diff 使用 meld 或 intellij

meld

git difftool --tool=meld --dir-diff fork/work-on-kustomize downstream/master

intellij

根据 https://gist.github.com/rambabusaravanan/1d1902e599c9​​c680319678b0f7650898

[difftool "intellij"]
    cmd = /usr/local/bin/idea diff $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")

然后执行

git difftool --tool=intellij --dir-diff fork/work-on-kustomize downstream/master

For any Meld or Pycharm users around,

  1. Add the remotes you want to compare and fetch from them
git fetch fork
git fetch downstream
  1. Run git diff with either meld or intellij

meld

git difftool --tool=meld --dir-diff fork/work-on-kustomize downstream/master

intellij

configure intellij in ~/.gitconfig according to https://gist.github.com/rambabusaravanan/1d1902e599c9c680319678b0f7650898

[difftool "intellij"]
    cmd = /usr/local/bin/idea diff $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")

and then do

git difftool --tool=intellij --dir-diff fork/work-on-kustomize downstream/master
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文