【问题标题】:How can I have multiple git accounts on a single development machine?如何在一台开发机器上拥有多个 git 帐户?
【发布时间】:2018-10-25 12:51:49
【问题描述】:

我试图在一台开发机器上同时处理多个(两个或三个)GitLab(甚至 GitHub)项目。因为在配置 IDE 和 git 服务时,当我尝试使用不同的用户名/密码签出或克隆另一个项目时,IDE 和 git 服务具有我的主要用户的数据,系统说要么项目未找到,要么我无权执行此操作。

如何在一台开发机器上配置和使用多个 git 用户?

【问题讨论】:

标签: git github gitlab multi-user


【解决方案1】:

默认情况下,git 使用系统范围的配置文件或存储在主目录顶部的配置文件。

但是您也可以通过手动编辑或使用git config 在每个存储库中设置一个文件.git/config。在里面,你可以指定以下部分:

[user]
    name = Your name
    email = yourownemail@yourprovider.com

…

[credential "<your_project_hoster_url>"]
    username = <username to log in with>

您可以查看git configgit credentials 以了解更多信息。

【讨论】:

    【解决方案2】:

    您可以使用将您的凭据存储在每个项目中。

    git config credential.helper store
    git push origin HEAD
    write user and password
    

    进入项目文件夹并输入 git 命令。

    【讨论】:

    • 如果您在一个仓库中有很多遥控器,则无法使用。
    【解决方案3】:

    您可以配置每个存储库的用户详细信息。

    git config user.name 'Your Name'
    git config user.email 'name@example.com'
    

    GitHub 可以配置为识别分配给电子邮件地址的多个 ssh 密钥,并在需要时将电子邮件消息发送到相应的。

    【讨论】:

    • 非常感谢!我已经将第一个作为答案,但一切都像魅力一样!
    【解决方案4】:

    如果您更喜欢 https 而不是 SSH 密钥解决方案,则以下内容至少适用于 Windows 上的 Github(以及使用凭据管理器与 TortoiseGit 连接):

    1. 在您的本地存储库中(或在克隆时),将远程(原始)url 设置为
    https://[yourusername]@github.com/[org]/[repo].git
    

    (这里的重要部分是 yourusername@github.com 因为这会在 Windows 凭据存储中创建一个 git:https://yourusername@github.com 条目,该条目不同于保存所有凭据的常规 git:https://github.com 条目“常规”远程网址)

    如果您想通过repo/.git/config 文件为现有存储库编辑它,这类似于

    [remote "origin"]
        url = https://[yourusername]@github.com/[org]/[repo].git
        fetch = +refs/heads/*:refs/remotes/origin/*
    
    1. 此外,在repo/.git/config 文件中设置credentialuser 属性,以便您的提交具有正确的作者:
    [credential]
       username = [Github-Username]
    [user]
       name = [Fullname]
       email = [Email Address]
    

    在您的 git 远程操作(获取、推送等)中,将根据您的身份验证管理器要求您进行身份验证。

    【讨论】:

      【解决方案5】:

      Git 多用户配置管理器gum

      安装

      $ npm i -g @gauseen/gum
      

      示例

      $ gum list
      
      Currently used name=gauseen email=gauseen@gmail.com
      ┌────────────┬─────────┬─────────────────────────┐
      │ group-name │    name │                   email │
      ├────────────┼─────────┼─────────────────────────┤
      │    global  │ gauseen │ gauseen@gmail.com       │
      │    user1   │ li si   │ lisi@gmail.com          │
      │    user2   │ wang er │ wanger@gmail.com        │
      └────────────┴─────────┴─────────────────────────┘
      $ gum use user1
      
      Currently used name=li si email=lisi@gmail.com
      

      【讨论】:

        猜你喜欢
        • 2010-10-14
        • 1970-01-01
        • 2021-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-21
        • 1970-01-01
        • 2015-07-02
        相关资源
        最近更新 更多