2016-11-15 93 views
0

Github-Help: Syncing a Fork的文档显示了三条命令让我的GitHub fork与上游repo保持同步。与上游同步叉:git fetch + git checkout + git merge与git checkout + git pull

git fetch upstream 
git checkout master 
git merge upstream/master 

我可以使用以下两个命令而不是上述三个吗?

git checkout master 
git pull upstream/master 

这两组命令是否等同,还是两者之间存在差异?

+0

的可能的复制[在Git是如何被获取比拉不同的又是怎样的合并比变基不同?(http://stackoverflow.com/ques tits/14894768/in-git-how-is-fetch-different-pull-and-how-is-merge-different-rebase) –

+0

'git pull' ='git fetch' +'git merge',至少在一般情况下 –

+0

仅供参考:'git pull upstream/master'是错误的,因为第三个字('upstream/master')必须是* remote *的名称,而'upstream/master'是一个*远程追踪分支*。不幸的是,Git使用了非常相似的单词,意思是非常不同的(尽管相关):单词* branch *至少有两个含义,单词* remote *本身具有一个含义,而短语* remote-tracking branch *有另一个含义。 – torek

回答

0

这些命令集不相同。

git pull 

被分成两个命令:

git fetch 
git merge 

的问题是,git的提取需要一个远程引用,而Git合并需要跟踪引用,这就是为什么Github的帮助页面有:

git fetch upstream 

但它有

git merge upstream/master 

合并命令将采用upstream/master分支并将其合并到当前检出的分支中(本例中为“主”)。但是取命令不会在分组工作,它需要一个遥控器,所以当你尝试:

git pull upstream/master 

的Git分裂成这样:

git fetch upstream/master 
git merge upstream/master 

,这将在抓取失败:

$ git pull upstream/master 
fatal: 'upstream/master' does not appear to be a git repository 
fatal: Could not read from remote repository. 

Please make sure you have the correct access rights 
and the repository exists. 
+0

**注意**:如果您至少执行了'git fetch upstream'至少一次:'git pull upstream/master'工作:'upstream/master'引用远程分支的本地副本,而不是远程服务器上的副本。 –

+0

@FabienBouleau这在我的系统上不正确。即使当它已经存在时,使用'upstream/master'的提取和拉取都会失败。提取需要远程引用,而不是分支引用。也许如果您将远程名称“upstream/master”添加为具有与“上游”相同的URL /文件夹的远程设备,或者其他配置更改(但不是默认情况下),至少当上游是辅助远程设备时(即添加了源第一)。 – LightCC

+0

我的不好,在这种情况下'upstream/master'这个符号是错误的。它必须是'git pull upstream master'或'git fetch upstream master'。并且第一次下载远程仓库时,它必须是'git fetch upstream'(没有'master'),或者远程跟踪分支信息不会被设置(只有'FETCH_HEAD')。使用'upstream/master'只能在远程追踪分支存在时引用远程追踪分支。 –