修改项目中Git远程仓库地址

1.CLONE远程仓库

在更换远程仓库之前,需要先下载原有的仓库,以方便将原来远程仓库的内容更换到新的远程仓库上,可以用git clone 命令将远程仓库克隆一份到本地。

 git clone <旧仓库地址> <本地目录>
git clone git@gitlab.xxxx.com:ip_data/ip_model.git

经过上述命令以后,我们用ls命令,可以看到当前文件夹下多了一个ip_model文件夹,我们可以通过mv ip_model repo_test命令对ip_model文件夹进行重命名,或者直接用以下git clone 命令

git clone git@gitlab.xxxx.com:ip_data/ip_model.git repo_test

cd repo_test进入repo_test 文件夹.

输入命令【git remote -v】查看git远程仓库地址,

git remote -v

会显示当前的远程仓库名和远程仓库地址

origin git@gitlab.xxxx.com:ip_data/ip_model.git(fetch)
origin git@gitlab.xxxx.com:ip_data/ip_model.git(push)

2.通过git命令修改指向的远程仓库git remote set-url

输入命令【git remote set-url origin 新地址】替换成新地址

git remote set-url origin 新地址
git remote set-url origin git@gitlab.xxxxx.com:lanyy/repo_test.git

重新输入命令【git remote -v】查看git远程仓库地址,

git remote -v

会显示当前的远程仓库名和远程仓库地址

origin git@gitlab.xxxx.com:ip_data/repo_test.git(fetch)
origin git@gitlab.xxxx.com:ip_data/repo_test.git(push)

上述修改的过程也可以用先删除当前的远程仓库名和远程仓库地址,再添加新的远程仓库名和远程仓库地址实现,如下步骤3.

3.删除旧的远程仓库地址添加新远程仓库

在备份旧仓库后,需要将新仓库添加到本地仓库中。首先使用git remote rm命令删除旧仓库名和旧仓库远程地址。

git remote rm origin

此时我们用git remote -v,查看仓库名和仓库远程地址,结果是空,说明旧仓库名和旧仓库远程地址已经被删除

然后用git remote add再添加新的远程仓库名和远程仓库地址

git remote add <新仓库名称> <新仓库地址>
git remote add origin git@gitlab.xxxxx.com:lanyy/repo_test.git

4.输入命令【git remote -v 】查看是否更新成功

git remote -v

结果显示

origin git@gitlab.xxxxx.com:lanyy/repo_test.git(fetch)
origin git@gitlab.xxxxx.com:lanyy/repo_test.git(push)

仓库名和地址已被更新成功

5.推送到新仓库

更换完远程仓库后,需要将本地的代码同步到新仓库中,使用git push命令将代码push到新仓库中。

git push <新仓库名称> <分支名称>

其中,新仓库名称为第2步中添加的新仓库别名,分支名称为需要push的本地分支名或者远程分支名。

6.删除旧仓库

推送完代码到新仓库后,可以删除旧仓库,使用git remote命令进行删除。

git remote remove <旧仓库名称>

其中,旧仓库名称为第二步中添加的旧仓库别名。

7.小结

通过以上几个步骤,我们可以成功更换远程仓库。需要注意的是,在更换远程仓库之前,一定要备份好原有的仓库。另外,更换远程仓库时要谨慎,避免因为操作不当造成代码丢失。

8.git remote 命令

git remote 命令用于用于管理 Git 仓库中的远程仓库。

git remote 命令提供了一些用于查看、添加、重命名和删除远程仓库的功能。

以下是 git remote 命令的常见用法:

git remote:列出当前仓库中已配置的远程仓库。
git remote -v:列出当前仓库中已配置的远程仓库,并显示它们的 URL。
git remote add <remote_name> <remote_url>:添加一个新的远程仓库。指定一个远程仓库的名称和 URL,将其添加到当前仓库中。
git remote rename <old_name> <new_name>:将已配置的远程仓库重命名。
git remote remove <remote_name>:从当前仓库中删除指定的远程仓库。
git remote set-url <remote_name> <new_url>:修改指定远程仓库的 URL。
git remote show <remote_name>:显示指定远程仓库的详细信息,包括 URL 和跟踪分支。

以下列出了远程仓库、添加远程仓库、重命名远程仓库、删除远程仓库、修改远程仓库 URL 和查看远程仓库信息的用法:

git remote
git remote -v
git remote add origin https://github.com/user/repo.git
git remote rename origin new-origin
git remote remove new-origin
git remote set-url origin https://github.com/user/new-repo.git
git remote show origin

应用实例

本章节内容我们将以 Github 作为远程仓库来操作,所以阅读本章节前需要先阅读关于 Github 的相关内容:Git 远程仓库(Github)。

显示所有远程仓库:

git remote -v

git remote -v 可以查看当前仓库中配置的远程仓库列表以及它们的 URL。

以下我们先载入远程仓库,然后查看信息:

$ git clone https://github.com/lanyy/runoob-git-test
$ cd runoob-git-test
$ git remote -v
origin  https://github.com/lanyy/runoob-git-test (fetch)
origin  https://github.com/lanyy/runoob-git-test (push)

origin 为远程地址的别名。

显示某个远程仓库的信息:

git remote show [remote]

例如:

$ git remote show https://github.com/lanyy/runoob-git-test
* remote https://github.com/lanyy/runoob-git-test
  Fetch URL: https://github.com/lanyy/runoob-git-test
  Push  URL: https://github.com/lanyy/runoob-git-test
  HEAD branch: master
  Local ref configured for 'git push':
    master pushes to master (local out of date)

添加远程版本库:

git remote add <remote_name> <remote_url>
  • <remote_name>:要添加的远程仓库的名称。通常,远程仓库的名称为 origin,但你也可以自定义一个名称。
  • <remote_url>:远程仓库的 URL。它可以是一个指向远程 Git 仓库的 HTTPS、SSH 或 Git 协议链接。

以下命令将向当前 Git 仓库添加一个名为 origin 的远程仓库,它的 URL 是 https://github.com/user/repo.git。

git remote add origin https://github.com/user/repo.git

实例用法:

# 提交到 Github
$ git remote add origin git@github.com:lanyy/runoob-git-test.git
$ git push -u origin master

添加远程仓库后,你就可以使用其他 Git 命令与远程仓库进行交互,例如推送本地代码到远程仓库、拉取远程仓库的代码等。

其他相关命令:

git remote rm name  # 删除远程仓库
git remote rename old_name new_name  # 修改仓库名

posted @ 2023-08-08 18:21  luckylan  阅读(4073)  评论(0)    收藏  举报