git - 为什么 GitHub 在比较两个相同的分支时会显示变化?
问题描述
我已经创建了测试存储库:https ://github.com/Labutin/CompareBranches 让我们尝试克隆和比较两个分支 branch_01 和 branch_02
$ git clone https://github.com/Labutin/CompareBranches.git
Cloning into 'CompareBranches'...
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (7/7), done.
$ git diff remotes/origin/branch_01..remotes/origin/branch_02 --exit-code
$ echo $?
0
所以,我们有两个相同的分支。
但是,如果我尝试使用 Web UI 来比较两个分支https://github.com/Labutin/CompareBranches/compare/branch_01...branch_02它显示 1 个文件已更改。为什么?Web UI 有什么问题?也许我必须修改网址?
解决方案
这个问题实际上是关于 GitHub 的——但我们可以用它来构建一个关于 Git 的问题。
在我克隆您的存储库后:
$ git clone https://github.com/Labutin/CompareBranches.git
Cloning into 'CompareBranches'...
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 0
我可以查看其中的所有提交:
$ cd CompareBranches
$ git log --all --decorate --oneline --graph
* 5707453 (origin/branch_02) Commit to branch_02
| * c0e3722 (origin/branch_01) commit to branch_01
|/
* 0e9a4e3 (HEAD -> master, origin/master, origin/HEAD) Initial commit
所以有三个提交,它们的实际(但缩写)名称是0e9a4e3
、c0e3722
和5707453
。远程跟踪名称 origin/master
是指第一个提交,其他两个远程跟踪名称是origin/branch_01
指origin/branch_02
其他两个提交。
如果我们让 Git 比较提交c0e3722
和5707453
,我们看不出有什么区别:
$ git diff c0e3722 5707453
但是如果我们让 Git 比较,比如 commit0e9a4e3
和 commit 5707453
,我们会发现这两个 commit是不同的:
$ git diff 0e9a4e3 5707453
diff --git a/README.md b/README.md
index f00f3be..b183451 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
-# CompareBranches
\ No newline at end of file
+# CompareBranches
+commit1
由于提交c0e3722
并5707453
具有相同的内容,因此与0e9a4e3
任何一个进行比较都会显示相同的更改。
该git diff
命令具有特殊语法
当我们想比较两个提交时,我们可以运行:
git diff <thing-1> <thing-2>
Git 会将 thing-1 与 thing-2 进行比较。尖括号内的部分可以是任何标识提交的内容:例如原始哈希 ID 0e9a4e3
,或分支名称,或远程跟踪名称。
如果我们运行:
git diff <thing-1>..<thing-2>
Git 做同样的事情:它比较两件事。
但是,如果我们使用三个点而不是两个点:
git diff <thing-1>...<thing-2>
Git 做了一些特别的事情。Git 没有比较我们命名的两个提交,而是找到了第三个提交。特别是,Git 会寻找一个合并基础提交,两个命名的提交都来自该提交。我们在git log --graph
上面的输出中看到了这一点:两个提示提交都来自 commit 0e9a4e3
。这就是合并基数,因此,使用三个点而不是两个点,这就是 Git 在比较左侧使用的提交。
右边就是它<thing-2>
自己。
事实证明,GitHub 在这里做同样的事情,这就是为什么 GitHub 的链接中有三个点:GitHubgit diff
故意模仿 , 的语法。
推荐阅读
- sql - postgres:列出所有具有唯一索引的列名
- kubernetes - 使用 ip 列出 kubernetes pod 的 Ansible 剧本
- vbscript - VBS:InputBox 位置与从 WMIC 检索的屏幕尺寸不正确相关
- weka - 是否可以通过 Experimenter 模块输出训练数据的错误?
- amazon-web-services - 一个容器启动失败,为什么ECS任务失败?
- r - 控制流:如何让数据显示而不是索引
- python - 使用 Infinite Scroll 从网站抓取数据?
- javascript - 有人可以解释这个函数/参数吗?
- python - 迷宫节点扩展和O(n)研究
- cassandra - Cassandra CommitLog 目录忘记删除文件