【问题标题】:git push error: dst refspec refs/heads/main matches more than onegit push 错误:dst refspec refs/heads/main 匹配多个
【发布时间】:2022-01-04 19:52:02
【问题描述】:

目前,我不得不在每次更改时都将其推送到新分支。并且必须删除该分支,因为每个新创建的分支都会再次出现错误。

git push origin main 给出以下输出

error: dst refspec refs/heads/main matches more than one
error: failed to push some refs to 'https://github.com/CarloCattano/fastesTube'

我的git tag 输出:

refs/heads/dev
refs/heads/dev2
refs/heads/dev3
refs/heads/main
v1.1
win64 

无论我是否使用-d 手动删除它们。 甚至尝试将项目迁移到新的 repo ,几次推送后问题仍然存在。

git remote -v

origin  https://github.com/CarloCattano/fastesTube (fetch)
origin  https://github.com/CarloCattano/fastesTube (push)

git ls-remote

ac4cac50b79ff682ddd01f6c0c3913d0bd765e64        HEAD
77273d612953f96e72ce305ab94f0a535a4c332d        refs/heads/dev3
3c344e7af2feb33db2d05f08866cad5fe624b57c        refs/heads/develop
ac4cac50b79ff682ddd01f6c0c3913d0bd765e64        refs/heads/main
fde3bb1ed7c770a5b8eb94a368bb34f25566f00e        refs/pull/1/head
ffe33059c3fcc12899953bc588772072d9a18bf0        refs/pull/2/head
77273d612953f96e72ce305ab94f0a535a4c332d        refs/pull/3/head
3c344e7af2feb33db2d05f08866cad5fe624b57c        refs/pull/4/head
b9d1c3f8b83ea1ac868143ec647776d03f9bacc7        refs/tags/refs/heads/dev
ffe33059c3fcc12899953bc588772072d9a18bf0        refs/tags/refs/heads/dev2
77273d612953f96e72ce305ab94f0a535a4c332d        refs/tags/refs/heads/dev3
4098ea71b5a0873db6be41e859e5b8242d81c708        refs/tags/refs/heads/main
a42341ba40635bd5063a0bf988eab6c00b0e62d1        refs/tags/v1.1
37220afec1d13dcac99c61ef766ac800fc6438f5        refs/tags/win64

强制推送似乎也不起作用。

可能是我错误地配置了 .yml 文件并为每个版本创建了标签。

【问题讨论】:

    标签: git github continuous-integration tags branch


    【解决方案1】:

    这里的问题是你创建了名为refs/heads/...的标签(三个点都填上)。

    在 Git 中,ref 或 reference 是一个通常以 refs/ 开头并具有命名空间限定符的字符串:

    • refs/heads/* 是 branch 名称:与 * 匹配的部分是分支名称;
    • refs/tags/* 是 tag 名称:与* 匹配的部分是标签名称;
    • refs/remotes/* 是 remote-tracking 名称;

    等等。通常,您可以给 Git 一个缩写名称,例如 main 或 v1.1,它可以通过查看现有名称​​确定这个名称是分支名称还是标签名称:

    • 通常有refs/heads/main 或refs/heads/master,但没有refs/tags/main 或refs/tags/master,因此main 或master 因此是一个分支 名称。
    • 可能有refs/tags/v1.1,但如果是这样,通常没有refs/heads/v1.1,因此v1.1 是一个标签名称。

    当使用这种方案时,您提供一个模棱两可的名称,例如 main 或 v1.1,Git 会自行判断它是分支名称还是标签名称,或您提供 full 名称,例如 refs/heads/main,Git 立即知道它是一个 branch 名称。前面没有refs/heads/ 或refs/tags/ 限定符的短名称是一个非限定名称。全名是一个限定名。

    git push 命令比大多数其他 Git 命令更复杂(除了 git fetch 也同样复杂),因为它必须处理 两个 Git 存储库,而不仅仅是一个。因此,git push 可以采用 refspec 代替 ref,它是一对用冒号 : 分隔的 ref。如果您使用完整的参考规范:

    git push origin refs/heads/main:refs/tags/v1.2
    

    那么左边和右边的部分都是refs,每个部分要么不合格,要么合格。可以通过查看本地 Git 存储库的名称(对于本地的)或远程的名称(对于远程的)来解析不合格的名称。

    但是,如果您使用 部分 refspec:

    git push origin main
    

    然后 Git 不确定您的意思是“在本地找到的 main”还是“在远程找到的 main”。因此,Git 会在这两个地方进行查找以做出最好的猜测。

    然而,在这种情况下,destinationGit(其引用显示在您的git ls-remote 输出中)同时具有refs/heads/main 和 refs/tags/refs/heads/main。所以,你的 Git 已经在本地查找了 main 并找到了 refs/heads/main;它现在在另一个 Git 存储库中的一组 ref 中查找它,并且 无法将其仅转换为一个 ref,因为 both refs/heads/main - 一个完全合格的分支名称——和refs/tags/refs/heads/main——一个完全限定的标签名称——匹配潜在的非限定refs/heads/main。结果是您收到此错误消息。

    您是否会收到git push origin refs/heads/main:refs/heads/main 的此错误,我不知道。不过,最好的解决方法是更正 destination 上的名称集,以便不再有 refs/tags/refs/* 名称。也就是这四个名字:

    b9d1c3f8b83ea1ac868143ec647776d03f9bacc7        refs/tags/refs/heads/dev
    ffe33059c3fcc12899953bc588772072d9a18bf0        refs/tags/refs/heads/dev2
    77273d612953f96e72ce305ab94f0a535a4c332d        refs/tags/refs/heads/dev3
    4098ea71b5a0873db6be41e859e5b8242d81c708        refs/tags/refs/heads/main
    

    应该调整(或完全删除),以便在 GitHub 上的 Git 存储库中没有标签名称以 refs/ 开头。理想情况下,这些标签名称也不应与任何分支名称匹配:标签名称通常应符合 v* 形式,或其他一些“显然是标签”模式,这样没人会意外地认为它们是分支名称,反之亦然.

    一旦你解决了这个问题,简单的git push 命令将再次起作用。

    【讨论】:

    • 感谢您提供如此详细的答复。删除标签和额外的分支就可以了。只是有一些关于“电子邮件隐私设置”的有趣错误,必须将其恢复为公开才能推送,但仅此而已。再次感谢如此详细和翔实的答案!
    猜你喜欢
    • 2019-10-26
    • 1970-01-01
    • 2012-11-02
    • 2022-07-14
    • 2018-01-28
    • 2021-03-18
    • 2013-09-05
    • 2022-12-06
    • 2017-12-07
    相关资源
    最近更新 更多