获取两个存储库之间的差异
我们如何获得两个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
在 repo_a 中:
In repo_a:
Meld 可以比较目录:
只需使用两个 git 存储库的目录,你就会得到一个很好的图形比较:
当您单击其中一个蓝色项目时,您可以看到发生了什么变化。
Meld can compare directories:
Just use the directories of the two git repos and you will get a nice graphical comparison:
When you click on one of the blue items, you can see what changed.
您可以首先将其他存储库作为远程添加到当前存储库:
然后从该远程获取分支:
这会将该分支创建为当前存储库中的新分支,然后您可以将该分支与任何分支进行比较,例如,将当前分支与新分支(branch_name)进行比较:
You can add other repo first as a remote to your current repo:
then fetch brach from that remote:
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):
我使用 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.
这是一种无需修改遥控器配置即可轻松完成此操作的方法。
从 repo A 的 master 中(假设您想要比较 master 分支):
An easy way to do that without touching your remotes config.
From repo A, in master (assume you want to compare master branches):
一旦您在一个存储库中拥有两个分支,您就可以执行 git diff 了。将它们放入一个存储库就像
Once you have both branches in one repository you can do a
git diff
. And getting them in one repository is as easy as这是不正确的。
remotes/b
是远程服务器,但不是分支。为了让它发挥作用,我必须这样做:
That's incorrect.
remotes/b
is a remote, but not a branch.To get it to work, I had to do:
请参阅 http://git.or.cz/gitwiki/GitTips ,“常规”中的“如何比较两个本地存储库”部分。
简而言之,您正在使用 GIT_ALTERNATE_OBJECT_DIRECTORIES 环境变量来访问其他存储库的对象数据库,并使用 git rev-parse 与
--git-dir
/ GIT_DIR 将其他存储库中的符号名称转换为 SHA-1 标识符。现代版本看起来像这样(假设您位于“repo_a”中):
其中
../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'):
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.您可以使用以下命令:
或者对于并排您可以使用:
如果对每个差异文件进行着色,您可以使用:
You can use the following command:
or for the side by side you can use:
In case of Colorizing every diff file, you can use:
为了跟进 @iamamac 的回答,在此之后有一个很好的摘要:
我会使用diff-tree:
To follow up @iamamac answer to have a nice summary after this:
I would use diff-tree:
最好的办法是在本地计算机上同时拥有两个存储库,并使用 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
提醒自己...首先获取,否则存储库没有本地哈希(我猜)。
步骤 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
文件或文件夹的差异或任何您喜欢的任务
https://i.sstatic.net/XhKxv.png
https://i.sstatic.net/NoA2V.png
difference or any task you like for the files or folders
https://i.sstatic.net/XhKxv.png
https://i.sstatic.net/NoA2V.png
对于周围的任何 Meld 或 Pycharm 用户,
meld
intellij
根据 https://gist.github.com/rambabusaravanan/1d1902e599c9c680319678b0f7650898
然后执行
For any Meld or Pycharm users around,
meld
intellij
configure intellij in
~/.gitconfig
according to https://gist.github.com/rambabusaravanan/1d1902e599c9c680319678b0f7650898and then do