ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

「Git」 多仓库源配置参考文档

2022-07-13 16:33:43  阅读:204  来源: 互联网

标签:Git 仓库 work rsa git 文档 ssh home id


Git」 多仓库源配置参考文档

目录

一、README

作者:博客园 无明之辈

个人主页:https://www.cnblogs.com/ming2

此文档所涉内容仅用于自己钻研学习,相关步骤取决于相关环境,仅供参考。

此文档不可用于流量转移或资源共享,严禁用于一切违法非法行为,

如有违逆,后果自负!情形严重程度,望各位知晓!

风萧萧兮,易水寒,壮士一去兮,不复还。探虎穴兮,入蛟宫,仰天呼气兮,成白虹。

※ 配置须知

此文档仅用于 Windows 环境下配置 git 参考:存在 git 环境【曾配置过 git 环境】

官方网站下载地址:

https://git-scm.com/

1、配置前准备

# 常见异常信息如下:
bad config line 1 in file C:/Users/Administrator/.gitconfig

# 解决方案如下:
在目录 C:\Users\Administrator 下定位到 .gitconfig 文件并删除

# 具体目录:
C:\Users\Administrator\.gitconfig

# 删除文件:
C:\Users\Administrator\.gitconfig

2、搭建 git 目录

# 新建文件夹:
C:\Users\Administrator\.ssh

# 新建文件:
C:\Users\Administrator\.ssh\config

# 注意:config 文件无扩展名

# 具体目录结构:
|-- C:
|	|—— 用户
|	|	|—— Administrator
|	|	|	|—— .ssh
|	|	|	|	|—— config
|	|	|	|	|—— id_rsa_home
|	|	|	|	|—— id_rsa_home.pub
|	|	|	|	|—— id_rsa_work
|	|	|	|	|—— id_rsa_work.pub

3、config 文件配置内容(可根据需求自定义配置)

# For me gitee
Host home_gitee
  HostName gitee.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_home

# For me github
Host home_github
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_home

# For work gitlab
Host work_gitlab
  HostName gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_work

4、id_rsa_home 文件说明

# id_rsa_home 文件为自己环境的 git 仓库源私钥(安全起见不要随意泄露私钥)

5、id_rsa_home.pub 文件说明

# id_rsa_home.pub 文件为自己环境的 git 仓库源公钥(需要添加至相应的远程仓库)

6、id_rsa_work 文件说明

# id_rsa_work 文件为工作环境的 git 仓库源私钥(安全起见不要随意泄露私钥)

7、id_rsa_work.pub 文件说明

# id_rsa_work.pub 文件为工作环境的 git 仓库源公钥(需要添加至相应的远程仓库)

二、Git 配置

参考地址:

https://blog.csdn.net/TylerGuoj/article/details/118889623

https://blog.csdn.net/millery22/article/details/123565892

1、移除用户全局配置

# 移除全局配置账户
git config --global --unset user.name

# 查看全局用户名
git config --global user.name

# 移除全局配置邮箱
git config --global --unset user.email

# 查看全局邮箱
git config --global user.email

2、生成第一对密钥

# 邮箱自定义
ssh-keygen -t rsa -C "your_email@163.com"

# Enter file in which to save the key
/c/Users/Administrator/.ssh/id_rsa_home

# config password
无需密码直接回车(安全起见可自行配置密码)

# confirm password
无需密码直接回车(安全起见可自行配置密码)
# 注意:
操作完成后会在目录 /c/Users/Administrator/.ssh/ 下生成 id_rsa_home 和 id_rsa_home.pub 两个文件,即一对密钥文件

3、生成第二对密钥

# 邮箱自定义
ssh-keygen -t rsa -C "your_email@qq.com"

# Enter file in which to save the key
/c/Users/Administrator/.ssh/id_rsa_work

# config password
无需密码直接回车(安全起见可自行配置密码)

# confirm password
无需密码直接回车(安全起见可自行配置密码)
# 注意:
操作完成后会在目录 /c/Users/Administrator/.ssh/ 下生成 id_rsa_work 和 id_rsa_work.pub 两个文件,即一对密钥文件

4、生成两对密钥的第二种方式

# 其中 id_rsa_home 为文件名	后面为对应仓库的邮箱	不区分邮箱类型
ssh-keygen -t rsa -f ~/.ssh/id_rsa_home -C "your_email@163.com"

# 其中 id_rsa_work 为文件名	后面为对应仓库的邮箱
ssh-keygen -t rsa -f ~/.ssh/id_rsa_work -C "your_email@qq.com"

5、配置多账号(即前面提到的 config 配置文件)

# For me gitee
Host home_gitee
  HostName gitee.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_home

# For me github
Host home_github
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_home

# For work gitlab
Host work_gitlab
  HostName gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_work

6、配置远程仓库

# 将 .ssh 文件夹中生成的公钥文件(id_rsa_home.pub 和 id_rsa_work.pub)的内容复制后配置到相应平台的 "SSH 配置" 中

7、配置项目信息

# 在项目文件夹下配置用户名和邮箱	注意邮箱必须和公钥中的邮箱保持一致
# 在项目 A 中使用 git bash 配置用户 A 信息

# 配置用户 A 的用户名
git config user.name "用户名 A"
# 配置用户 A 的邮箱
git config user.email "your_email@163.com"
# 在项目 B 中使用 git bash 配置用户 B 信息

# 配置用户 B 的用户名
git config user.name "用户名 B"
# 配置用户 B 的邮箱
git config user.email "your_email@qq.com"

8、配置完成后即可正常拉取推送代码~

至此,所有操作到此结束!

春风得意马蹄疾,一日看尽长安花。————〔唐〕孟郊《登科后》

免责声明:

1、一切后果自负,与博主无任何关系;欢迎指出文中有问题的地⽅,会尽快修正,谢谢!

2、博文主要为了记录⼯作、学习中遇到的问题,因本⼈技术有限,可能存在不正确的地⽅,仅供参考;

3、该博文内容参考于相关知识网站及论坛由本人总结而出,不代表本人意见,请⽹友自行注意辨别;

4、博文详细,创作不易,转载⽂章请写明来源,注明原处,感谢原作者的⾟苦创作,望理解,谢谢!

博主主页:https://www.cnblogs.com/ming2 Copyright © 2022 无明之辈

标签:Git,仓库,work,rsa,git,文档,ssh,home,id
来源: https://www.cnblogs.com/ming2/p/16474337.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有