2012-06-15 55 views
1

如果我做git fetch origin master做git diff ...如果我做git fetch(沒有指定分支),origin不會有相同的結果,然後做git diff ...原點(見下文)。這是爲什麼? (只是要注意:Git版本1.7.3.1.msysgit.0)爲什麼git fetch指定分支不匹配沒有指定分支匹配(爲了區別)

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! 

回答

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. 
+0

非常感謝......該操作似乎仍不自然,現在至少我知道現在發生了什麼:)有什麼從FETCH_HEAD更新我的本地原點/主的方法? –

+1

在您的示例中,您的本地「主」分支跟蹤原點的主分支。所以你可以發出'git merge FETCH_HEAD'來合併你用'git fetch origin master'獲取的變化。另外,你可以用'git fetch origin master:refs/remotes/origin/master'直接讀取'origin/master'指針,然後'git merge origin/master'。坦率地說,在日常運營中,這並不重要。要麼工作。我習慣和懶惰使用'FETCH_HEAD'版本。你也可以看看'git pull',它同時執行'fetch'和'merge'。 – Christopher

相關問題