git本地项目配置多远程仓库

本文介绍了如何在本地Git项目中设置多个远程仓库,包括初始化、添加远程仓库、配置用户名和邮箱、推送/拉取代码,以及解决合并冲突和删除远程仓库的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.进入项目根目录

cd /www/repo

2.如果没有初始化则请输入命令完成初始化

git init

3.如果已配置过git远程仓库,请移除

git remote rm origin

4.依次添加多个远程仓库地址

git remote add github git@github.com:org/repo.git 
git remote add gitee git@gitee.com:org/repo.git

5.配置当前项目的用户名和邮箱

git config user.name Tom 
git config user.email tom@github.com

6.查看.git/config配置信息

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[branch "master"]
[remote "github"]
        url = git@github.com:org/repo.git
        fetch = +refs/heads/*:refs/remotes/github/*
[remote "gitee"]
        url = git@gitee.com:org/repo.git
        fetch = +refs/heads/*:refs/remotes/gitee/*
[user]
        email = tom@github.com

7.推送/拉取代码示例(原本默认存在的origin被命名为github和gitee)

// 分别执行github和gitee两个远程仓库的拉取和推送操作,例:
git push github master
git push github test
git pull github master

git push gitee master
git pull gitee master

8.使用 git remote -v 查看远程仓库

/www/repo> git remote -v
github  git@github.com:org/repo.git (fetch)
github  git@github.com:org/repo.git (push)
gitee   git@gitee.com:org/repo.git (fetch)
gitee   git@gitee.com:org/repo.git (push)

9.如果遇到如下问题,可参考:

Q1: ! [rejected] master -> master (non-fast-forward)……

A1:git pull gitee master --allow-unrelated-histories

Q2:fatal:refusing to merge unrelated histories ……(拒绝合并无关的历史) 

A2:

(1)将本地仓库和远程长裤分支做好映射

    git branch --set-upstream-to=origin/remote_branch your_local_branch

(2)整合远程仓库和本地仓库(忽略版本不同造成的影响)

    git pull --allow-unrelated-histories

10.删除远程仓库就变成了git remote rm gitee或git remote rm github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值