首页 文章

如何更改远程Git存储库的URI(URL)?

提问于
浏览
2943

我在我的硬盘驱动器(本地)上克隆的USB密钥上有一个repo(origin) . 我将“origin”移动到NAS并成功测试从这里克隆它 .

我想知道我是否可以在“本地”设置中更改“origin”的URI,因此它现在将从NAS中提取,而不是从USB密钥中提取 .

现在,我可以看到两个解决方案:

  • 将所有内容推送到usb-orign,然后再将其复制到NAS(由于对nas-origin的新提交,意味着很多工作);

  • 将新遥控器添加到“本地”并删除旧遥控器(我担心我会破坏我的历史记录) .

12 回答

  • 629
    • 使用gitbash git remote rm origin 上的命令删除原点

    • 现在使用gitbash git remote add origin (在位桶中复制项目存储库中的HTTP URL)添加新的Origin

  • 4795

    Change Host for a Git Origin Server

    来自:http://pseudofish.com/blog/2010/06/28/change-host-for-a-git-origin-server/

    希望这不是你需要做的事情 . 我一直用来与几个git项目合作的服务器让域名过期 . 这意味着要找到一种迁移本地存储库以重新同步的方法 .

    更新:感谢@mawolf指出最近的git版本有一个简单的方法(2010年2月后):

    git remote set-url origin ssh://newhost.com/usr/local/gitroot/myproject.git
    

    有关详细信息,请参见手册页 .

    如果您使用的是旧版本,请尝试以下操作:

    作为一个警告,这只是因为它是相同的服务器,只是使用不同的名称 .

    假设新主机名是 newhost.com ,而旧主机名是 oldhost.com ,则更改非常简单 .

    编辑工作目录中的 .git/config 文件 . 你应该看到类似的东西:

    [remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ssh://oldhost.com/usr/local/gitroot/myproject.git
    

    oldhost.com 更改为 newhost.com ,保存文件即可完成 .

    从我的有限测试( git pull origin; git push origin; gitx )来看,一切似乎都是有序的 . 是的,我知道混淆git内部结构是不好的形式 .

  • 5

    Troubleshooting :

    尝试更改遥控器时可能会遇到这些错误 . 没有这么遥远的'[名字]'

    此错误表示您尝试更改的远程不存在:

    git remote set-url sofake https://github.com/octocat/Spoon-Knife致命:没有这样的远程'sofake'

    检查您是否正确输入了远程名称 .

    参考:https://help.github.com/articles/changing-a-remote-s-url/

  • 0
    git remote -v
    # View existing remotes
    # origin  https://github.com/user/repo.git (fetch)
    # origin  https://github.com/user/repo.git (push)
    
    git remote set-url origin https://github.com/user/repo2.git
    # Change the 'origin' remote's URL
    
    git remote -v
    # Verify new remote URL
    # origin  https://github.com/user/repo2.git (fetch)
    # origin  https://github.com/user/repo2.git (push)
    

    Changing a remote's URL

  • 39

    如果你克隆了你的本地会自动编组,

    克隆它的远程URL .

    你可以用 git remote -v 检查一下

    如果你想改变它,

    git remote set-url origin https://github.io/my_repo.git
    

    这里,

    origin - your branch

    如果你想 overwrite 现有分支你仍然可以使用它..它将覆盖你现有的...它会做,

    git remote remove url
    and 
    git remote add origin url
    

    为了你...

  • 15

    git remote set-url

    ex) git remote set-url origin https://github.com/myName/GitTest.git

  • 7

    我工作:

    git remote set-url origin <project>
    
  • 16

    在Git Bash中,输入命令:

    git remote set-url origin https://NewRepoLink.git

    输入凭据

    完成

  • 6

    如果您正在使用TortoiseGit,请按照以下步骤操作:

    • 转到您当地的结帐文件夹,然后右键单击转到 TortoiseGit -> Settings

    • 在左侧窗格中选择 Git -> Remote

    • 在右侧窗格中选择 origin

    • 现在将 URL 文本框值更改为新远程存储库所在的位置

    您的分支机构和所有本地提交将保持不变,您可以像以前一样继续工作 .

  • 79
    git remote set-url origin git://new.location
    

    (或者,打开 .git/config ,查找 [remote "origin"] ,并编辑 url = 行 .

    您可以通过检查遥控器来检查它是否有效:

    git remote -v
    # origin  git://new.location (fetch)
    # origin  git://new.location (push)
    

    下次推送时,您必须指定新的上游分支,例如:

    git push -u origin master
    

    另见:GitHub: Changing a remote's URL

  • 5

    您可以

    git remote set-url origin git://new.url.here
    

    (请参阅 git help remote )或者您只需编辑 .git/config 并更改其中的网址即可 . 你're not in any danger of losing history unless you do something very silly (and if you'很担心,只需复制你的回购,因为你的回购是你的历史 . )

  • 14

    切换远程URL

    开放式终端 .

    Ist Step: - 将当前工作目录更改为本地项目 .

    2nd Step: - 列出现有的遥控器,以获取要更改的遥控器的名称 .

    3rd Step: - git remote -v

    origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
    
    origin  https://github.com/USERNAME/REPOSITORY.git (push)
    

    使用git remote set-url命令将远程的URL从HTTPS更改为SSH .

    4th Step: - git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

    现在验证远程URL是否已更改 .

    5th Step: - git remote -v 验证新的远程URL

    origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
    origin  git@github.com:USERNAME/REPOSITORY.git (push)
    

相关问题