为什么指定分支的 git fetch 与未指定分支的 fetch 不匹配(出于不同的目的)



如果我做 git fetch origin master do git diff ...如果我做 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!

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.

相关内容

  • 没有找到相关文章

最新更新