我已经使用了git push命令,它到目前为止已按预期工作。在今天测试git命令时,我发现推送到HEAD而不是master工作没有错误,如下所示。 我想了解这两个命令之间的区别,以便我可以选择正确的命令。
# git push origin master
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of known hosts.
To git@github.com:shan/mobileapp
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:shan/mobileapp'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again. See the
'Note about fast-forwards' section of 'git push --help' for details.
# git push origin HEAD
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of known hosts.
Counting objects: 23, done.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (19/19), 2.56 KiB, done.
Total 19 (delta 11), reused 0 (delta 0)
To git@github.com:shan/mobileapp
* [new branch] HEAD -> topic/wip
将其推向头部似乎已经创建了名为topic / wip的分支
我现在可以将分支主题/ wip与master合并。但我希望上面提到的第一个命令按预期工作。而且我不需要创建一个临时分支来与master合并。
答案 0 :(得分:2)
在git中HEAD
基本上是指“当前提交”。因此,当您签出wip
分支时,git push origin HEAD
相当于git push origin wip
,它将推送到远程分支origin/wip
,如果需要则创建它,如在你的第二个例子中显示。
您无法掌握的原因是您的本地分支副本已过时。在推送之前,您需要更新本地副本:git pull origin master
。