为什么“git fetch origin branch:branch”仅适用于非当前分支? [英] Why "git fetch origin branch:branch" works only on a non-current branch?

查看:1893
本文介绍了为什么“git fetch origin branch:branch”仅适用于非当前分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用功能分支时,我使用这个Git命令将我的开发分支更新为最新状态,紧接在将我的功能分支与开发合并之前:

  git fetch原点开发:开发

,即本地开发指向与origin / develop相同的提交,并且处于具有原始状态的最新状态。

但是,当develop分支被签出时,该命令失败:

 致命:拒绝当前分支引用/头部/开发非裸仓库
致命:远端意外挂断

它如果我知道为什么会发生这种情况,我会更好地理解Git。 错误消息来自 builti n / fetch.c#check_not_current_branch()

该函数一直回到 commit 8ee5d73,2008年10月,git 1.6.0.4 b
$ b

该评论很有指导意义: / p>


一些令人困惑的教程建议,使用类似这样的方法将
提取到当前分支中是个好主意:




  git fetch origin master:master 

lockquote

(甚至更糟糕的是:与pull相同的命令行而不是fetch)。

虽然它可能有意义存储你想要的内容,当前分支为 master 时,通常是
明显错误。

只有当(不正确的) git pull origin master master:master 试图通过给出 - update-head-ok 到底层 git fetch ,否则我们应该拒绝它,但是在某些地方我们会失去这种行为。



对于当前分支的检查现在只在非纯粹的
仓库中执行,这是对原始行为的改进。


考虑到函数 check_not_current_branch()调用:

  if (!update_head_ok)
check_not_current_branch(ref_map);

这意味着 git fetch -u origin develop:develop

  -u 
--update-head-ok




默认情况下,git fetch拒绝更新与当前分支对应的头部。此标志禁用检查。

这仅仅是内部使用 git pull git fetch ,除非你正在实施自己的瓷器,否则你不应该使用它。


即使你不应该要使用该选项,它会回答您的初始需求,使< git获取原始分支:分支工作在当前分支上。

>




关于这个补丁的来源,按照那里的讨论


尽管存储您想要购买的东西可能是有意义的


这就是 fetch 部分:它存储来自更新的 origin / master 的远程历史记录, 。

但是,当前的本地分支也是 master 。 ://www.spinics.net/lists/git/msg82317.htmlrel =nofollow noreferrer> in this answer :



master 是当前分支
时,我认为 git fetch url side:master 并且我们省略了 - update-head-ok 已损坏。

当前 master

它也无法更新工作目录,并将
作为索引,就像删除所有内容一样。



While working on a feature branch, I use this Git command to update my "develop" branch to the latest state, immediately before merging my feature branch with the "develop":

git fetch origin develop:develop

This works, i.e. the local "develop" points at the same commit as "origin/develop" and is in the latest state with origin.

Somehow, though, this command fails when the "develop" branch is checked out:

fatal: Refusing to fetch into current branch refs/heads/develop of non-bare repository
fatal: The remote end hung up unexpectedly

It would help me understand Git better, if I knew why it happens so.

解决方案

The error message comes from builtin/fetch.c#check_not_current_branch().
That function goes all the way back to commit 8ee5d73, Oct. 2008, git 1.6.0.4

The comment is instructive:

Some confusing tutorials suggested that it would be a good idea to fetch into the current branch with something like this:

git fetch origin master:master

(or even worse: the same command line with "pull" instead of "fetch").
While it might make sense to store what you want to pull, it typically is plain wrong when the current branch is "master".
This should only be allowed when (an incorrect) "git pull origin master:master" tries to work around by giving --update-head-ok to underlying "git fetch", and otherwise we should refuse it, but somewhere along the lines we lost that behavior.

The check for the current branch is now only performed in non-bare repositories, which is an improvement from the original behaviour.

Considering that the function check_not_current_branch() is called with:

if (!update_head_ok)
        check_not_current_branch(ref_map);

That means a git fetch -u origin develop:develop should work.

-u
--update-head-ok

By default git fetch refuses to update the head which corresponds to the current branch. This flag disables the check.
This is purely for the internal use for git pull to communicate with git fetch, and unless you are implementing your own Porcelain you are not supposed to use it.

Even though you are not supposed to use that option, it does answer your initial requirement, making "git fetch origin branch:branch" work on a current branch.


Regarding the origin of this patch, follow the discussion there.

While it might make sense to store what you want to pull

That is the fetch part: it stores the remote history from the updated origin/master.
But that is especially broken when the current local branch is also master.
As mentioned in this answer:

I think "git fetch url side:master" when master is the current branch and we have omitted --update-head-ok is broken.
The test fails on current master.

It would also fail to update the working directory and would leave the index as if you're removing everything.

这篇关于为什么“git fetch origin branch:branch”仅适用于非当前分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆