专业的编程技术博客社区

网站首页 > 博客文章 正文

git 多用户配置(git多用户管理及权限控制)

baijin 2024-10-10 04:23:02 博客文章 8 ℃ 0 评论

基础环境

基础环境,尽量保持一致,否则可能会出现意料之外的问题。

系统

macos/win10/Centos7

git版本

2.34.1

前期准备

清除 git 全局配置

# 查看 git 全局配置
git config --global --list

# 删除 git 全局配置的用户名和邮箱
git config --global --unset user.name
git config --global --unset user.email

生成新秘匙

先将用户目录下.ssh文件夹下无用的旧的秘钥删除,避免干扰

win10 用户使用 git bash 终端

# 进入 .ssh 目录
cd ~/.ssh

# 执行以下命令一路回车生成 gitee、github、gitlab 秘钥
# 生成 gitee 秘钥
ssh-keygen -t rsa -C "907980857@qq.com" -f "id_rsa_gitee"

# 生成 github 秘钥
ssh-keygen -t rsa -C "907980857@qq.com" -f "id_rsa_github"

# 生成 gitlab 秘钥
ssh-keygen -t rsa -C "jack.wang" -f "id_rsa_gitlab"

# 将 ssh key 添加到 SSH agent 中
# 如果提示以下内容
# Could not open a connection to your authentication agent
# 先执行 ssh-agent bash
ssh-add ~/.ssh/id_rsa_gitee

ssh-add ~/.ssh/id_rsa_github

ssh-add ~/.ssh/id_rsa_gitlab

# 提示以下内容意味添加成功
# Identity added: /c/Users/arvin/.ssh/id_rsa_gitee (907980857@qq.com)
# Identity added: /c/Users/arvin/.ssh/id_rsa_github (907980857@qq.com)
# Identity added: /c/Users/arvin/.ssh/id_rsa_gitlab (jack.wang)

配置账号对应

# 在 .ssh 目录下执行以下命令
touch config
vim config

# 添加以下配置
# HostName 仓库网站的域名
# User 用户名
# IdentityFile 私钥的绝对路径
# PreferredAuthentications 配置登录时用什么权限认证
Host gitee
HostName gitee.com
User jincheny
IdentityFile ~/.ssh/id_rsa_gitee

Host github
HostName github.com
User jincheny
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github

Host gitlab
HostName git.gitlab.com
User jack.wang
IdentityFile ~/.ssh/id_rsa_gitlab

添加ssh公钥

# 复制 cat 查看到的公钥添加到对应平台
cat id_rsa_gitee.pub
cat id_rsa_github.pub
cat id_rsa_gitlab.pub

# 验证连接是否成功
ssh -T git@gitee
ssh -T git@github
ssh -T git@gitlab

# 提示
# ……
# Are you sure you want to continue connecting (yes/no)?
# ……
# 输入 yes

修改局部配置

在项目根目录下进行单独配置

# 拉取项目代码
git clone git@gitee.com:eagle_0810/met.git

# 配置局部用户名
git config user.name "jincheny"

# 配置局部邮箱
git config user.email "907980857@qq.com"

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表