1

如果我执行 git fetch origin master 执行 git diff ...origin 如果我执行 git fetch (不指定分支)然后执行 git diff ...origin (见下文),则不会有相同的结果。这是为什么?(请注意:git 版本 1.7.3.1.msysgit.0)

git init parent && cd parent

echo 'version1' > contents.txt
git add contents.txt
git commit -m "version1"

cd ..
git clone parent child
cd child

echo 'child' >> contents.txt
git commit -a -m "Child"

cd ../parent
echo 'parent' >> contents.txt
git commit -a -m "Parent"

cd ../child
git fetch origin master
git diff ...origin

echo Expected diff here and it wasn't there!

git fetch
git diff ...origin

echo Ok, diff appeared properly now!
4

1 回答 1

3

git fetch origin master不会将更新的提交指针存储在 中.git/refs/remotes/origin/master,而是将其存储在 中.git/FETCH_HEAD,如您的示例输出所示:

$ git fetch origin master
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /tmp/parent
* branch            master     -> FETCH_HEAD

如果您想查看差异,只需发出git diff ...FETCH_HEAD

$ git diff ...FETCH_HEAD
diff --git a/contents.txt b/contents.txt
index 5bdcfc1..1f428af 100644
--- a/contents.txt
+++ b/contents.txt
@@ -1 +1,2 @@
 version1
+parent

您可以在手册页中看到原因:

The ref names and their object names of fetched refs are stored in .git/FETCH_HEAD. 
This information is left for a later merge operation done by git merge.

git fetch origin另一方面,当您发出时,它会更新您所有的本地跟踪跟踪分支。从你的例子:

$ git fetch origin
From /tmp/parent
e23f025..4ea3d15  master     -> origin/master

再次从手册页:

Update the remote-tracking branches:

       $ git fetch origin

   The above command copies all branches from the remote refs/heads/ namespace and stores them to the local refs/remotes/origin/ namespace, unless the
   branch.<name>.fetch option is used to specify a non-default refspec.
于 2012-06-15T14:24:22.657 回答