git操作

发布于:2025-03-21 ⋅ 阅读:(15) ⋅ 点赞:(0)

一、同时连接多个git仓库

1、创建多个ssh公私钥

# 生成第一个 SSH 密钥对(用于第一个 GitLab 仓库)
ssh-keygen -t rsa -b 4096 -C "your-email@example.com" -f ~/.ssh/id_rsa_gitlab_1

# 生成第二个 SSH 密钥对(用于第二个 GitLab 仓库)
ssh-keygen -t rsa -b 4096 -C "your-email@example.com" -f ~/.ssh/id_rsa_gitlab_2

~/.ssh/ 目录下创建两对文件:

  • id_rsa_gitlab_1id_rsa_gitlab_1.pub
  • id_rsa_gitlab_2id_rsa_gitlab_2.pub

2、将 SSH 公钥添加到 GitLab

对应.pub文件的内容添加到gitlab个人账户 ssh key

3、配置 SSH 客户端

# 第一个 GitLab 仓库配置
Host gitlab-repo1 #第一个仓库的别名
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa_gitlab_1

# 第二个 GitLab 仓库配置
Host gitlab-repo2 ##第二个仓库的别名
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa_gitlab_2

4、配置 Git 远程仓库

# 对于第一个仓库(使用第一个 SSH 密钥)
git remote set-url origin git@gitlab-repo1:username/repo1.git

# 对于第二个仓库(使用第二个 SSH 密钥)
git remote set-url origin git@gitlab-repo2:username/repo2.git

gitlab-repo1:步骤3、配置 SSH 客户端中对应的仓库别名

username:gitlab中的账号

repo1.git:gitlab工程路径

5、测试是否成功

# 测试第一个仓库的连接
ssh -T git@gitlab-repo1

# 测试第二个仓库的连接
ssh -T git@gitlab-repo2

如果设置正确,您会看到类似以下的消息:

Welcome to GitLab, @username!

二、本地git工程推送到新的gitlab仓库

1、初始化本地 Git 仓库

git init

2、将代码添加到 Git 版本控制中

git add .
git commit -m "首次提交"

3、创建远程仓库

在 GitHub、GitLab 等平台上创建新的project。

4、本地仓库与远程仓库关联

将本地仓库与远程仓库关联,类似第一种方式:

git remote add origin <远程仓库地址>

如果本地工程已经是git工程,需要关联到新的git仓库,忽略前面1、2步

git remote add new-origin <新的远程仓库地址>

5、推送到远程仓库

git push -u origin master # 或 main

如果需要推送到新的远程仓库:

git push -u new-origin master  # 或者 main

推送报错如下,有可能是Git 缓冲区太小:

Pushing to https://gitlab.****.git
Enumerating objects: 5594, done.
Counting objects: 100% (5594/5594), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2777/2777), done.
POST git-receive-pack (chunked)4)
error: RPC failed; HTTP 500 curl 22 The requested URL returned error: 500
send-pack: unexpected disconnect while reading sideband packet
Writing objects: 100% (5594/5594), 65.66 MiB | 98.58 MiB/s, done.
Total 5594 (delta 2037), reused 5520 (delta 1998), pack-reused 0
fatal: the remote end hung up unexpectedly

修改缓存区大小

git config http.postBuffer 524288000