同时使用GitHub和GitLab的Git配置
同时使用GitHub和GitLab的Git配置 在实际开发中,我们经常需要同时使用 GitHub 和 GitLab 来管理不同的项目。本文将介绍如何配置 Git 以同时使用这两个平台。
配置SSH密钥 1. 生成多个SSH密钥 为了区分不同的 Git 托管平台,我们需要为每个平台生成独立的 SSH 密钥:
# 为 GitHub 生成密钥 ssh-keygen -t rsa -C "your_email@example.com" -f ~/.ssh/id_rsa_github # 为 GitLab 生成密钥 ssh-keygen -t rsa -C "your_email@example.com" -f ~/.ssh/id_rsa_gitlab 2. 配置SSH config文件 编辑 ~/.ssh/config 文件,为不同的平台配置不同的 SSH 密钥:
# GitHub配置 Host github.com HostName github.com User git IdentityFile /c/Users/zhang/.ssh/id_ed25519_github IdentitiesOnly yes # GitLab配置 Host self.gitlab HostName self.gitlab.com Port 6622 User git IdentityFile /c/Users/zhang/.ssh/id_rsa_gitlab IdentitiesOnly yes 3. 添加SSH密钥到平台 将公钥添加到对应的平台: