git push 到指定分支方法
Git push 命令用于将本地分支的更新,推送至Git远程仓库。通常我们使用 git push origin master 将本地master分支推送至origin远程仓库的master分支,但有时候我们需要将本地分支推送至远程仓库的其他分支,这时候就需要用到git push 命令的 --set-upstream 参数。
使用方法
我们在本地创建一个新的分支,命令如下:
git branch newbranch
我们将本地新分支推送至远程仓库:
git push origin newbranch
这时,我们会收到提示:
fatal: The current branch newbranch has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin newbranch
我们按照提示,使用如下命令:
git push --set-upstream origin newbranch
这样,就可以将本地分支newbranch推送至远程仓库的newbranch分支了。
如果我们想将本地分支推送至远程仓库的其他分支,比如develop,我们可以使用如下命令:
git push --set-upstream origin newbranch:develop
这样,就可以将本地分支newbranch推送至远程仓库的develop分支了。
如果我们想将本地分支推送至远程仓库的多个分支,比如develop和test,我们可以使用如下命令:
git push --set-upstream origin newbranch:develop newbranch:test
这样,就可以将本地分支newbranch推送至远程仓库的develop和test分支了。
需要注意的是,如果远程仓库中已经存在相同的分支,则我们在推送时可能会报错,比如:
! [rejected] newbranch -> develop (non-fast-forward) error: failed to push some refs to 'git@github.com:xxx/xxx.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
此时,我们可以使用 git pull 命令将远程仓库的更新拉取到本地,再推送至远程仓库即可。