git push到指定分支方法详解

分类:知识百科 日期: 点击:0

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 命令将远程仓库的更新拉取到本地,再推送至远程仓库即可。

标签:

版权声明

1. 本站所有素材,仅限学习交流,仅展示部分内容,如需查看完整内容,请下载原文件。
2. 会员在本站下载的所有素材,只拥有使用权,著作权归原作者所有。
3. 所有素材,未经合法授权,请勿用于商业用途,会员不得以任何形式发布、传播、复制、转售该素材,否则一律封号处理。
4. 如果素材损害你的权益请联系客服QQ:77594475 处理。