项目中添加远程仓库并merge

这几天项目中遇到一个git场景:
项目仓库保存在以下三个地方:

  1. GitHub
  2. GitLab
  3. Coding

由于项目是我在开发维护,所以这三个仓库地址一直都是在同一个远程仓库origin下,三处代码时刻保持一致。

>>> git remote -v
origin  https://github.com/myname/project.git (fetch)
origin  https://github.com/myname/project.git (push)
origin  http://10.8.15.49:9091/myname/project.git (push)
origin  https://e.coding.net/myname/01/project.git (push)

但最近项目代码需要进行一处修改,需要同事来做,同事不会使用Git,所以就直接让他在Coding上进行修改了。但如何把他做出的修改更新到GitHub和GitLab上,使三处代码继续保持一致呢?

首先,添加新的远程仓库

>>> git remote add coding https://e.coding.net/myname/01/project.git
>>> git remote -v
coding  https://e.coding.net/myname/01/project.git (fetch)
coding  https://e.coding.net/myname/01/project.git (push)
origin  https://github.com/myname/project.git (fetch)
origin  https://github.com/myname/project.git (push)
origin  http://10.8.15.49:9091/myname/project.git (push)
origin  https://e.coding.net/myname/01/project.git (push)
>>> cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = https://github.com/myname/project.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = http://10.8.15.49:9091/myname/project.git
        url = https://e.coding.net/myname/01/project.git
[branch "main"]
        remote = origin
        merge = refs/heads/main
[branch "1.4"]
        remote = origin
        merge = refs/heads/1.4
[remote "coding"]
        url = https://e.coding.net/myname/01/project.git
        fetch = +refs/heads/*:refs/remotes/coding/*

可以看到已经多出coding远程仓库了

然后,使用fetch更新一下

>>> git fetch coding
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 9 (delta 7), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), 810 bytes | 67.00 KiB/s, done.
From https://e.coding.net/myname/01/project
 * [new branch]      1.4        -> coding/1.4
 * [new branch]      main       -> coding/main

然后将代码合并,上传

>>> git checkout 1.4
>>> git merge coding/1.4
>>> git push origin 1.4

大功告成!!!


之前一直都是手动修改.git/config文件来添加删除远程仓库地址,但最近才知道git提供有专门的命令

>>> git remote set-url origin --add https://e.coding.net/myname/01/project.git

效果就是会在.git/config中的origin远程仓库下添加一个url

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = https://github.com/MacwinWin/wxapp_api.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = http://10.8.15.49:9091/gaoxiang/wxapp_api.git
        url = https://e.coding.net/xlxdsj/01/wxapp_api.git
[branch "main"]
        remote = origin
        merge = refs/heads/main
[branch "1.4"]
        remote = origin
        merge = refs/heads/1.4

参考:
https://stackoverflow.com/a/12795747/7151777
https://www.jianshu.com/p/4cd46619b3a5
https://stackoverflow.com/a/16608774/7151777

<think>好的,我需要帮助用户了解如何在Git添加远程仓库。首先,用户的问题是关于源码控制系统中添加远程仓库的教程,特别是在Git中。根据用户提供的引用内容,特别是引用[2]和引用[3],里面有关于创建SSH公钥、连接远程仓库以及同步的步骤。我需要整理这些信息,分步骤说明,确保符合系统级指令的要求,比如行内数学公式用$...$,独立公式用$$...$$,不过这里可能用不上数学公式,但要注意LaTeX的正确性。另外,需要用中文回答,结构清晰,在末尾生成相关问题。 首先,我需要确定添加远程仓库的基本步骤。根据引用[2],步骤包括创建远程仓库(比如在Gitee)、生成SSH公钥、添加公钥到远程仓库、本地仓库关联远程仓库、推送代码等。同时,引用[3]提到git remote add和git remote rm来管理远程仓库的关联,以及git fetch和git pull的同步方法。需要将这些步骤整合成一个连贯的教程。 接下来,要确保结构清晰,分步骤讲解。比如先介绍什么是远程仓库,然后创建仓库,配置SSH,添加远程仓库到本地,最后推送代码。同时,要引用正确的引用标记,例如在讲解SSH公钥时引用[2],在同步时引用[3]。 可能需要注意的地方是,用户提到的“如何在Git或其他SCM工具中设置”,但用户提供的引用主要是Git相关的,特别是Gitee的例子,所以可能需要集中讲解Git提到其他工具可能类似但步骤不同。不过用户的问题更偏向Git,所以可以专注于此。 另外,需要检查是否需要代码块,比如具体的Git命令,如git remote add origin [url],这些应该用代码块包裹,确保语法正确。同时,生成的相关问题要围绕添加远程仓库的扩展,比如如何管理多个远程仓库,如何验证连接,如何处理冲突等。 最后,确保回答中没有使用Markdown格式,但用户的问题里允许代码块,所以按照示例中的方式,用```包裹代码。同时,引用标识要自然地放在引用内容的末尾,比如在提到SSH公钥的时候添加[^2],在同步步骤添加[^3]。 现在开始组织回答: 第一部分,介绍远程仓库的概念,引用[2]中的定义。 第二部分,详细步骤: 1. 创建远程仓库(以Gitee为例)。 2. 生成SSH公钥,添加远程仓库(引用[2]的步骤)。 3. 本地仓库初始化,配置用户信息。 4. 添加远程仓库地址到本地(git remote add)。 5. 推送代码(git push)。 6. 其他操作如解除关联(git remote rm)和同步(git fetch/pull,引用[3])。 代码示例部分需要展示关键命令,如git remote add,git push等。 最后生成相关问题,考虑用户可能想知道的后续操作,比如多个远程仓库、验证连接、处理冲突等。 需要确保所有引用正确,步骤清晰,符合用户的要求。</think>### 如何在Git添加远程仓库 #### 1. 远程仓库简介 远程仓库是托管在互联网服务器上的代码存储库,用于团队协作和代码备份。通过关联本地仓库远程仓库,可以实现代码的推送(push)、拉取(pull)和同步(fetch)。 #### 2. 添加远程仓库步骤 **步骤1:创建远程仓库(以Gitee为例)** 1. 登录Gitee,点击“新建仓库填写仓库名称、描述等信息。 2. 完成创建后,记录仓库的SSH地址(如`git@gitee.com:username/repo.git`)。 **步骤2:生成SSH公钥配置** 1. 本地终端输入以下命令生成SSH密钥对: ```bash ssh-keygen -t rsa -C "your_email@example.com" ``` 2. 查看公钥内容复制: ```bash cat ~/.ssh/id_rsa.pub ``` 3. 将公钥粘贴到Gitee的“SSH公钥”设置中[^2]。 **步骤3:初始化本地仓库** 1. 进入项目目录初始化Git仓库: ```bash git init git config --global user.name "Your Name" git config --global user.email "your_email@example.com" ``` **步骤4:关联远程仓库** 1. 添加远程仓库地址: ```bash git remote add origin git@gitee.com:username/repo.git ``` - `origin`是远程仓库的默认别名,可自定义(如`gitee`)。 **步骤5:推送代码到远程仓库** 1. 提交本地代码推送: ```bash git add . git commit -m "Initial commit" git push -u origin master # 首次推送需指定分支 ``` **步骤6:同步远程仓库更新** - 拉取远程更新到本地: ```bash git pull origin master ``` 或分步操作: ```bash git fetch origin # 仅获取更新[^3] git merge origin/master ``` #### 3. 管理远程仓库 - **解除关联**: ```bash git remote rm origin ``` - **查看已关联的远程仓库**: ```bash git remote -v ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值