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

问题描述 投票:3181回答:14

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

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

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

  • 将所有内容推送到usb-orign,并将其再次复制到NAS(由于对来源的新提交而意味着大量的工作);
  • 添加一个新的遥控器到“本地”并删除旧遥控器(我担心我会打破我的历史)。
git url git-remote
14个回答
5351
投票

您可以

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

(请参阅git help remote)或者您可以编辑.git/config并更改其中的URL。除非你做一些非常愚蠢的事情,否则你不会有失去历史的危险(如果你担心,只要复制你的回购,因为你的回购是你的历史。)


7
投票

我工作:

git remote set-url origin <project>

6
投票

在Git Bash中,输入命令:

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

输入凭据

完成


6
投票

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

  1. 转到您当地的结帐文件夹,然后右键单击转到TortoiseGit -> Settings
  2. 在左侧窗格中选择Git -> Remote
  3. 在右侧窗格中选择origin
  4. 现在将URL文本框值更改为新远程存储库所在的位置

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


5
投票

你有很多方法可以做到这一点:

安慰

git remote set-url origin [Here new url] 

请确保您已在存储库所在的位置打开它。

配置

它放在.git / config(与存储库相同的文件夹)中

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = [Here new url]  <------------------------------------
...

TortoiseGit

Step 1 - open settings

Step 2 - change url

然后只需编辑URL。

SourceTree

  1. 单击工具栏上的“设置”按钮以打开“存储库设置”窗口。
  2. 单击“添加”以将远程存储库路径添加到存储库。将打开“远程详细信息”窗口。
  3. 输入远程路径的名称。
  4. 输入远程存储库的URL /路径
  5. 输入远程存储库的托管服务的用户名。
  6. 单击“确定”以添加远程路径。
  7. 回到Repository Settings窗口,单击'OK'。现在应该在存储库中添加新的远程路径。
  8. 如果您需要编辑已添加的远程路径,只需单击“编辑”按钮即可。您应该被定向到“远程详细信息”窗口,您可以在其中编辑远程路径的详细信息(URL /路径/主机类型)。
  9. 要删除远程存储库路径,请单击“删除”按钮

enter image description here

enter image description here

REF。 Support


4
投票

enter image description here

故障排除 :

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

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

git remote set-url sofake https://github.com/octocat/Spoon-Knife致命:没有这样的遥控'软化'

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

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


1
投票

将远程git URI更改为[email protected]而不是https://github.com

git remote set-url origin [email protected]:<username>/<repo>.git

好处是你可以在使用git push时自动执行ssh-agent

eval `ssh-agent` && expect ~/.ssh/agent && ssh-add -l
git add . && git commit -m "your commit" && git push -u origin master

当您使用expect到脚本文件时,如下所示:

#!/usr/bin/expect -f
spawn git push origin
expect "Username for 'https://github.com':"
send "<username>\n"
expect "Password for 'https://[email protected]':"
send "<password>\n"
interact

727
投票
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


87
投票

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内部结构是不好的形式。


46
投票
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


31
投票

Switching remote URLs

开放式终端。

Ist步骤: - 将当前工作目录更改为本地项目。

第2步: - 列出现有的遥控器,以获取要更改的遥控器的名称。

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。

第3步: - git remote set-url origin [email protected]:USERNAME/REPOSITORY.git

第4步: - 现在验证远程URL是否已更改。

git remote -v验证新的远程URL

origin  [email protected]:USERNAME/REPOSITORY.git (fetch)
origin  [email protected]:USERNAME/REPOSITORY.git (push)

19
投票
  1. 使用gitbash git remote rm origin上的命令删除origin
  2. 现在使用gitbash git remote add origin(在位桶中复制来自项目存储库的HTTP URL)添加新的Origin

18
投票

git remote set-url {name} {url}

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


10
投票

要检查git远程连接:

git remote -v

现在,将本地存储库设置为远程git:

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

现在使其成为上游或推送使用以下代码:

git push --set-upstream origin master -f


8
投票

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

克隆它的远程URL。

你可以用git remote -v查看它

如果你想改变它,

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

这里,

起源 - 你的分支

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

git remote remove url
and 
git remote add origin url

为了你...

© www.soinside.com 2019 - 2024. All rights reserved.