git: 记一次更换git远程仓库引发的问题


前言

最近把git仓库从github切换到gitee。然而在本地更换repo的时候遇到一些问题,各种谷歌也没解决掉,具体表现如下:

更换到gitee后执行git pull仍然使用的是旧repo (github)的问题。因此整理一下git重命名repo的操作

解决方法传送门

重命名remote

git remote rename {old\_name} {new\_name}

添加新remote

git remote add {name} {git-repo}

在两个仓库都有内容的情况下更换 origin

git remote rename origin github
# 添加新的仓库地址
git remote add origin {new_repo}
# 查看是否替换成功
git remote -v

rename出现的pull的仍然是旧repo的问题

简短版
# 执行git pull origin如果提示如下信息,就是配置问题
# 因为`.git/config`中配置了master(其他分支同理)的默认远程分支还是旧的
$ git pull origin
You asked to pull from the remote 'origin', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

# 查看配置, 发现[branch "master"]是旧的.直接删除或者使用git 命令替换即可
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "github"]
        url = git@old.git
        fetch = +refs/heads/*:refs/remotes/github/*
[branch "master"]
        remote = github
        merge = refs/heads/master
[remote "origin"]
        url = git@new.git
        fetch = +refs/heads/*:refs/remotes/origin/*

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

# 验证一下,已经使用新repo了
$ git pull  -v
From new
 = [up to date]      master     -> origin/master
 = [up to date]      release    -> origin/release
Already up to date.

整个历程

# 查看repo配置正常
$ git remote -v

# 提示下一次fetch会使用新的repo
$ git remote show origin
* remote origin
  Fetch URL: git@new.git
  Push  URL: git@new.git
  HEAD branch: master
  Remote branches:
    master  new (next fetch will store in remotes/origin)
    release new (next fetch will store in remotes/origin)
  Local ref configured for 'git push':
    master pushes to master (up to date)
# fetch 新repo正常,再次show origin已经正常
$ git fetch origin
From new
 * [new branch]      master     -> origin/master
 * [new branch]      release    -> origin/release
$ git remote show origin
* remote origin
  Fetch URL: git@new.git
  Push  URL: git@new.git
  HEAD branch: master
  Remote branches:
    master  tracked
    release tracked
  Local ref configured for 'git push':
    master pushes to master (up to date)

# 然后问题来了,此时git pull有问题。使用的仍然是旧repo
$ git pull -v
Enter passphrase for key '/home/simon/.ssh/id_rsa':
From old
 = [up to date]      master     -> github/master
 = [up to date]      release    -> github/release
Already up to date.

# 此时pull和push分支正常
$ git push origin master -v
Pushing to git@new.git
To new
 = [up to date]      master -> master
updating local tracking ref 'refs/remotes/origin/master'
Everything up-to-date

# 难道pull也和fetch一样,需要先pull origin一下?
$ git pull origin
You asked to pull from the remote 'origin', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

# 仔细看了git pull的提示信息,发现可能是配置了git pull的时候默认分支有问题
# 看了一下果然是的
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "github"]
        url = old
        fetch = +refs/heads/*:refs/remotes/github/*
[branch "master"]
        remote = github
        merge = refs/heads/master
[remote "origin"]
        url = new
        fetch = +refs/heads/*:refs/remotes/origin/*

# 此时修改对应branch的默认拉取的远程分支即可。也可以直接删除config里对应的配置
$ git branch --set-upstream-to=origin/master master
Branch 'master' set up to track remote branch 'master' from 'origin'.

# 或者
$ git branch master -u origin/master

我的github pages: perfectnewer.gitub.io

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值