git 拒绝获取当前分支

发布于 2024-08-21 02:58:08 字数 456 浏览 9 评论 0 原文

我设置了一个远程存储库,并且可以向其中推送新的更改,但无法从中获取,我总是收到(相当神秘的)错误消息:

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

这是什么意思?我应该怎么做才能启用抓取?

注意,这个远程存储库仅用作备份存储库,因此它应该几乎是我的本地存储库的精确副本。我真的不明白为什么我可以推送它但不能从中获取......)

(请 配置看起来像:

[remote "origin"]
    url = ssh://blablablah
    fetch = +refs/*:refs/*
    mirror = true

I set up a remote repository and I can push new changes to it, but I cannot fetch from it, I always get the (rather cryptic) error message:

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

What does it mean? What should I do to enable fetching?

(Note that this remote repo is only used as a backup repo, so it should be pretty much an exact copy of my local repository. I really can't understand why I can push to it but not fetch from it...)

My config looks like:

[remote "origin"]
    url = ssh://blablablah
    fetch = +refs/*:refs/*
    mirror = true

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

不如归去 2024-08-28 02:58:08

如果有人因为特别想要获取当前分支而发现这一点,您可以使用 --update-head-ok 标志。来自文档

-u
--更新头-确定
默认情况下 git fetch 拒绝更新与当前分支相对应的头。该标志禁用检查。这纯粹是为了 git pull 与 git fetch 进行通信的内部使用,除非您正在实现自己的 Porcelain,否则不应该使用它。

在某些情况下,我们确实希望实现我们自己的 Porcelain命令,例如自动化和工具。

In case anyone finds this because they specifically want to fetch into the current branch, you can use the --update-head-ok flag. From the docs:

-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.

In some cases we do want to implement our own porcelain commands, e.g., automation and tooling.

愿得七秒忆 2024-08-28 02:58:08

您要做的就是获取您正在处理的分支。也就是说,您位于 master 分支上并且尝试更新它。那是不可能的。更常见的是更新 remotes/* 分支,然后将其拉入本地分支。也许您想要的是,

git remote add otherrepo thehost:/the/path.git

这将设置要提取到remotes/otherrepo/* 中的存储库。 git fetch otherrepo 应该可以解决问题。或者,您可以手动编辑 .git/config 并将遥控器的 fetch 设置为 refs/heads/*:refs/remotes/otherrepo/*

What you're trying to do is to fetch the branch you're workin on. That is, you are on the master branch and you try to update it. That's not possible. It's more common to update the remotes/* branches and then pull it into your local ones. What you want is, perhaps,

git remote add otherrepo thehost:/the/path.git

That will setup repository to be fetched into remotes/otherrepo/*. git fetch otherrepo should do the trick. Alternativeley, you can manually edit your .git/config and set fetch for the remote to something like refs/heads/*:refs/remotes/otherrepo/*.

很酷又爱笑 2024-08-28 02:58:08

另外,如果您位于 ma​​ster 分支并且想要获得最新版本,那么这应该可以工作

git pull origin ma​​ster

Also this this should work if you are in master branch and wants to get latest try this

git pull origin master

饭团 2024-08-28 02:58:08

要在获取 PR 进行本地测试时处理此问题,只需签出另一个分支,然后获取 PR 即可。

git checkout -b feature-branch # create a branch to fetch changes into.
git checkout master # or main
git fetch origin pull/5/head:add-feature # fetch pull request by ID
git checkout feature-branch

To deal with this when fetching a PR to test locally, just checkout another branch and then fetch the PR.

git checkout -b feature-branch # create a branch to fetch changes into.
git checkout master # or main
git fetch origin pull/5/head:add-feature # fetch pull request by ID
git checkout feature-branch
各空 2024-08-28 02:58:08

请注意,此错误消息不仅适用于您当前的分支,还适用于作为工作树签出的任何分支。

git fetch(man) 没有 -- update-head-ok 选项应该保护签出的分支不被更新,以防止签出分支的工作树不同步。

代码是在使用“git worktree之前编写的(man) 得到广泛传播,并且只检查当前工作树中签出的分支,该工作树已使用 Git 2.35(2022 年第一季度)更新。

请参阅 提交 593a2a5提交 9fdf4f1, 提交38baae6提交 8bc1f39提交 c8dd491, 提交 7435e7e提交 c25edee提交 66996be(2021 年 12 月 1 日),作者:Anders Kaseorg (andersk)
(由 Junio C Hamano -- gitster -- 合并于 提交 13fa77b,2021 年 12 月 21 日)

fetch:保护所有签出的分支工作树

签字人:Anders Kaseorg

拒绝获取任何工作树当前签出的分支,而不仅仅是当前的分支。

修复了之前报告的问题错误

作为使用 find_shared_symref 的副作用,当我们位于分离的 HEAD 上时,我们也会拒绝提取,因为我们正在对相关分支进行变基或平分。
这似乎是一个明智的改变。


使用 Git 2.37(2022 年第 3 季度),“ git fetch"(man)< /sup> 没有 --update-head-ok 确保没有工作树中的 HEAD 指向任何正在更新的引用,这太浪费了,这已经被优化了一点。

请参阅 提交 f7400da(2022 年 5 月 16 日),作者:Orgad Shaneh (orgads)
(由 Junio C Hamano -- gitster -- 合并于 提交 5ed49a7,2022 年 5 月 25 日)

fetch:仅限制共享符号引用检查当地分支机构

签署人:Orgad Shaneh

此检查是在 8ee5d73 中引入的(“修复在没有--update-head-ok”,2008-10-13,Git v1.6.1-rc1 -- merge),以防止错误地替换活动分支的引用,例如通过运行 git fetch origin master:master(man) .

(请参阅“为什么“git fetch originbranch:branch”仅适用于非当前分支?”)

后来在 8bc1f39 ("fetch:保护在所有工作树中签出的分支”,2021-12-01,Git v2.35.0-rc0 -- 合并列于批次#4)以扫描所有工作树.

当有很多标签或分支时,此操作非常昂贵(在我的存储库中大约需要 30 秒),并且它会在每次获取时执行,即使根本没有更新本地头。

将其限制为仅保护 refs/heads/* 以提高获取性能。

Note that this error message won't apply just to your current branch, but to any branch checked out as a worktree.

"git fetch"(man) without the --update-head-ok option ought to protect a checked out branch from getting updated, to prevent the working tree that checks it out to go out of sync.

The code was written before the use of "git worktree"(man) got widespread, and only checked the branch that was checked out in the current worktree, which has been updated with Git 2.35 (Q1 2022).

See commit 593a2a5, commit 9fdf4f1, commit 38baae6, commit 8bc1f39, commit c8dd491, commit 7435e7e, commit c25edee, commit 66996be (01 Dec 2021) by Anders Kaseorg (andersk).
(Merged by Junio C Hamano -- gitster -- in commit 13fa77b, 21 Dec 2021)

fetch: protect branches checked out in all worktrees

Signed-off-by: Anders Kaseorg

Refuse to fetch into the currently checked out branch of any working tree, not just the current one.

Fixes this previously reported bug.

As a side effect of using find_shared_symref, we'll also refuse the fetch when we're on a detached HEAD because we're rebasing or bisecting on the branch in question.
This seems like a sensible change.


With Git 2.37 (Q3 2022), the way "git fetch"(man) without --update-head-ok ensures that HEAD in no worktree points at any ref being updated was too wasteful, which has been optimized a bit.

See commit f7400da (16 May 2022) by Orgad Shaneh (orgads).
(Merged by Junio C Hamano -- gitster -- in commit 5ed49a7, 25 May 2022)

fetch: limit shared symref check only for local branches

Signed-off-by: Orgad Shaneh

This check was introduced in 8ee5d73 ("Fix fetch/pull when run without --update-head-ok", 2008-10-13, Git v1.6.1-rc1 -- merge) in order to protect against replacing the ref of the active branch by mistake, for example by running git fetch origin master:master(man) .

(see "Why "git fetch origin branch:branch" works only on a non-current branch?")

It was later extended in 8bc1f39 ("fetch: protect branches checked out in all worktrees", 2021-12-01, Git v2.35.0-rc0 -- merge listed in batch #4) to scan all worktrees.

This operation is very expensive (takes about 30s in my repository) when there are many tags or branches, and it is executed on every fetch, even if no local heads are updated at all.

Limit it to protect only refs/heads/* to improve fetch performance.

中性美 2024-08-28 02:58:08

当我不假思索地克隆一个存储库而不是获取它时,我遇到了这个问题,这样两个存储库都是主存储库。如果您没有对远程存储库进行任何操作,则可以使用基本的 git 命令来修复问题,如下所示:(1) 删除远程存储库,(2) 将本地存储库复制到远程存储库所在的位置,(3) 删除本地存储库,然后 (4) 使用设置本地存储库

git init; git fetch $REMOTE_GIT_REPOSITORY_URL  

然后 git pull 和 git push 会做正确的事情。根据 Michael 更高效、更有原则的答案,避免使用 git remote 的好处是,您不需要考虑跟踪分支的语义。

I've had this problem when I thoughtlessly cloned a repository instead of fetching it, so that both repositories were masters. If you have done no work on the remote repository, you can fix things with the basic git commands as follows: (1) delete the remote repository, (2) copy the local repository to where the remote one was, (3) delete the local one, and then (4) set up the local repository using

git init; git fetch $REMOTE_GIT_REPOSITORY_URL  

Then git pull and git push will do the right things. The advantage of avoiding git remote, per Michael's more efficient and principled answer, is that you don't need to think about the semantics of tracking branches.

风吹雨成花 2024-08-28 02:58:08

我有同样的问题。首先我尝试使用它来获取

git fetch [remote_name] [branch_name] 

我遇到了你提到的同样的问题。之后我尝试了这个简单的命令。

git pull [remote_name] [branch_name]

Note 将获取并合并更改。如果您使用终端然后打开文件,需要提交消息。通过此消息,您将推送最新的提交。尝试这个命令,最后你就可以推送请求了。

git push [remote_name] [branch_name_local] 

I have the same problem. First I try to fetch using this

git fetch [remote_name] [branch_name] 

I had the same problem you mention. After that I tried this simple command.

git pull [remote_name] [branch_name]

Note will fetch and merge the changes. If you using terminal then file open, requiring to commit message. With this message you will push the latest commit. Try this command and finally you will be able to push the request.

git push [remote_name] [branch_name_local] 
淡笑忘祈一世凡恋 2024-08-28 02:58:08

您实际上是在命令行中输入 Git 命令,还是从您自己的代码运行 Git 可执行文件?
如果您从代码运行它,您确定 Git 正在尝试获取正确的本地目录吗?

有两种可能的方法可以做到这一点:

  1. 使用您的编程提供的选项在执行 Git 之前设置正确的工作目录的语言
    C# 示例,因为这就是我正在使用的)

  2. 始终传递 Gi​​t 的 -C 参数 用于指定本地存储库的目录。


我有一个项目,我从 C# 代码调用 Git 可执行文件,当我不小心忘记设置 -C 参数 时,我收到了与问题中相同的错误消息。

Are you actually typing Git commands into the command line, or are you running the Git executable from your own code?
If you're running it from code, are you sure that Git is trying to fetch into the correct local directory?

There are two possible ways to do this:

  1. Use the options provided by your programming language to set the correct working directory before executing Git
    (C# example, because that's what I'm using)

  2. Always pass the -C parameter to Git to specify the directory with the local repo.


I have a project where I'm calling the Git executable from C# code, and I got the same error message like in the question when I accidentally forgot to set the -C parameter.

三五鸿雁 2024-08-28 02:58:08

当您确实想获取当前分支时 - 例如,例如从孪生存储库中获取 -u origin mybranch:mybranch (并且它不是由于像 OP 情况那样的虚假配置),您可能会更方便地做:

git pull [origin] --ff-only

这仅允许快进更新,扩展线性历史记录。与 fetch -u 不同,它已经更新了索引。

如果由于提交历史记录不同而导致失败,并且您希望立即再次实现统一(而不是创建合并守护进程等),您可以考虑重新基于远程 - 例如通过:

git pull --rebase[=interactive]

可以分解为:

git fetch origin mybranch
# inspect the diverge situation 
git rebase origin/mybranch mybranch [-i]...

When you really want to fetch into the current branch - e.g. like fetch -u origin mybranch:mybranch from a twin repo (and its not due to a bogus config like in the OP case), you may more conveniently do:

git pull [origin] --ff-only

This allows only a fast-forward update, extending a linear history. It already updates the index unlike the fetch -u.

In case this fails due to diverged commit history and you want to achieve unity again immediately (instead of creating merge daimonds etc.), you may consider rebasing onto the remote - e.g. by:

git pull --rebase[=interactive]

Which can be broken up into:

git fetch origin mybranch
# inspect the diverge situation 
git rebase origin/mybranch mybranch [-i]...
怕倦 2024-08-28 02:58:08

致命:拒绝获取非裸存储库的当前分支 refs/heads/BRANCHNAME

我已在本地创建了一个分支 BRANCHNAME,然后执行命令“git fetchupstream pull/ID/head:BRANCHNAME
并得到致命:拒绝获取非裸存储库的当前分支 refs/heads/BRANCHNAME

然后我删除了该分支并再次调用相同的cmd,一切正常。

实际上我正在检查拉取请求分支。

fatal: Refusing to fetch into current branch refs/heads/BRANCHNAME of non-bare repository

I have Created a branch BRANCHNAME locally then executed command "git fetch upstream pull/ID/head:BRANCHNAME"
and got fatal: Refusing to fetch into current branch refs/heads/BRANCHNAME of non-bare repository

then i deleted the branch and again call the same cmd it ways fine.

Actually i was checking out pull request branch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文