Git 推送主分支以外的分支

Git Push Branch Other Than Master

我一直在寻找这方面的可靠答案,但没有真正找到任何相关的东西。我们正在使用远程 git 存储库,我们正在将不是 master 的分支推送到远程存储库。例如:

git checkout -b new_branch
git add --all .
git commit -m "changes"
git push remote new_branch

然而,当我们尝试从那个裸远程存储库克隆时,我们得到错误:

git clone /path/to/repo.git 

warning: remote HEAD refers to nonexistent ref, unable to checkout.

我不确定为什么 HEAD 是分离的而不是指向 new_branch 上的最后一次提交?我们如何克隆这个远程存储库?

可以使用以下任一命令克隆远程仓库

git clone https://github.com/username/repo.git
git clone git@github.com:username/repo.git

让我知道你试图更好地帮助你的确切命令。

warning: remote HEAD refers to nonexistent ref, unable to checkout. 表示远程(裸)存储库包含文件 HEAD 中的分支引用,该引用与同一存储库中的任何已发布分支都不匹配。

请注意,该警告仅表示 git 未进行签出。 克隆的存储库在其他方面都很好。只需执行 git branch -a 查看可能的分支并执行 git checkout the-branch-you-want 解决问题。

这通常是因为该文件的默认内容是 ref: refs/heads/master,它表示如果有人要 clone 这个存储库,他们应该默认克隆分支 refs/heads/master。默认情况下 Git 将创建没有 refs/heads/ 前缀的本地分支(即默认情况下 master)。尝试 git help symbolic-ref 了解更多信息。

这种情况的问题是 Git 没有提供修改 remote 符号引用的方法,所以要么你使用 Git 托管服务提供商的东西已实施(例如设置 - GitHub 中的默认分支,如果您具有管理员权限)或者您必须使用分支名称 master 作为默认分支(因为这是该符号引用的默认值)。

解决这个问题的一种方法是创建一个没有提交的新远程裸仓库,然后执行 git push name-of-the-remote my-special-branch-name 这将导致裸仓库包含一个分支 my-special-branch-nameHEAD 符号引用仍然包含指向 master 的默认值。结果,您将收到上述警告。

参见:this post