为什么git fetch指定分支与fetch不匹配而没有指定分支(用于区分目的)

时间:2012-06-15 13:39:36

标签: git

如果我执行git fetch origin master正在执行git diff ...如果我执行git fetch(不指定分支)然后执行git diff ... origin(参见下文),则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!

1 个答案:

答案 0 :(得分: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.