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

e-s*_*tis 3529 git url git-remote

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

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

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

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

  • 添加一个新的遥控器到"本地"并删除旧遥控器(我担心我会打破我的历史).

hob*_*bbs 5755

您可以

git remote set-url origin new.git.url/here
Run Code Online (Sandbox Code Playgroud)

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

  • @kelorek或者你可以第一次`git push -u origin master` :) (29认同)
  • 如果你有一个不同的shell用户,那么你可能想在新url的开头指定你的git用户,例如:`myself @ git:// new.url.here` (24认同)
  • 我还必须`git remote set-url --push origin git:// ...`以便设置origin ...(push)url. (22认同)
  • 您可能还希望使用以下命令为新的原始位置设置主上游分支:`git branch -u origin/master`.这将允许你只是'git push`而不是每次都要'git push origin master`. (10认同)
  • 对于多个分支,可以使用git push -u --all将所有分支一次推送到新的url(而不是git push -u origin master)。 (3认同)

小智 790

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)
Run Code Online (Sandbox Code Playgroud)

更改遥控器的URL

  • 令人惊讶的是,你仅仅因为给出了与已接受的答案相同的答案就获得了近千张赞成票,因此获得了近一万分,但三年后。 (34认同)
  • @MS Berends “git remote -v”有助于验证,而接受的解决方案没有提供这一点。 (25认同)
  • 为了获得所有这些,我添加了:“git remote set-url --push origin git@github.com/User/Branch.git”和“git remote set-urlcomposer https://github.com/User/Branch”。 git` (4认同)

Mit*_*ana 118

这非常容易和简单;只需按照这些说明操作即可。

  1. 要添加或更改远程源:
    git remote set-url origin githubrepurl
    
    Run Code Online (Sandbox Code Playgroud)
  2. 要查看本地存储库当前拥有哪个远程 URL:
    git remote show origin
    
    Run Code Online (Sandbox Code Playgroud)


yod*_*oda 92

更改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
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = ssh://oldhost.com/usr/local/gitroot/myproject.git
Run Code Online (Sandbox Code Playgroud)

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

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


小智 58

效果会很好,你可以试试这个

对于 SSH:

命令: git remote set-url origin <ssh_url>

例子: git remote set-url origin git@github.com:username/rep_name.git

对于 HTTPS:

命令: git remote set-url origin <https_url>

例子: git remote set-url origin https://github.com/username/REPOSITORY.git


Zaz*_*Zaz 50

git remote set-url origin git://new.location
Run Code Online (Sandbox Code Playgroud)

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

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

git remote -v
# origin  git://new.location (fetch)
# origin  git://new.location (push)
Run Code Online (Sandbox Code Playgroud)

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

git push -u origin master
Run Code Online (Sandbox Code Playgroud)

另请参阅:GitHub:更改远程URL

  • +1 使用“git push -u”命令提供完整的答案。也许对其他人来说是显而易见的,但对我来说却不是。 (3认同)

小智 40

First you need to type this command to view existing remotes

git remote -v

Then second you need to type this command to Change the 'origin' remote's URL

git remote set-url origin <paste your GitHub URL>


Zah*_*kot 36

正如这里所见,

$ 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
Run Code Online (Sandbox Code Playgroud)

  • 当为一个十年前的问题添加二十一个其他答案的答案时,包含对答案的解释并指出你的答案所涉及的问题的新方面非常重要。通过由一系列命令组成的答案,解释每个命令正在做什么以及如何撤消每个命令的影响(如果可能)是很有用的。如果有人能够执行前几个步骤,但在后面的步骤中遇到错误,则撤消非常重要。 (18认同)
  • @JasonAller我认为这是相当不言自明的,这是迄今为止最好的答案,其他的都是个笑话。 (3认同)

VIK*_*HLI 32

切换远程URL

开放式终端.

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

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

git remote -v

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

origin  https://github.com/USERNAME/REPOSITORY.git (push)
Run Code Online (Sandbox Code Playgroud)

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

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

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

git remote -v 验证新的远程URL

origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
origin  git@github.com:USERNAME/REPOSITORY.git (push)
Run Code Online (Sandbox Code Playgroud)


viv*_*kar 26

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

git remote set-url origin git@github.com:<username>/<repo>.git
Run Code Online (Sandbox Code Playgroud)

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


Sun*_*ary 21

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


bon*_*hoe 18

git remote set-url {name} {url}

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


Ami*_*esh 16

在此处输入图片说明

故障排除 :

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

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

git remote set-url SOFAKE https://github.com/octocat/Spoon-Knife 致命:没有这样的远程“沙发”

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

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


Sau*_*abh 15

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

git remote -v
Run Code Online (Sandbox Code Playgroud)

如果您的存储库使用SSH,您将看到如下内容:

> origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
> origin  git@github.com:USERNAME/REPOSITORY.git (push)
Run Code Online (Sandbox Code Playgroud)

如果您的存储库使用HTTPS,您将看到如下内容:

> origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
> origin  https://github.com/USERNAME/REPOSITORY.git (push)
Run Code Online (Sandbox Code Playgroud)

更改 URL 是通过git remote set-url. 根据 的输出git remote -v,您可以通过以下方式更改 URL:

在的情况下,SSH,你可以更改URL REPOSITORY.git,以NEW_REPOSITORY.git这样的:

$ git remote set-url origin git@github.com:USERNAME/NEW_REPOSITORY.git
Run Code Online (Sandbox Code Playgroud)

而在的情况下,HTTPS,您可以更改URL REPOSITORY.git,以NEW_REPOSITORY.git这样的:

$ git remote set-url origin https://github.com/USERNAME/NEW_REPOSITORY.git
Run Code Online (Sandbox Code Playgroud)

注意:如果您更改了GitHub用户名,您可以按照与上述相同的过程更新与您的存储库关联的用户名的更改。你只需要更新USERNAMEgit remote set-url命令。


Raj*_*ram 14

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

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

脚步:

export gituser='<Username>:<password>@'
git remote set-url origin https://${gituser}<gitlab_repo_url> 
git push origin <Branch Name>
Run Code Online (Sandbox Code Playgroud)


小智 14

转到要更改的文件夹/存储库并执行以下命令:

以下命令将更改存储库的 git fetch url。

git remote set-url origin <your new url>.git
Run Code Online (Sandbox Code Playgroud)

以下命令将更改存储库的 git Push url。

git remote set-url --push origin <your new url>.git
Run Code Online (Sandbox Code Playgroud)

以下命令检查上述更改是否反映

git remote -v
Run Code Online (Sandbox Code Playgroud)


小智 13

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


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

所以你可以选择在哪里工作 git push origin <branch>git push newplace <branch>


小智 12

移除遥控器

使用 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)
Run Code Online (Sandbox Code Playgroud)


Anu*_*rya 11

要检查git远程连接:

git remote -v
Run Code Online (Sandbox Code Playgroud)

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

git remote set-url origin https://NewRepoLink.git
Run Code Online (Sandbox Code Playgroud)

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

git push --set-upstream origin master -f


Moh*_*med 9

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

克隆它的远程URL.

你可以用它来检查 git remote -v

如果你想改变它,

git remote set-url origin https://github.io/my_repo.git
Run Code Online (Sandbox Code Playgroud)

这里,

起源 - 你的分支

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

git remote remove url
and 
git remote add origin url
Run Code Online (Sandbox Code Playgroud)

为了你...


dev*_*jay 7

在Git Bash中,输入命令:

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

输入凭据

完成


Die*_*ezú 7

我工作:

git remote set-url origin <project>
Run Code Online (Sandbox Code Playgroud)


Abh*_*Das 7

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

nano .git/config
Run Code Online (Sandbox Code Playgroud)

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

git remote -v 
Run Code Online (Sandbox Code Playgroud)


joj*_*ojo 7

另一种方法是重命名“旧”原点(在下面的示例中,我将其简单命名old-origin)并添加一个新原点。如果您仍然希望能够时不时地推送到旧源,这可能是理想的方法

git remote rename origin old-origin
git remote add origin git@new-git-server.com>:<username>/<projectname>.git
Run Code Online (Sandbox Code Playgroud)

如果您需要将本地状态推送到新的来源:

git push -u origin --all
git push -u origin --tags
Run Code Online (Sandbox Code Playgroud)


Shy*_*dda 7

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

git remote set-url --push origin new.git.url/here
Run Code Online (Sandbox Code Playgroud)

所以要更新获取 URL:

git remote set-url origin new.git.url/here
Run Code Online (Sandbox Code Playgroud)

要更新拉取 URL:

git remote set-url --push origin new.git.url/here
Run Code Online (Sandbox Code Playgroud)


Vip*_*ani 6

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

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

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


Prz*_*ski 6

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

安慰

git remote set-url origin [Here new url] 
Run Code Online (Sandbox Code Playgroud)

只需确保已在存储库所在的位置将其打开即可。

设定档

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

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = [Here new url]  <------------------------------------
...
Run Code Online (Sandbox Code Playgroud)

龟龟

第1步-打开设置

第2步-更改网址

然后,只需编辑URL。

源树

  1. 单击工具栏上的“设置”按钮以打开“存储库设置”窗口。

  2. 单击“添加”以将远程存储库路径添加到存储库。将打开“远程详细信息”窗口。

  3. 输入远程路径的名称。

  4. 输入远程存储库的URL /路径

  5. 输入远程存储库的托管服务的用户名。

  6. 单击“确定”添加远程路径。

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

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

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

在此处输入图片说明

在此处输入图片说明

参考 支持


sab*_*zdi 6

检查你的特权

就我而言,我需要检查我的用户名

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

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

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


Che*_*ana 5

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

git remote set-url origin git@github.com:<username>/<repo>.git
Run Code Online (Sandbox Code Playgroud)

例:

git remote set-url origin git@github.com:Chetabahana/my_repo_name.git
Run Code Online (Sandbox Code Playgroud)

好处是您可以在使用ssh-agentgit push自动执行:

#!/bin/bash

# Check ssh connection
ssh-add -l &>/dev/null
[[ "$?" == 2 ]] && eval `ssh-agent`
ssh-add -l &>/dev/null
[[ "$?" == 1 ]] && expect $HOME/.ssh/agent

# Send git commands to push
git add . && git commit -m "your commit" && git push -u origin master
Run Code Online (Sandbox Code Playgroud)

放置一个脚本文件$HOME/.ssh/agent以使其ssh-add使用期望运行,如下所示:

#!/usr/bin/expect -f
set HOME $env(HOME)
spawn ssh-add $HOME/.ssh/id_rsa
expect "Enter passphrase for $HOME/.ssh/id_rsa:"
send "<my_passphrase>\n";
expect "Identity added: $HOME/.ssh/id_rsa ($HOME/.ssh/id_rsa)"
interact
Run Code Online (Sandbox Code Playgroud)


Tho*_*ann 5

(仅限 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]")}
Run Code Online (Sandbox Code Playgroud)


m_h*_*ish 5

这个答案与我在其他地方找不到答案的问题有关:为 Gitlab 中的一个项目执行此操作,该项目有一个子组。这确实通过 SSH 起作用:

git remote add origin git@gitlab.com:group/subgroup/project.git
Run Code Online (Sandbox Code Playgroud)

与所有使用“用户名”而不是“组”的答案相比,这些答案都没有通过 SSH 对我有用。或者,通过 https 实现的方法:

git remote add origin https://username@gitlab.com/group/subgroup/project.git
Run Code Online (Sandbox Code Playgroud)

请注意“.com:group”和“.com/group”两者的区别

如果这不能立即工作,我在成功使用 SSH 方法之前使用了 HTTPS 方法,这可能是一个因素,但我不能在没有它的情况下尝试复制它。