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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2432764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:02:07  来源:igfitidea点击:

How to change the URI (URL) for a remote Git repository?

giturlgit-remote

提问by e-satis

I have a repo (origin) on a USB key that I cloned on my hard drive (local). I moved "origin" to a NAS and successfully tested cloning it from here.

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

I would like to know if I can change the URI of "origin" in the settings of "local" so it will now pull from the NAS, and not from the USB key.

我想知道我是否可以在“本地”设置中更改“来源”的 URI,这样它现在将从 NAS 中拉出,而不是从 USB 密钥中拉出。

For now, I can see two solutions:

目前,我可以看到两种解决方案:

  • push everything to the usb-orign, and copy it to the NAS again (implies a lot of work due to new commits to nas-origin);

  • add a new remote to "local" and delete the old one (I fear I'll break my history).

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

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

回答by hobbs

You can

你可以

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

(see git help remote) or you can just edit .git/configand change the URLs there. You're not in any danger of losing history unless you do something very silly (and if you're worried, just make a copy of your repo, since your repo isyour history.)

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

回答by Utensil

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

更改遥控器的 URL

回答by yoda

Change Host for a Git Origin Server

更改 Git 源服务器的主机

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

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

Hopefully this isn't something you need to do. The server that I've been using to collaborate on a few git projects with had the domain name expire. This meant finding a way of migrating the local repositories to get back in sync.

希望这不是您需要做的事情。我一直用于在一些 git 项目上进行协作的服务器的域名已过期。这意味着找到一种迁移本地存储库以恢复同步的方法。

Update: Thanks to @mawolf for pointing out there is an easy way with recent git versions (post Feb, 2010):

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

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

See the man page for details.

有关详细信息,请参阅手册页。

If you're on an older version, then try this:

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

As a caveat, this works only as it is the same server, just with different names.

需要注意的是,这仅在它是同一台服务器时才有效,只是名称不同。

Assuming that the new hostname is newhost.com, and the old one was oldhost.com, the change is quite simple.

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

Edit the .git/configfile in your working directory. You should see something like:

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

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

Change oldhost.comto newhost.com, save the file and you're done.

更改oldhost.comnewhost.com,保存文件,您就完成了。

From my limited testing (git pull origin; git push origin; gitx) everything seems in order. And yes, I know it is bad form to mess with git internals.

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

回答by Zaz

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

(alternatively, open .git/config, look for [remote "origin"], and edit the url =line.

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

You can check it worked by examining the remotes:

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

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

Next time you push, you'll have to specify the new upstream branch, e.g.:

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

git push -u origin master

See also: GitHub: Changing a remote's URL

另请参阅:GitHub:更改遥控器的 URL

回答by VIKAS KOHLI

Switching remote URLs

切换远程 URL

Open Terminal.

打开终端。

Ist Step:- Change the current working directory to your local project.

第一个步骤:- 将当前工作目录更改为您的本地项目。

2nd Step:- List your existing remotes in order to get the name of the remote you want to change.

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

git remote -v

git remote -v

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

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

Change your remote's URL from HTTPS to SSH with the git remote set-url command.

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

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

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

4th Step:- Now Verify that the remote URL has changed.

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

git remote -vVerify new remote URL

git remote -v验证新的远程 URL

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

回答by Sunil Chaudhary

  1. remove origin using command on gitbash git remote rm origin
  2. And now add new Origin using gitbash git remote add origin(Copy HTTP URL from your project repository in bit bucket) done
  1. 在 gitbash git remote rm origin上使用命令删除 原点
  2. 现在使用 gitbash 添加新的 Origin git remote add origin(从您的项目存储库中复制 HTTP URL 在位桶中)完成

回答by bong jae choe

git remote set-url {name} {url}

git remote set-url {name} {url}

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

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

回答by viveknaskar

Write the below command from your repo terminal:

从您的 repo 终端编写以下命令:

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

Refer thislink for more details about changing the url in the remote.

有关更改遥控器中 url 的更多详细信息,请参阅链接。

回答by Mohideen bin Mohammed

if you cloned your local will automatically consist,

如果你克隆你的本地将自动包含,

remote URL where it gets cloned.

它被克隆的远程 URL。

you can check it using git remote -v

你可以使用 git remote -v

if you want to made change in it,

如果你想改变它,

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

here,

这里,

origin - your branch

起源 - 你的分支

if you want to overwriteexisting branch you can still use it.. it will override your existing ... it will do,

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

git remote remove url
and 
git remote add origin url

for you...

为你...

回答by Anupam Maurya

To check git remote connection:

检查 git 远程连接:

git remote -v

Now, set the local repository to remote git:

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

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

Now to make it upstream or push use following code:

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

git push --set-upstream origin master -f

git push --set-upstream origin master -f