我如何git push到尚不存在的目的地

时间:2018-11-19 10:53:29

标签: git

我正在尝试使用哈希在git服务器上创建一个新分支,但也没有在本地创建该分支:

$ git push origin HEAD^:new-branch
error: unable to push to unqualified destination: new-branch
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to 'git@github:company/repo.git'

当您强行推入现有分支时,此方法有效。我已尝试将目的地设为refs/new-branch,它说:

$ git push origin HEAD^:refs/new-branch
Total 0 (delta 0), reused 0 (delta 0)
remote: error: refusing to create funny ref 'refs/new-branch' remotely
To github.com:company/repo.git
 ! [remote rejected]       HEAD^ -> refs/new-branch (funny refname)
error: failed to push some refs to 'git@github.com:company/repo.git'

尚不存在的目的地的格式是什么?

我将像这样解决它

$ git branch new-branch
$ git push origin new-branch
$ git push origin HEAD^:new-branch -f
$ git branch -D new-branch

或类似的

$ git branch new-branch HEAD^
$ git push origin new-branch
$ git branch -D new-branch

但将是要知道如何在服务器上创建新分支而不用在本地创建

1 个答案:

答案 0 :(得分:2)

分支是refs/heads/...,而不只是refs/...,因此您应推送到refs/heads/new-branch在远程存储库上创建新分支。

git push origin HEAD^:refs/heads/new-branch