如何在一台机器上管理多个 GitHub 账户

用 SSH 密钥在一台机器上管理多个 GitHub 账户

如何用 SSH 密钥在一台机器上管理多个 GitHub 账户 (freecodecamp.org)

如何在同一台电脑上同时使用多个Git账号? - 知乎 (zhihu.com)

  1. 生成多个 SSH 密钥

    C:\Users\Administrator\.ssh 文件夹中打开 gitbash,生成两个 SHH 密钥,分别作用于我的两个github账号。

    bash 复制代码
    ssh-keygen -t rsa -C "one@gmail.com" -f "id_rsa_one"
    ssh-keygen -t rsa -C "two@gmail.com" -f "id_rsa_two"

    其中邮箱对应两个github账号注册时的邮箱。

  2. 将两个密钥加入到对应的github账户

  3. 编辑配置文件

    编辑 ~/.ssh/config 文件,没有就建一个,Host 名称可以根据你的需求更改。

    复制代码
    # one
    Host github_one
    Hostname ssh.github.com
    IdentityFile ~/.ssh/id_rsa_one
    port 22
    
    # two
    Host github_two
    Hostname ssh.github.com
    IdentityFile ~/.ssh/id_rsa_two
    port 22

    修改之后,在 git bash 中运行以下命令,检查是否连接正常。

    bash 复制代码
    ssh -T git@github_one
    ssh -T git@github_two

    如果都能正常返回如下信息,就说明配置正常。

    text 复制代码
    Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

    同样的方式你就可以配置更多的以SSH登录的不同git用户。

  4. 配置仓库设置

    全局设置可以作为你默认上传的账号,在需要用其他账号时,需要重新配置 user.nameuser.email

    bash 复制代码
    # 添加本地设置
    # 设置为私有仓库的GitHub账号邮箱和公有账号的GitHub邮箱。
    git config --local user.name xxxx
    git config --local user.email xxx
  5. 上传文件

    • 添加远程仓库

      未创建远程仓库时,采用 git remote add 添加远程仓库,origin可替换仓库的名称。

      bash 复制代码
      # 远程仓库不存在
      git remote add origin git@github_two:xxx/example.git
      
      # 远程仓库已存在
      git remote add test git@github_two:xxx/test.git
      error: remote test already exists.
    • 上传至远程仓库

      先用以下命令更新 test 远程仓库的 URL,以便您可以使用新的地址进行推送或拉取操作。

      bash 复制代码
      git remote set-url test git@github_two:two/test.git

      然后在VScode中推送文件即可。

相关推荐
:-)6 小时前
开源项目二开 Git 规范工作流
git
阿米亚波10 小时前
【C/C++包管理器】vcpkg(by microsoft)
c语言·c++·git·vscode·microsoft·github·vcpkg
苍煜12 小时前
Git Worktree 多工作树实战教学-工作多分支实用教程
git
打工的小王13 小时前
github教程之多人协作
github
笨笨狗吞噬者13 小时前
IRIS: shell 中的智能实时输入提示工具
github·shell
RainingTime15 小时前
新版IDEA使用传统用户名、密码登录Git
git
fthux15 小时前
MCP协议开发实战:从零搭建AI Agent工具链
前端·人工智能·ai·开源·github
@CLoudbays_Martin1116 小时前
Linux 服务器如何查看是否被植入后门?
linux·服务器·网络·安全·github·ssl
一次旅行16 小时前
Ollama本地私有化大模型完整工程实战
人工智能·机器学习·github
华科大胡子16 小时前
GitHub Copilot 能换成本地模型吗?—— 本地化替代方案深度解析
人工智能·github·copilot