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

时间:2010-03-12 12:48:47

标签: git url git-remote

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

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

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

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

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

30 个答案:

答案 0 :(得分:5198)

你可以

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

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

答案 1 :(得分:701)

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

答案 2 :(得分:85)

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

答案 3 :(得分:42)

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

另请参阅:git merge doc

答案 4 :(得分:26)

切换远程URL

打开终端。

Ist 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。

第3步: - git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

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

git remote -v 验证新的远程URL

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

答案 5 :(得分:19)

here所示,

$ git remote rm origin
$ git remote add origin git@github.com:aplikacjainfo/proj1.git
$ git config master.remote origin
$ git config master.merge refs/heads/master

答案 6 :(得分:18)

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

答案 7 :(得分:16)

git remote set-url {name} {url}

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

答案 8 :(得分:9)

导航到本地存储库的项目根目录,并检查现有的遥控器:

git remote -v

如果您的存储库使用的是 SSH ,您会看到类似以下内容的

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

如果您的存储库使用的是 HTTPS ,您会看到类似以下内容的

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

使用git remote set-url更改URL。根据{{​​1}}的输出,您可以通过以下方式更改URL:

对于 SSH ,您可以将网址从git remote -v更改为REPOSITORY.git,例如:

NEW_REPOSITORY.git

对于 HTTPS ,您可以将网址从$ git remote set-url origin git@github.com:USERNAME/NEW_REPOSITORY.git 更改为REPOSITORY.git,例如:

NEW_REPOSITORY.git

注意:如果您更改了 GitHub 用户名,则可以按照与上述相同的过程来更新与存储库关联的用户名中的更改。您只需要在$ git remote set-url origin https://github.com/USERNAME/NEW_REPOSITORY.git 命令中更新USERNAME

答案 9 :(得分:8)

要检查git远程连接:

git remote -v

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

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

现在将其设为上游或使用以下代码进行推送:

git push --set-upstream origin master -f

答案 10 :(得分:7)

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

克隆它的远程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

给你......

答案 11 :(得分:6)

我工作过:

git remote set-url origin <project>

答案 12 :(得分:5)

在Git Bash中输入命令:

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

输入凭据

完成

答案 13 :(得分:5)

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

  1. 转到您当地的结帐文件夹,然后右键点击转到TortoiseGit -> Settings
  2. 在左侧窗格中选择Git -> Remote
  3. 在右侧窗格中选择origin
  4. 现在将URL文本框值更改为新远程存储库所在的位置
  5. 您的分支机构和所有本地提交将保持不变,您可以像以前一样继续工作。

答案 14 :(得分:5)

对我来说,接受的答案仅适用于获取而不适用于拉取的情况。我做了以下工作以使其也适用于推送。

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

所以要更新获取 URL:

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

更新拉取 URL:

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

答案 15 :(得分:4)

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

控制台

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. 返回“存储库设置”窗口,单击“确定”。现在应该在存储库中添加新的远程路径。

  8. 如果需要编辑已经添加的远程路径,只需单击“编辑”按钮。应该将您定向到“远程详细信息”窗口,您可以在其中编辑远程路径的详细信息(URL /路径/主机类型)。

  9. 要删除远程存储库路径,请单击“删除”按钮

enter image description here

enter image description here

参考Support

答案 16 :(得分:4)

更改远程上游: git remote set-url origin <url>


添加更多上游: git remote add newplace <url>

因此您可以选择在哪里工作 git push origin <branch>git push newplace <branch>

答案 17 :(得分:3)

enter image description here

故障排除:

尝试更换遥控器时,您可能会遇到这些错误。 没有这样的远程“ [名称]”

此错误意味着您尝试更改的遥控器不存在:

git远程设置URL沙发https://github.com/octocat/Spoon-Knife 致命的:没有这种遥远的“傻瓜”

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

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

答案 18 :(得分:3)

您可以通过编辑配置文件来更改URL。 转到您的项目根目录:

nano .git/config

然后编辑url字段并设置新的url。 保存更改。您可以使用命令来验证更改。

git remote -v 

答案 19 :(得分:1)

如果您想在源网址中也设置用户名和密码,您可以按照以下步骤操作。

在变量中导出密码可以避免特殊字符问题。

步骤:

export gituser='<Username>:<password>@'
git remote set-url origin https://${gituser}<gitlab_repo_url> 
git push origin <Branch Name>

答案 20 :(得分:1)

另一种方法是重命名“旧”起源(在下面的示例中,我将其简称为old-origin)并添加一个新的起源。如果您仍然希望时不时地将其推到原来的位置,这可能是所需的方法

git remote rename origin old-origin
git remote add origin git@new-git-server.com>:<username>/<projectname>.git

如果需要将您的本地州推到新的起点,

git push -u origin --all
git push -u origin --tags

答案 21 :(得分:1)

如果您是组织所有者或拥有仓库管理员权限,则可以重命名仓库。

 git remote set-url origin new_url

know more

答案 22 :(得分:1)

卸下遥控器

使用git remote rm命令从存储库中删除远程URL。

    $ git remote -v
    # View current remotes
    > origin  https://github.com/OWNER/REPOSITORY.git (fetch)
    > origin  https://github.com/OWNER/REPOSITORY.git (push)
    > destination  https://github.com/FORKER/REPOSITORY.git (fetch)
    > destination  https://github.com/FORKER/REPOSITORY.git (push)

    $ git remote rm destination
    # Remove remote
    $ git remote -v
    # Verify it's gone
    > origin  https://github.com/OWNER/REPOSITORY.git (fetch)
    > origin  https://github.com/OWNER/REPOSITORY.git (push)

答案 23 :(得分:1)

检查您的特权

对于我来说,我需要检查我的用户名

我有两个或三个具有单独凭据的存储库。

问题是我的允许,我有两个私人git服务器和存储库

此第二个帐户是该新存储库的管理员,第一个是我的默认用户帐户,我应该向第一个帐户授予权限

答案 24 :(得分:1)

在回购终端上编写以下命令:

git remote set-url origin git@github.com:<username>/<repo>.git

请参阅this链接以获取有关更改遥控器中URL的更多详细信息。

答案 25 :(得分:0)

如果您的存储库是私有的,则

  1. 从“开始”菜单打开控制面板
  2. 选择用户帐户
  3. 在左侧菜单中选择“管理您的凭据”
  4. 删除与Git或GitHub相关的所有凭据

Reference

答案 26 :(得分:0)

对于那些想要从Visual Studio 2019进行此更改的人

打开团队资源管理器(Ctrl + M)

主页->设置

Git->存储库设置

远程->编辑

enter image description here

答案 27 :(得分:0)

(仅限 Windows PS)在所有本地存储库中递归更改服务器/协议

Get-ChildItem -Directory -Recurse -Depth [Number] -Hidden -name | %{$_.replace("\.git","")} | %{git -C $_ remote set-url origin $(git -C $_ remote get-url origin).replace("[OLD SERVER]", "[NEW SERVER]")}

答案 28 :(得分:0)

如果你在 linux/Mac 上使用 Sed/grep,它也很容易改变而不会丢失关联。

最近这样做是为了在配置中更改我所有本地存储库的用户名,但对远程线路也完全采用了相同的方法。

VARIABLE_FIND='old'; VARIABLE_REPLACE='new'; path_name='~/PycharmProjects/*/.git/'; grep -rl --include=config "${VARIABLE_FIND}" "${path_name}" | xargs sed -i "s|${VARIABLE_FIND}|${VARIABLE_REPLACE}|g"

要替换匹配的整行,您可以这样做。

VARIABLE_FIND='someneedle'; VARIABLE_REPLACE='somenewvalue'; path_name='/home/*/' grep -rl --include=config "${VARIABLE_FIND}" "${path_name}" | xargs sed -i "/${VARIABLE_FIND//\//\\/}/c\\${VARIABLE_REPLACE}" ;

参考: Replace whole line containing a string using Sed

答案 29 :(得分:0)

  

将远程git URI更改为git@github.com,而不是https://github.com

git remote set-url origin git@github.com:<username>/<repo>.git

好处是,当您使用 ssh-agent 时,您可以自动执行git push

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://user@github.com':"
send "<password>\n"
interact