具有 refspec 差异的 Git pull/fetch
使用 refspec 是一种获取远程分支并创建一个具有给定名称的类似分支的便捷方法(或者相反:创建一个具有与本地分支不同的给定名称的远程分支)。我对一件小事感到困惑 - 因为 pull 也会与当前分支进行合并,所以我期望不同的行为:
git fetch origin master:mymaster
和
git pull origin master:mymaster
上述两个命令似乎产生完全相同的结果 - 这是一个本地分支称为 mymaster,与 origin/master 相同。我是对的还是两者之间有模糊的区别?
最后,使用 refspec 将创建一个本地分支,而不是一个跟踪分支,对吧?由于当调用 git push 时不带任何参数时,跟踪分支会自动推送,AFAIK
Using refspec is a convenient way to grab a remote branch and create a similar one but with given name (or the other way round: create a remote one with a given name different from the local one). I'm puzzled about one tiny thing - as pull will also do the merge with current branch I would expect different behavior from:
git fetch origin master:mymaster
and from
git pull origin master:mymaster
Both of the above commands seem to produce exactly same result - that is a local branch called mymaster, same as origin/master. Am I right or is there a vague difference between the two?
Finally, using a refspec will create a local branch not a tracking branch, right? Since tracking branches are pushed automagically when one invokes git push without any arguments AFAIK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
refspec 只是一个源/目标对。使用 refspec
x:y
和fetch
告诉 git 在此存储库中创建一个名为“y”的分支,该分支是远程存储库中名为“x”的分支的副本。没有别的了。通过
pull
,git 在顶部抛出一个合并。首先,使用给定的 refspec 完成获取,然后将目标分支合并到当前分支中。如果这令人困惑,请按照以下步骤操作:完全限定,即 refs/ heads/mymaster 和 refs/heads/master 。为了进行比较,git 在克隆上设置的默认 refspec 是 +refs/heads/*:refs/remotes/origin/*。 refs/remotes 提供了一个方便的命名空间,用于将远程分支与本地分支分开。您正在做的就是告诉 git 将远程跟踪分支放在与本地分支相同的命名空间中。
至于“跟踪分支”,这只是配置文件中的一个条目,告诉 git 默认情况下将本地分支拉入或推入何处。
A refspec is just a source/destination pair. Using a refspec
x:y
withfetch
tells git to make a branch in this repo named "y" that is a copy of the branch named "x" in the remote repo. Nothing else.With
pull
, git throws a merge on top. First, a fetch is done using the given refspec, and then the destination branch is merged into the current branch. If that's confusing, here's a step-by-step:Fully qualified, that would be
refs/heads/mymaster
andrefs/heads/master
. For comparison, the default refspec set up by git on a clone is+refs/heads/*:refs/remotes/origin/*
.refs/remotes
makes a convenient namespace for keeping remote branches separate from local ones. What you're doing is telling git to put a remote-tracking branch in the same namespace as your local branches.As for "tracking branches", that's just an entry in your config file telling git where to pull and push a local branch to/from by default.
git fetch origin master:mymaster
通过从远程存储库的 master 分支获取来更新本地存储库中的分支 mymaster。git pull origin master:mymaster 执行上述操作并将其合并到当前分支中。
git fetch origin master:mymaster
updates branch mymaster in the local repository by fetching from the master branch of the remote repository.git pull origin master:mymaster
does above and merges it into the current branch.git fetch origin master:mymaster
但该命令必须严格满足以下两个条件:
本地当前分支不能是 mymaster。
本地 mymaster 分支是 origin/master 的祖先。
然后将进行快进合并。否则会报告致命错误。
当以上两个条件都成立时,执行命令:
除了执行命令:
还会执行:
注意:不是 git merge mymaster
FETCH_HEAD 与 mymaster 不同,因为 mymaster 可能已经快进合并了。
git fetch origin master:mymaster
However, the command must meet the following two conditions strictly:
The local current branch cannot be mymaster.
The local mymaster branch is the ancestor of origin/master.
then will make fast-forward merge. Otherwise it will report a fatal.
When both of the above conditions are true, execute the command:
In addition to executing the command:
Will also execute:
Notice:not git merge mymaster
FETCH_HEAD is different from mymaster, because mymaster maybe already fast-forward merge.
我使用 smartgit 创建分支,因此该分支可能没有正确合并到 master 中。
创建了版本的支持分支,即 support/4.2,自动创建标签,但现在我尝试执行 git pull,它显示了同样的错误。
由于 support/4.2 分支是在 github 中创建的,但未在本地正确合并。
所以我用过这个:-
git pull origin master:mymaster
在我的例子中 - git pull origin support/4.2:support/4.2
它有效:)
I had used smartgit to create branch, so might be at that branch doesn't properly merged into master.
Created support branch for release ie support/4.2, tag automatically get created but now wehn I try to do git pull, it shows me same error.
As support/4.2 brannch is created in github but not properly merged in local.
So I have used this :-
git pull origin master:mymaster
In my case - git pull origin support/4.2:support/4.2
It works :)