git 拒绝获取当前分支
我设置了一个远程存储库,并且可以向其中推送新的更改,但无法从中获取,我总是收到(相当神秘的)错误消息:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果有人因为特别想要获取当前分支而发现这一点,您可以使用
--update-head-ok
标志。来自文档:在某些情况下,我们确实希望实现我们自己的 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:In some cases we do want to implement our own porcelain commands, e.g., automation and tooling.
您要做的就是获取您正在处理的分支。也就是说,您位于 master 分支上并且尝试更新它。那是不可能的。更常见的是更新
remotes/*
分支,然后将其拉入本地分支。也许您想要的是,这将设置要提取到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,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 setfetch
for the remote to something likerefs/heads/*:refs/remotes/otherrepo/*
.另外,如果您位于 master 分支并且想要获得最新版本,那么这应该可以工作
Also this this should work if you are in master branch and wants to get latest try this
要在获取 PR 进行本地测试时处理此问题,只需签出另一个分支,然后获取 PR 即可。
To deal with this when fetching a PR to test locally, just checkout another branch and then fetch the PR.
请注意,此错误消息不仅适用于您当前的分支,还适用于作为工作树签出的任何分支。
“
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 日)使用 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 日)(请参阅“为什么“
git fetch originbranch:branch
”仅适用于非当前分支?”)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)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)(see "Why "
git fetch origin branch:branch
" works only on a non-current branch?")当我不假思索地克隆一个存储库而不是获取它时,我遇到了这个问题,这样两个存储库都是主存储库。如果您没有对远程存储库进行任何操作,则可以使用基本的 git 命令来修复问题,如下所示:(1) 删除远程存储库,(2) 将本地存储库复制到远程存储库所在的位置,(3) 删除本地存储库,然后 (4) 使用设置本地存储库
然后 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
Then
git pull
andgit push
will do the right things. The advantage of avoidinggit remote
, per Michael's more efficient and principled answer, is that you don't need to think about the semantics of tracking branches.我有同样的问题。首先我尝试使用它来获取
我遇到了你提到的同样的问题。之后我尝试了这个简单的命令。
Note 将获取并合并更改。如果您使用终端然后打开文件,需要提交消息。通过此消息,您将推送最新的提交。尝试这个命令,最后你就可以推送请求了。
I have the same problem. First I try to fetch using this
I had the same problem you mention. After that I tried this simple command.
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 命令,还是从您自己的代码运行 Git 可执行文件?
如果您从代码运行它,您确定 Git 正在尝试获取正确的本地目录吗?
有两种可能的方法可以做到这一点:
使用您的编程提供的选项在执行 Git 之前设置正确的工作目录的语言
(C# 示例,因为这就是我正在使用的)
始终传递 Git 的
-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:
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)
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
.当您确实想获取当前分支时 - 例如,例如从孪生存储库中获取 -u origin mybranch:mybranch (并且它不是由于像 OP 情况那样的虚假配置),您可能会更方便地做:
这仅允许快进更新,扩展线性历史记录。与
fetch -u
不同,它已经更新了索引。如果由于提交历史记录不同而导致失败,并且您希望立即再次实现统一(而不是创建合并守护进程等),您可以考虑重新基于远程 - 例如通过:
可以分解为:
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: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:
Which can be broken up into:
致命:拒绝获取非裸存储库的当前分支 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.