GitHub:多帐户设置

问题描述 投票:7回答:3

我正在尝试超过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 [email protected]: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

INFORMATION: Final Code

如果有人想要使用,@ VonC的答案有效并添加最终代码作为我的答案。

github
3个回答
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 public关键,然后它会工作。


4
投票

所以根据@ VonC在这里的回答我做了什么。

  1. 我为另一个帐户生成了ssh密钥,并添加了id_rsa_ac2(其中ac2用于第二个帐户)
  2. 不只是交叉检查它是否适用于ssh -T ac2.github.com
  3. 在/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这将上传到您的第一个帐户。

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

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

希望这些信息能够帮助其他有同样问题的人。

再次感谢@VonC


2
投票

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

https://gitlab.com/procyclinsur/Fedora-Environment

#!/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 [email protected] [email protected]"
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 [email protected]:<account>/<project>.git时,你应该更换gitlab.com如下。

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

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

分别。

© www.soinside.com 2019 - 2024. All rights reserved.