Git远程仓库地址变更本地代码仓库如何修改

906 阅读1分钟

大家好,我是前端小贺,希望能够通过自己的学习输出给你带来帮助。

背景

最近公司代码准备从私有gitlab迁移到阿里的云效。就整理下如何去修改本地的git配置去链接到新的远程仓库。

正解

以下均以repo:gittest为例演示:

新地址:newgit.com/gittest.git

旧地址:oldgit.com/gittest.git

方法一 通过命令行直接修改远程仓库地址

  1. 进入gittest仓库的根目录
  2. 可以通过git remote -v查看下现在的远程仓库地址
➜  gittest git:(test) git remote -v
origin	https://oldgit.com/gittest.git (fetch)
origin	https://oldgit.com/gittest.git (push)
  1. git remote set-url origin https://newgit.com/gittest.git
  2. 通过git remote -v验证下现在的远程仓库地址
➜  gittest git:(test) git remote -v
origin	https://newgit.com/gittest.git (fetch)
origin	https://newgit.com/gittest.git (push)

方法二 直接修改当前目录下的git配置文件

  1. vim gittest/.git/config

    修改[remote "origin"]下面的url即可

    [core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
            ignorecase = true
            precomposeunicode = true
    [remote "origin"]
            # 原来的 url = https://oldgit.com/gittest.git
            url = https://newgit.com/gittest.git
            fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
            remote = origin
            merge = refs/heads/master
    [branch "dev"]
            remote = origin
            merge = refs/heads/dev
    

方法三 通过命令行先删除再添加远程仓库

  1. 进入gittest仓库的根目录

  2. 可以通过git remote -v查看下现在的远程仓库地址

    ➜  gittest git:(test) git remote -v
    origin	https://oldgit.com/gittest.git (fetch)
    origin	https://oldgit.com/gittest.git (push)
    
  3. ➜  git remote rm origin
    ➜  git remote add origin https://newgit.com/gittest.git
    
  4. 通过git remote -v验证下现在的远程仓库地址

    ➜  gittest git:(test) git remote -v
    origin	https://newgit.com/gittest.git (fetch)
    origin	https://newgit.com/gittest.git (push)
    

参考

git-scm.com/docs/git-re…

blog.csdn.net/asdfsfsdgdf…

最后

您的每一个点赞及评论都是对我坚持写作最大的支持! 另外希望各位朋友和我交流讨论,如有不对的地方,更希望批评指正!

我是前端小贺,希望能够通过自己的学习输出给你带来帮助。