git操纵

[复制链接]
发表于 2025-10-10 02:46:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
一、同时毗连多个git堆栈

1、创建多个ssh公私钥
  1. # 生成第一个 SSH 密钥对(用于第一个 GitLab 仓库)
  2. ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_gitlab_1
  3. # 生成第二个 SSH 密钥对(用于第二个 GitLab 仓库)
  4. ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_gitlab_2
复制代码
~/.ssh/ 目次下创建两对文件:

  • id_rsa_gitlab_1 和 id_rsa_gitlab_1.pub
  • id_rsa_gitlab_2 和 id_rsa_gitlab_2.pub
2、将 SSH 公钥添加到 GitLab

对应.pub文件的内容添加到gitlab个人账户 ssh key
3、设置 SSH 客户端
  1. # 第一个 GitLab 仓库配置
  2. Host gitlab-repo1 #第一个仓库的别名
  3.   HostName gitlab.com
  4.   User git
  5.   IdentityFile ~/.ssh/id_rsa_gitlab_1
  6. # 第二个 GitLab 仓库配置
  7. Host gitlab-repo2 ##第二个仓库的别名
  8.   HostName gitlab.com
  9.   User git
  10.   IdentityFile ~/.ssh/id_rsa_gitlab_2
复制代码
4、设置 Git 长途堆栈
  1. # 对于第一个仓库(使用第一个 SSH 密钥)
  2. git remote set-url origin git@gitlab-repo1:username/repo1.git
  3. # 对于第二个仓库(使用第二个 SSH 密钥)
  4. git remote set-url origin git@gitlab-repo2:username/repo2.git
复制代码
gitlab-repo1:步调3、设置 SSH 客户端中对应的堆栈别名
username:gitlab中的账号
repo1.git:gitlab工程路径
5、测试是否乐成
  1. # 测试第一个仓库的连接
  2. ssh -T git@gitlab-repo1
  3. # 测试第二个仓库的连接
  4. ssh -T git@gitlab-repo2
复制代码
假如设置精确,您会看到雷同以下的消息:
  1. Welcome to GitLab, @username!
复制代码
二、本地git工程推送到新的gitlab堆栈

1、初始化本地 Git 堆栈
  1. git init
复制代码
2、将代码添加到 Git 版本控制中
  1. git add .
  2. git commit -m "首次提交"
复制代码
3、创建长途堆栈

在 GitHub、GitLab 等平台上创建新的project。
4、本地堆栈与长途堆栈关联

将本地堆栈与长途堆栈关联,雷同第一种方式:
  1. git remote add origin <远程仓库地址>
复制代码
假如本地工程已经是git工程,必要关联到新的git堆栈,忽略前面1、2步
  1. git remote add new-origin <新的远程仓库地址>
复制代码
5、推送到长途堆栈
  1. git push -u origin master # 或 main
复制代码
假如必要推送到新的长途堆栈:
  1. git push -u new-origin master  # 或者 main
复制代码
推送报错如下,有大概是Git 缓冲区太小:
  1. Pushing to https://gitlab.****.git
  2. Enumerating objects: 5594, done.
  3. Counting objects: 100% (5594/5594), done.
  4. Delta compression using up to 8 threads
  5. Compressing objects: 100% (2777/2777), done.
  6. POST git-receive-pack (chunked)4)
  7. error: RPC failed; HTTP 500 curl 22 The requested URL returned error: 500
  8. send-pack: unexpected disconnect while reading sideband packet
  9. Writing objects: 100% (5594/5594), 65.66 MiB | 98.58 MiB/s, done.
  10. Total 5594 (delta 2037), reused 5520 (delta 1998), pack-reused 0
  11. fatal: the remote end hung up unexpectedly
复制代码
修改缓存区巨细
  1. git config http.postBuffer 524288000
复制代码
三、git使用错误

初次毗连主机的安全验证,Git 会将该主机的公钥添加到本地的 ~/.ssh/known_hosts 文件中,确保以后毗连时不再提示。
  1. The authenticity of host 'gitlab.glb.osl-nucleus.com (198.19.0.58)' can't be established.
  2. ED25519 key fingerprint is SHA256:2VcaaCmC4yv73cGnmcdVAL6+2UBqGexFMRS9SmCeL1Y.
  3. This key is not known by any other names.
  4. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
  5. Warning: Permanently added 'gitlab.glb.osl-nucleus.com' (ED25519) to the list of known hosts.
  6. Connection closed by 198.19.0.58 port 22
  7. 致命错误:无法读取远程仓库。
  8. 请确认您有正确的访问权限并且仓库存在
复制代码
SSH 私钥没有精确加载
ssh-add -l:查抄当前署理中加载的密钥
ssh-add ~/.ssh/id_rsa:重新加载密钥
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表