【转载】git配置多用户多平台

在Git使用中经常会碰到多用户问题,例如:你在公司里有一个git账户,在github上有一个账户,并且你想在一台电脑上同时对这两个git账户进行操作,此时就需要进行git多用户配置。
首先配置不同的SSH KEY,使用ssh-keygen命令产生两个不同的SSH KEY,进入.ssh目录:

1
2
3
4
5
6
7
8
#切换到.ssh目录
cd ~/.ssh 
#使用自己的企业邮箱产生SSH KEY
ssh-keygen -t rsa -C "mywork@email.com" 
#企业的可以使用id_rsa,也可以自己起名,例如:id_rsa_work
Enter file in which to save the key (/root/.ssh/id_rsa): id_rsa
#将ssh key添加到SSH agent中,该命令如果报错:Could not open a connection to your authentication agent.无法连接到ssh agent,可执行ssh-agent bash命令后再执行ssh-add命令。
ssh-add ~/.ssh/id_rsa

同理,配置自己的github账户,再有其他账户类似:

1
2
3
4
5
6
7
8
#切换到.ssh目录
cd ~/.ssh 
#使用自己github的注册邮箱产生SSH KEY
ssh-keygen -t rsa -C "mygithub@email.com" 
#github的SSH KEY
Enter file in which to save the key (/root/.ssh/id_rsa): id_rsa_github
#将ssh key添加到SSH agent中 ,该命令如果报错:Could not open a connection to your authentication agent.无法连接到ssh agent,可执行ssh-agent bash命令后再执行ssh-add命令。
ssh-add ~/.ssh/id_rsa_github

在生成ssh key之后,需要分别在github的profile中和公司git的profile中编辑SSH KEY,以github为例:

1
2
1. Title,可以随便写:
2. 将.ssh目录下对应的id_rsa_github.pub中的内容拷到Key中,点击Add SSH key按钮即可。公司的git类似。

  然后在.ssh目录下配置config文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#切换到.ssh目录
cd ~/.ssh
#创建并编辑config文件
vim config
# 粘贴到config文件中
#公司的git地址
Host git.***.com 
   User git
   Hostname git.***.com  #公司的git地址
   IdentityFile ~/.ssh/id_rsa  #访问公司git的SSH KEY
   Port   ***  #公司的git端口
 
Host github.com
   User git
   Hostname github.com #github的地址
   IdentityFile ~/.ssh/id_rsa_github  #访问github的SSH KEY

测试配置是否成功

1
2
3
4
5
6
7
8
#github的地址
ssh -T git@github.com
#出现如下内容,表示成功链接github,***为你的github账户的用户名
Hi ***! You've successfully authenticated, but GitHub does not provide shell access.
#公司的git地址
ssh -T git@git.***.com
#出现如下内容,表示成功链接github,***为公司git账户的用户名
Hi ***! You've successfully authenticated, but GitHub does not provide shell access.

提交代码

本地如果没有项目,git上创建项目仓库并拉取:

1
2
3
4
5
6
git clone git@***:***/git-test.git
cd git-test
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

  本地已有项目,git上创建仓库并拉取

1
2
3
4
5
6
cd 已有项目目录
git init
git remote add origin git@***:***/git-test.git
git add .
git commit
git push -u origin master

  

posted @   二月无雨  阅读(149)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示