我在我的硬盘上(本地)克隆的USB键上有一个repo(起源)。我将“起源”转移到NAS,并成功测试了从这里克隆。

我想知道我是否可以在“本地”设置中更改“起源”的URI,所以它现在将从NAS中拖动,而不是从USB键。

到目前为止,我可以看到两个解决方案:

把一切推到USB起源,然后将其复制到NAS(这意味着由于NAS起源的新承诺而付出很多工作);添加一个新的远程到“本地”并删除旧的(我担心我会打破我的历史)。


当前回答

查看 git 远程连接:

git remote -v

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

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

现在让它升级或按下使用下列代码:

git push --set-upstream 起源大师 -f

其他回答

另一种方法是重新命名“旧”起源(在下面的例子中,我简单地称之为旧起源),并添加一个新的。

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

这是非常容易和简单的;只需遵循这些指示。

要添加或更改远程起源: git 远程设置 URL 起源 githubrepurl 查看您目前在本地存储库中有哪些远程 URL: git 远程显示起源

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)

更改远程的URL

如果您想在原始URL中设置用户名和密码,您可以遵循下面的步骤。

输出密码在变量将避免与特殊字符的问题。

步骤:

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

导航到本地存储库的项目根,并检查现有远程:

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 远程 -v 的输出,您可以以以下方式更改 URL:

在 SSH 的情况下,您可以从 REPOSITORY.git 更改 URL 到 NEW_REPOSITORY.git 如下:

$ git remote set-url origin git@github.com:USERNAME/NEW_REPOSITORY.git

而在 HTTPS 的情况下,您可以从 REPOSITORY.git 更改 URL 到 NEW_REPOSITORY.git 如下:

$ git remote set-url origin https://github.com/USERNAME/NEW_REPOSITORY.git

注意: 如果您已更改了 GitHub 用户名,您可以按照上述相同的过程更新与您的存储库相关的用户名的更改。