本文介绍了Git拉/取与refspec差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用refspec是一种获取远程分支并创建一个类似名称的方便方法,但使用给定名称(或者反过来:创建一个具有与本地名称不同的给定名称的远程分支)。我对一个小问题感到困惑 - 因为pull也会和当前分支合并,所以我期待不同的行为:

  git fetch origin master:mymaster 

和from

  git pull origin master:mymaster 

两者上述命令似乎产生完全相同的结果 - 这是一个名为mymaster的本地分支,与原点/主相同。我是对的还是两者之间存在模糊的区别?

最后,使用refspec将创建一个本地分支不是跟踪分支,对吧?由于跟踪分支是自动推送的,当一个调用没有任何参数的git push时AFAIK

解决方案

refspec只是一个源/目标对。使用refspec x:y fetch 告诉git在这个名为y的回购中创建一个分支,远程回购中名为x的分支的副本。没有别的。



使用 pull ,git会在顶部引发合并。首先,使用给定的refspec完成提取,然后将目标分支合并到当前分支中。如果这很让人困惑,请按照以下步骤操作:

  git pull原点大师:mymaster 


  1. 转到原点并获取分支master

  2. 制作一个本地名为mymaster的副本

  3. 将mymaster合并到当前分支中

完全限定,即 refs / heads / mymaster refs / heads / master 。为了比较,git在克隆上设置的默认refspec是 + refs / heads / *:refs / remotes / origin / * refs / remotes 为保持远程分支与本地分支分离提供了方便的名称空间。你在做的是告诉git把一个远程跟踪分支放在与你的本地分支相同的命名空间中。



至于跟踪分支,这只是一个入口在你的配置文件中,告诉git在哪里拉和推动本地分支到/默认情况下。


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

解决方案

A refspec is just a source/destination pair. Using a refspec x:y with fetch 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:

git pull origin master:mymaster
  1. Go to origin and get branch "master"
  2. Make a copy of it locally named "mymaster"
  3. Merge "mymaster" into the current branch

Fully qualified, that would be refs/heads/mymaster and refs/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拉/取与refspec差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 23:01