Git 设置远程分支

综述

Git 设置本地分支对应的远程分支的两类方法

# Set upstream when pushing to remote
git push -u origin test

# Set upstream without pushing it
# with option -u / --set-upstream-to
git branch -u origin/test
git branch --set-upstream-to=origin/test
复制代码

git push

可以在本地分支在第一次push时设置远程分支。可以通过选项-u或——set-upstream为每个更新或推送的分支设置远程引用。例如,我的本地仓库checkout一个分支test,此时提交到git仓库时,可以使用

$ git push -u origin test
复制代码

git branch

如果你不想push,只是单纯的想设置本地分支对应的远程分支,可以通过下面命令实现

git branch -u origin/test
Branch 'test' set up to track remote branch 'test' from 'origin'.

复制代码

或者

git branch --set-upstream-to=origin/test
Branch 'test' set up to track remote branch 'test' from 'origin'.

复制代码

注意,使用git branch 的时候,如果远程分支不存在,则会提示报错:

>git branch -u origin/test
error: the requested upstream branch 'origin/test' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.

复制代码

提示信息中说明了,需要使用git push 去设置远程分支。

一旦设置好了,可以通过下面命令查看git的分支对应情况

git branch -vv
* test a1304b7 [origin/test] bug fix
  full 665c405 [origin/full] fix unit test bug
  master        21230b8 [origin/master: behind 20] Merge branch 'test' into 'master'
复制代码

取消设置

$ git branch --unset-upstream [<branchname>]
复制代码
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值