解决git中upstream丢失问题Your branch is based on 'origin/xxxx', but the upstream is gone.

之前使用了git clone 直接把github上自己项目的一个分支拉到了本地,输入:

git status

>Your branch is up-to-date with 'origin/dev'.

表明这时候本地这个分支已经和远程的分支origin/dev已经保持关联。
当本地和远程的该分支有版本不同时,关联后能够很好的知道本地分支和远程分支的差异,以及在提交和更新时也能有很大的方便。
但是我删除了和本地关联的远程分支时,这时候输入:

git status
>Your branch is based on 'origin/home_branch', but the upstream is gone.

这时候提示基于远程分支origin/home_branch已经不见了,失去了关联依然还是可以提交和更新的,但是在效率上会差很多,所以我在网上找了很久,终于找到了在删除远程分支后如何重新让本地分支关联其他分支。

https://stackoverflow.com/questions/21609781/why-call-git-branch-unset-upstream-to-fixup
这是一篇很好的文章解决了我的问题,英文看着头大的同学直接看我写的好了。


You’re not supposed to need to know the exact mechanics of how git sets up a local branch to track a remote one, but they are relevant below, so I’ll show how this works. We start with your local branch name, source. There are two configuration entries using this name, spelled branch.source.remote and branch.source.merge. From the output you showed, it’s clear that these are both set, so that you’d see the following if you ran the given commands:

你不用知道git是怎么把远程和本地分支关联起来的,输入如下两条命令(注意:这里是以本地的source为例):

git config --get branch.source.remote
>origin

这条说明返回的是本地分支连接的远程名称为origin 一般来说默认远程都是这个名字

git config --get branch.source.merge
>refs/heads/master

这条说明本地的这条source分支是和远程的master分支关联的(注意:即使远程的master分支已经被删除了,这里还是会保留master的关联,所以这才是这个问题的关键所在)
联系这两条内容,可以知道远程名为origin,并且本地分支source所关联的远程分支为master
好的,我们接着输入:

git branch -a

这里能把本地的分支和远程的分支全部打印出来,其中对应的origin(前面提到的远程名)的分支就是远程的分支,如果远程的分支上没有我们本地关联的分支,就会出现本文所要解决的问题。

如何解决呢?


这里是解决方法,–set-upstream-to命令来更改本地分支关联的远程分支,这个命令会把branch.source.remote和branch.source.merge都更改

输入(这里把本地分支的关联更改为远程的source分支,确保远程的source事先已经存在):

$ git branch --set-upstream-to=origin/source

这样就更改成功了
输入:

git status
>Your branch is up-to-date with 'origin/source'.

本地分支已经关联上远程的source了。

  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值