git配置多用户多平台

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

#切换到.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账户,再有其他账户类似:

#切换到.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. Title,可以随便写: 2. 将.ssh目录下对应的id_rsa_github.pub中的内容拷到Key中,点击Add SSH key按钮即可。公司的git类似。

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

#切换到.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

测试配置是否成功

#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上创建项目仓库并拉取:

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上创建仓库并拉取

cd 已有项目目录 git init git remote add origin git@***:***/git-test.git git add . git commit git push -u origin master

__EOF__

本文作者rysinal
本文链接https://www.cnblogs.com/rysinal/p/6381874.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   rysinal  阅读(6173)  评论(1编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示