Git项目如何拥有多个远程仓库

远程仓库的使用

远程仓库是托管在因特网或其他网络中项目的版本库。你可以同时拥有多个远程仓库(源),有些仓库只读,有些可以读写。

查看远程仓库

查看远程仓库可以运行git remote命令。它会列出当前项目远程服务器的简写,如果克隆了自己的仓库,至少可以看到origin – 这是Git对克隆的仓库默认设置的名字。

$ git remote
origin

也可以指定选项 -v,这会显示需要读写远程仓库的简写与对应的URL。

git remote -v
origin  https://github.com/learning/hello-world.git (fetch)
origin  https://github.com/learning/hello-world.git (push) 

如果远程仓库不止一个,该命令会将他们全部列出。例如 ,与几个协作者合作的,拥有多个远程仓库的项目。看起来是这样:

origin  https://gitee.com/learning/typescript-vite-app.git (fetch)
origin  https://gitee.com/learning/typescript-vite-app.git (push)
other   https://github.com/study/ts-vite-app.git (fetch)
other   https://github.com/study/ts-vite-app.git (push) 

这表示我们可以同时拥有github和gitee上的远程仓库,对不同的远程仓库可以具有不同的权限。

添加远程仓库

使用clone命令克隆一个仓库,命令会自动将其添加为远程仓库,并以origin为简称。那如何添加另外一个远程仓库,并设置简写呢?

# git remote add <shortname> <url>

$ git remote 
origin
$ git remote add puppy https://github.com/animals/puppy-app.git
$ git remote -v
origin https://gitee.com/schacon/puppy-app.git (fetch)
origin https://gitee.com/schacon/puppy-app.git (push)
puppy https://github.com/animals/puppy-app.git (fetch)
puppy https://github.com/animals/puppy-app.git (push)

现在可以在命令行中使用puppy代替整个URL。如果想拉取github上的仓库中有但你没有的内容,可以运行git fetch puppy

$ git fetch puppy
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 43 (delta 10), reused 31 (delta 5)
Unpacking objects: 100% (43/43), done.
From https://github.com/animals/puppy-app.git
 * [new branch] master -> puppy/master
 * [new branch] develop -> puppy/develop

切换远程仓库地址

$ git remote set-url origin URL

从远程仓库中获取数据

# 从远程仓库获取数据
$ git fetch <remote>

这个命令会访问远程仓库,并拉取没有的数据。执行完成后,将拥有那个远程仓库中所有分支的引用,可以随时查看或合并。
注意git fetch命令只会将数据下载到本地仓库,并不会自动合并或修改,所以需要你手动进行合并。

如果分支设置了跟踪远程分支,可以使用git pull命令来自动抓取并合并该远程分支到当前分支。默认情况下,git clone命令会自动设置本地master分支跟踪克隆的远程仓库的master分支(或其他名字的默认分支)。运行git pull 通常会从最初克隆的服务器上抓取数据,并自动尝试合并到当前所在的分支。

推送到远程仓库

# git push <remote> <branch>
$ git push origin master

当你具有克隆项目的写入权限,且之前没有人推送过,这个命令才能生效。在协作时,有人推送到上游,你需要先获取最新内容并合并到你的工作后才能推送。

查看某个远程仓库

如果想要查看某个远程仓库的更多信息,可以使用git remote show <remote>命令。它会列出远程仓库的URL与跟踪分支的信息,哪些分支不在你的本地,哪些分支已经从服务器上移除了等等。

# 查看远程仓库信息
$ git remote show origin

# 剪除远程仓库已经删除但还在本地遗留的远程分支
$ git remote prune origin

远程仓库更换地址

# 查看远程仓库信息
$ git remote -v
# 更换远程仓库地址
$ git remote set-url origin https://github.com/animals/puppy-app.git

远程仓库的重命名与删除

可以使用git remote rename命令来重命名某个仓库的简称

$ git remote rename puppy py
$ git remote
origin
py

如果想要移除一个远程仓库,可以使用git remote remove或者git remote rm

$ git remote remove puppy
$ git remote
origin

每天进步一点点-.-!


  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,可以将一个本地 Git 仓库关联到多个远程仓库。你可以按照以下步骤进行操作: 1. 首先,在本地创建一个空的 Git 仓库,或者进入已有的 Git 仓库目录。 2. 打开终端或命令行界面,使用 `git remote add` 命令来分别关联多个远程仓库。例如,如果你要关联的第一个远程仓库的名称为 "origin",远程仓库的 URL 为 "https://github.com/your-username/your-repo.git",则可以运行以下命令: ``` git remote add origin https://github.com/your-username/your-repo.git ``` 如果你要关联的第二个远程仓库的名称为 "second",远程仓库的 URL 为 "https://github.com/your-username/second-repo.git",则可以运行以下命令: ``` git remote add second https://github.com/your-username/second-repo.git ``` 这里的 "origin" 和 "second" 是远程仓库的名称,你可以根据需要自定义。 3. 确认关联成功后,你可以使用 `git remote -v` 命令来查看已关联的远程仓库列表。例如,运行以下命令: ``` git remote -v ``` 你将看到类似以下输出: ``` origin https://github.com/your-username/your-repo.git (fetch) origin https://github.com/your-username/your-repo.git (push) second https://github.com/your-username/second-repo.git (fetch) second https://github.com/your-username/second-repo.git (push) ``` 这表示你已成功关联了两个远程仓库。 4. 当你需要将代码推送到特定的远程仓库时,可以使用 `git push` 命令加上远程仓库的名称。例如,要将代码推送到 "origin" 远程仓库的 master 分支,可以运行以下命令: ``` git push origin master ``` 要将代码推送到 "second" 远程仓库的 develop 分支,可以运行以下命令: ``` git push second develop ``` 请注意,上述命令中的 "your-username" 应替换为你的 GitHub 用户名。你可以根据需要设置不同的远程仓库名称和分支名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值