克隆仓库和原始远程仓库之间的git diff差异

175

我克隆了一个 Github 存储库,但没有在本地进行更改。该存储库在同一分支上向前推进了提交。

  1. 如何找出本地存储库和原始 Github 存储库之间的差异?
  2. 如何找出工作副本和原始 Github 存储库之间的差异?
  3. 如何找出本地存储库和同一项目的另一个 Github 存储库之间的差异?

1
可能是重复的问题:如何比较本地 Git 分支和远程分支? - Ciro Santilli OurBigBook.com
3个回答

163

1)添加任何您想要比较的远程仓库:

git remote add foobar git://github.com/user/foobar.git

2) 更新您所拥有的远程代码库的本地副本:

git fetch foobar

获取操作不会改变您的工作副本。

3)将本地仓库中的任何分支与您添加的任何远程分支进行比较:

git diff master foobar/master

我需要在diff之前进行git fetch吗?我猜没有它就无法进行diff了。 - Delta George
这实际上是仅针对问题3的回复(与另一个GitHub存储库的差异)。 - Ruslan Kabalin
5
"原始的 Github 存储库" 和其他远程存储库没有任何区别。或者我有什么误解吗?" - dbyrne
当我尝试使用git 2.7.4时,"git diff master foobar/master"没有显示差异。在我的看来,它比较了我的本地副本("master")和作为第一个参数给出的repo("master")中的"路径/文件""foobar/master",并且只有在那里才会有差异。但是命令"diff foobar/master"对我有效,将我的本地master与foobar/master进行比较。 - user2081279
作为替代方案,可以使用git remote update而不是fetch(第2个) - 它将更新所有设置为跟踪远程分支的分支,但不会合并任何更改; 或者使用git pull - Timo
@ Ruslan Kabalin:不。 - Timo

51

回答你的问题(假设你在主分支上,并且已经执行了“git fetch origin”命令使你的本地仓库获取远程更改):

1)自从本地分支创建以来,远程分支的提交:

git diff HEAD...origin/master

2) 我假设你所说的“工作副本”是指你的本地分支,其中包含一些尚未推送到远程的本地提交。要查看本地分支上存在但在远程分支上不存在的差异,请运行:

git diff origin/master...HEAD

3) 查看dbyrne的回答


谢谢你提供 HEAD..origin/master 语法!我们一直遇到 origin/HEAD 不存在的错误,这个解决了问题。 - Dan Bechard
@ruslan:如果我克隆了一个我有权限进行更改的远程目录,当git diff HEAD...origin/master什么都不返回时,这是什么意思? - Mona Jalal
当我使用Windows GUI进行克隆时出现了错误,因此我想知道克隆是否已经完成。我在目录中看到文件夹,但我想确保它与远程的相同。然而,在git shell中,git diff没有返回任何内容。我很困惑我的克隆是否成功了? - Mona Jalal
@MonaJalal 这意味着你克隆它之后没有上游更改。 - Ruslan Kabalin

22

这个例子可能对某些人有所帮助:

origin 是我给远程仓库“Github 上的内容”起的别名
mybranch 是我给正在与 Github 同步的本地分支“本地内容”起的别名。
--如果您没有创建过分支,则您的分支名称为“master”。但是,我使用不同的名称 mybranch 来展示如何使用分支名称参数。


我的 Github 远程仓库到底是什么?

$ git remote -v
origin  https://github.com/flipmcf/Playground.git (fetch)
origin  https://github.com/flipmcf/Playground.git (push)

添加“同一代码的其他GitHub仓库”-我们称其为分支(fork):

$ git remote add someOtherRepo https://github.com/otherUser/Playground.git

$git remote -v
origin  https://github.com/flipmcf/Playground.git (fetch)
origin  https://github.com/flipmcf/Playground.git (push)
someOtherRepo https://github.com/otherUser/Playground.git (push)
someOtherRepo https://github.com/otherUser/Playground.git (fetch)

确保本地仓库是最新的:

$ git fetch

在本地更改一些内容,比如说文件 ./foo/bar.py

$ git status
# On branch mybranch
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   foo/bar.py

查看我的未提交更改

$ git diff mybranch
diff --git a/playground/foo/bar.py b/playground/foo/bar.py
index b4fb1be..516323b 100655
--- a/playground/foo/bar.py
+++ b/playground/foo/bar.py
@@ -1,27 +1,29 @@
- This line is wrong
+ This line is fixed now - yea!
+ And I added this line too.

本地提交。

$ git commit foo/bar.py -m"I changed stuff"
[myfork 9f31ff7] I changed stuff
1 files changed, 2 insertions(+), 1 deletions(-)

现在,我与我的远程(在GitHub上)不同。

$ git status
# On branch mybranch
# Your branch is ahead of 'origin/mybranch' by 1 commit.
#
nothing to commit (working directory clean)

将此与远程仓库进行比较 - 您的分支: (通常使用git diff master origin)

$ git diff mybranch origin
diff --git a/playground/foo/bar.py b/playground/foo/bar.py
index 516323b..b4fb1be 100655
--- a/playground/foo/bar.py
+++ b/playground/foo/bar.py
@@ -1,27 +1,29 @@
- This line is wrong
+ This line is fixed now - yea!
+ And I added this line too.

(git push 将这些应用到远程)

我的远程分支和远程主分支有什么不同?

$ git diff origin/mybranch origin/master

我的本地分支与远程主分支有何不同?

$ git diff origin/master

我的东西与同一仓库的其他人的衍生版本(fork)或主分支(master branch)有何不同?

$git diff mybranch someOtherRepo/master

5
在执行 'git diff' 之前,我认为有必要执行 'git fetch' 确保我们本地的远程副本是最新的。 - Don
1
我已经阅读了大约20篇关于如何比较远程和本地的帖子,但我自己刚刚弄清楚:必须先进行git fetch。git确实是RCS的汇编代码。天哪。感谢确认,唐! - Terence Parr
刚刚在这个答案中添加了 git fetch - FlipMcF
1
唔…我不喜欢我用“myfork”作为我的分支名称。这可能会让某人感到困惑(比如我,我刚回来找这个答案)。 - FlipMcF
编辑 - 澄清并回答“fork区别”问题#3 - FlipMcF

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接