Git pull/fetch 与 refspec 差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7169103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:53:34  来源:igfitidea点击:

Git pull/fetch with refspec differences

gitgit-pullgit-fetch

提问by pielgrzym

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:

使用 refspec 是一种获取远程分支并创建一个类似但具有给定名称的分支的便捷方法(或者相反:创建一个给定名称与本地分支不同的远程分支)。我对一件小事感到困惑 - 因为 pull 也会与当前分支进行合并,我希望从以下方面获得不同的行为:

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?

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

Finally, using a refspec will create a localbranch nota trackingbranch, right? Since tracking branches are pushed automagically when one invokes git push without any arguments AFAIK

最后,使用的Refspec将创建一个本地分支没有一个跟踪分支,对不对?由于当一个人在没有任何参数的情况下调用 git push 时会自动推送跟踪分支 AFAIK

回答by Ryan Stewart

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

refspec 只是一个源/目标对。使用 refspec x:ywithfetch告诉 git 在这个名为“y”的 repo 中创建一个分支,它是远程 repo 中名为“x”的分支的副本。没有其他的。

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:

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

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
  1. 转到原点并获取分支“master”
  2. 在本地创建一个名为“mymaster”的副本
  3. 将“mymaster”合并到当前分支

Fully qualified, that would be refs/heads/mymasterand refs/heads/master. For comparison, the default refspec set up by git on a clone is +refs/heads/*:refs/remotes/origin/*. refs/remotesmakes 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.

完全合格,那就是refs/heads/mymasterrefs/heads/master。为了比较,git 在克隆上设置的默认 refspec 是+refs/heads/*:refs/remotes/origin/*. refs/remotes为使远程分支与本地分支保持分离提供了一个方便的命名空间。您正在做的是告诉 git 将远程跟踪分支放在与本地分支相同的命名空间中。

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 默认情况下从何处拉取和推送本地分支。

回答by manojlds

git fetch origin master:mymasterupdates branch mymaster in the local repository by fetching from the master branch of the remote repository.

git fetch origin master:mymaster通过从远程存储库的 master 分支获取更新本地存储库中的分支 mymaster。

git pull origin master:mymasterdoes above and merges it into the current branch.

git pull origin master:mymaster执行上述操作并将其合并到当前分支中。

回答by Fred Hu

git fetch origin master:mymaster

git fetch origin master:mymaster

However, the command must meet the following two conditions strictly:

但是,该命令必须严格满足以下两个条件:

  1. The local current branch cannot be mymaster.

  2. The local mymaster branch is the ancestor of origin/master.

  1. 本地当前分支不能是 mymaster。

  2. 本地 mymaster 分支是 origin/master 的祖先。

then will make fast-forward merge.Otherwise it will report an fatal.

然后将进行快进合并。否则它会报告一个致命的。

When both of the above conditions are true, execute the command:

当以上两个条件都成立时,执行命令:

git pull origin master:mymaster

git pull origin master:mymaster

In addition to executing the command:

除了执行命令:

git fetch origin master:mymaster

git fetch origin master:mymaster

Will also execute:

还会执行:

git merge FETCH_HEAD.

git 合并 FETCH_HEAD。

Notice :not git merge mymaster

注意:不是 git merge mymaster

FETCH_HEAD is different from mymaster,because mymaster maybe already fast-forward merge.

FETCH_HEAD 与 mymaster 不同,因为 mymaster 可能已经快进合并了。

回答by kandysingh

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

我曾使用 smartgit 创建分支,所以可能在那个分支没有正确合并到 master 中。为发行版创建支持分支,即 support/4.2,标签自动创建,但现在当我尝试执行 git pull 时,它显示了同样的错误。由于 support/4.2 分支是在 github 中创建的,但在本地没有正确合并。所以我使用了这个 :- git pull origin master:mymaster

In my case - git pull origin support/4.2:support/4.2

就我而言 - git pull origin support/4.2:support/4.2

It works :)

有用 :)