【问题标题】:GitHub: Multiple account setupGitHub:多账户设置
【发布时间】:2012-11-13 05:15:34
【问题描述】:

自 3 个多小时以来,我一直在尝试为 github 设置多个帐户并且有点累。我已经尝试了几乎所有可能的方式来描述这里、github 和文章,但都没有奏效。我也是 github 和 Unix 的新手。所以需要你的帮助来解决这个问题。下面是我在做什么

我使用的是 Windows 7,并为两个不同的帐户设置了两个 ssh 密钥。

  1. id_rsa
  2. id_rsa_ac2

比在用户的.ssh 目录中创建配置文件并添加以下代码

#Account one
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile /c/Projects/.ssh/id_rsa

#Account two
Host ac2.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile /c/Projects/.ssh/id_rsa_ac2

现在我正在尝试使用以下代码添加遥控器

git remote add origin git@ac2.github.com:myaccount/my.git

并使用以下代码推送

git push origin master

但是当我试图推动它时,它给了我错误: Error: Permission to myaccount/my.git denied to {account}. // where it is considering default user account and not for ac2 user account fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

非常感谢..

其他信息:

我已经测试了id_rsa_ac2 并给出了成功验证的消息。但奇怪的是使用原始帐户而不是 ac2 帐户用户名提供用户名

Hi {username!} You've successfully authenticated, but GitHub does not provide shell access. //here user id should be from ac2 but it is showing userid from id_rsa and not from id_rsa_ac2

信息:最终代码

@VonC 的回答有效,如果有人想使用,可以添加最终代码作为我的回答。

【问题讨论】:

    标签: github


    【解决方案1】:

    所以根据@VonC's answer here我做了什么。

    1. 我已为另一个帐户生成 ssh 密钥并添加了 id_rsa_ac2(其中 ac2 用于第二个帐户)
    2. 不仅仅是交叉检查,它可以与ssh -T ac2.github.com一起使用
    3. 创建 config 文件(无扩展名)在 /c/Users/yourname/.ssh/ 目录

    这是我用于配置文件的代码

    #Account one
    Host github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile /c/Users/yourname/.ssh/id_rsa
        User git
    
    #Account two
    Host ac2.github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile /c/Users/yourname/.ssh/id_rsa_ac2
        User git
    

    所以现在,一旦您完成此操作,您就可以根据需要开始使用这两个帐户了。

    对于主帐户,我使用git remote add origin git@github/youraccount/rep.git 添加了远程作为来源 比推送使用git push origin master 这将上传到您的第一个帐户。

    为使用的第二个(ac2)帐户添加远程git remote add ac2 ac2.github/yoursecondaccount/rep.git 而不是推送使用git push ac2 master 这将上传到第二个(ac2)帐户。

    要检查它是否添加了远程使用git remote -v,如果您想删除任何人而不是使用git remote rm origin,其中origin是您添加的远程。

    希望此信息对遇到相同问题的其他人有所帮助。

    再次感谢@VonC

    【讨论】:

    • 都是因为你!再次感谢您的大力帮助和赞赏:)
    • @CodeLover 我给另一个答案留下了一个脚本来自动化一切。 (虽然这个过程有点不同,但它适用于 GitLab,我也假设 GitHub。)如果你认为我应该添加任何内容,请告诉我。
    【解决方案2】:

    要考虑到您的配置,您需要在远程地址中使用其Host 名称:

    git remote add origin ac2.github.com:myaccount/my
    

    如果您已将HOME 环境变量(在Windows 上默认未定义,但在使用msysgit git-cmd.bat 时已定义)到您的.ssh 目录所在的目录,其id_rsa_ac2 私钥和id_rsa_ac2.pub 公钥,那么它将起作用。

    【讨论】:

    • 帮助很大!它已经完成并且两个帐户都可以使用。只是为了知识,我必须使用你描述的两个答案来定义User git,而不是它起作用。非常感谢
    • @VonC 我留下了另一个答案,其中有一个脚本可以自动执行所有操作。 (虽然这个过程有点不同,但它适用于 GitLab,我也会假设 GitHub。)如果你认为我应该添加任何内容,请告诉我
    • @CodeLover 6 年后,我的回答还不够好?我同意,其他答案有所改进,但我仍然回答了你原来的问题
    • @VonC 不是关于此,而是要选择对其他人更有用的答案以及详细的步骤。无论如何,我再次选择了你的答案。
    • @CodeLover 同意:请选择您认为对用户有帮助的答案。昨天我很惊讶;)
    【解决方案3】:

    这是一个自动将两个 GitLab 帐户添加到您的设置的脚本。

    setup-gitlab.sh

    #!/bin/bash
    
    # VERIFIED FOR FEDORA 27 MATE (Likely to work in others distros)
    # Multi Account SSH for GitLab/OpenSSH Setup.
    ROOT=root
    if (( whoami == $ROOT ))
        then 
        echo "Run as standard user"
    elif [[ -z $1 || -z $2 ]]
        then
        echo "command usage: setup-gitlab.bash workemail@domain.com homeemail@domain.com"
    elif [[ ! $1 =~ .*@.*\..* ]]
        echo "Work email is not in the correct format. Must match regex .*@.*\..*"
    elif [[ ! $2 =~ .*@.*\..* ]]
        echo "Home email is not in the correct format. Must match regex .*@.*\..*"
    else
        HOMEEMAIL=$1
        WORKEMAIL=$2
        USRNAME=`whomai`
    
    # /home/<username>/.ssh/
    # ├── config
    # ├── home-gitlab-key
    # ├── home-gitlab-key.pub
    # ├── known_hosts
    # ├── work-gitlab-key
    # └── work-gitlab-key.pub
    
    #Executed to match the above directory.
        ssh-keygen -t rsa -C "$WORKEMAIL" -b 4096 -f work-gitlab -N ""
        ssh-keygen -t rsa -C "$HOMEEMAIL" -b 4096 -f home-gitlab -N ""
    
    # Agent Configuration Setup (.ssh/config)
        cat >> ~/.ssh/config <<EOF
    Host gitlab-work
      HostName gitlab.com
      User git
      IdentityFile /home/$USRNAME/.ssh/work-gitlab-key
    
    Host gitlab-home
      HostName gitlab.com
      User git
      IdentityFile /home/$USRNAME/.ssh/home-gitlab-key
    EOF
    
    # Agent Setup (potentially optional???)
        cat >> ~/.bashrc <<'EOF'
    eval "$(ssh-agent -s)"
    for i in `ls ~/.ssh/*.pub` ; do ssh-add ${i::-4} ; done
    EOF
    
        . .bashrc
    
    fi
    

    运行脚本后,您需要将创建的两个公钥的内容分别复制到每个 GitLab 帐户。

    另外注意,当使用git clone git@gitlab.com:&lt;account&gt;/&lt;project&gt;.git 时,您应该如下替换gitlab.com

    git clone git@gitlab-home:<account>/<project>.git
    

    git clone git@gitlab-work:<account>/<project>.git
    

    分别。

    【讨论】:

      【解决方案4】:

      我正在使用这个脚本,它切换缩进和其他所有必要的东西,比如 git 设置

      https://gist.github.com/milosjanda/a86ebc039293f22fabe723024ec42b46

      if [[ -f ~/.ssh/.work ]]; then
        echo "swith to HOME"
        # mv ~/.ssh/id_rsa ~/.ssh/work; mv ~/.ssh/home ~/.ssh/id_rsa
        rm ~/.ssh 2> /dev/null
        ln -s ~/.ssh-work/home ~/.ssh
        git config --global user.email "my.name@gmail.com"
      else
        echo "swith to WORK"
        # mv ~/.ssh/id_rsa ~/.ssh/home; mv ~/.ssh/work ~/.ssh/id_rsa
        rm ~/.ssh 2> /dev/null
        ln -s ~/.ssh-work/work ~/.ssh
        git config --global user.email "my.name@company.eu"
      fi
      
      # Delete all identities from ssh-agent
      ssh-add -D
      
      # Add new identity to ssh-agent
      ssh-add
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-04
        • 1970-01-01
        • 1970-01-01
        • 2020-08-18
        相关资源
        最近更新 更多