试图从我的电脑上完成我实际的“工作”回购,以及我在GitHub上的回购。

工作账户是先建立的,一切都完美无缺。

然而,我的账户似乎无法推送到我的回购,这是在另一个账户/电子邮件下设置的。

我尝试将我的工作密钥复制到我的帐户,但这抛出了一个错误,因为密钥当然只能附加到一个帐户。

我如何用各自的GitHub凭证推/拉两个帐户?


当前回答

通过在~/.ssh/config中为github.com创建不同的主机别名, 并为每个主机别名提供自己的SSH密钥,您可以轻松地使用多个 Github账户没有混淆。 这是因为github.com不是用用户来区分的,用户总是用git来区分, 但用的是你用来连接的SSH密钥。 只要使用自己的主机别名配置远程源即可。”

以上摘要来自下面博客文章的评论。

我发现这个解释是最清楚的。至少在2012年4月,这对我来说是有效的。

http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/

其他回答

您所需要做的就是使用多个SSH密钥对配置您的SSH设置。

这个链接很容易理解(谢谢Eric): http://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574 生成SSH密钥(Win/msysgit): https://help.github.com/articles/generating-an-ssh-key/

第一个环节的相关步骤:

Generate an SSH-key: ssh-keygen -t ed25519 -C "john@doe.example.com" Follow the prompts and decide a name, e.g. id_ed25519_example_company. Copy the SSH public-key to GitHub from ~/.ssh/id_ed25519_doe_company.pub and tell ssh about the key: ssh-add ~/.ssh/id_ed25519_doe_company Create a config file in ~/.ssh with the following contents: Host github-doe-company HostName github.com User git IdentityFile ~/.ssh/id_ed25519_doe_company Add your remote: git remote add origin git@github-doe-company:username/repo.git or change using: git remote set-url origin git@github-doe-company:username/repo.git


此外,如果你正在使用多个使用不同角色的存储库,你需要确保你的各个存储库有相应的用户设置覆盖:

设置用户名,电子邮件和GitHub令牌-覆盖个人回购的设置 https://help.github.com/articles/setting-your-commit-email-address-in-git/

注意: 有些人可能需要不同的电子邮件用于不同的存储库,从git 2.13开始,你可以通过编辑全局配置文件在目录的基础上设置电子邮件:~/。Gitconfig使用这样的条件:

[user]
    name = Default Name
    email = defaultemail@example.com

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

然后是特定于工作的配置~/work/。Gitconfig看起来是这样的:

[user]
    name = Pavan Kataria
    email = pavan.kataria@example.com

谢谢@alexg在评论中告诉我这件事。

可以有多种方法来做到这一点,但以下解决方案为我工作,非常简单。 我不是试图用SSH,我的步骤和解决方案是基于HTTPS。

Create your project DIR on your local machine. Example d:\test_git_multiple_account go to the folder "test_git_multiple_account" Add few files here into the DIR Open Git bash here and run following command a. git init // initialization b. git add , // add c. git commit -m "initial commit" you will get following output : in my case i use to add one python file created from code. **[master (root-commit) d4defd9] initial commit 2 files changed, 4 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 Hello.py** d. git remote add origin <HTTPS repo link> e. git remote -v // check the repo version f. git push origin master it will ask your git hub user name and password via popup screen. you will get the following output Counting objects: 100% (5/5), done. Delta compression using up to 4 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 411 bytes | 31.00 KiB/s, done. Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'master' on GitHub by visiting: remote: https://github.com/vishvadeepaktripathi/Hello_Py/pull/new/master remote: To https://github.com/vishvadeepaktripathi/Hello_Py.git * [new branch] master -> master

这将创建一个名为master的新分支。 你可以提交到主分支一旦你改变了分支,在这种情况下,你现有的文件将被删除。所以我建议签入主分支到第一步,然后继续为每个命令,如果你想直接签入主分支。 在第一次登录时,它可能会给你一个错误消息,并再次要求登录名和密码,然后它会将您的更改发布到Git中心。

一旦这样做,你会得到消息到新的拉请求到你的github帐户。 您可以将您的更改从主分支合并到主分支。

我在这里创建了主分支,根据你的选择命名你的分支。 还要附加屏幕截图。 在这里输入图像描述

我使用shell脚本将我切换到任何我想要“活跃”的帐户。从本质上讲,您可以从头开始,正确配置一个帐户并正常工作,然后将这些文件移动到具有适当前缀的名称。从那时起,你可以使用命令“github”,或“gitxyz”切换:

# my github script
cd ~/.ssh

if [ -f git_dhoerl -a -f git_dhoerl.pub -a -f config_dhoerl ]
then
    ; 
else 
    echo "Error: missing new files"
    exit 1
fi 

# Save a copy in /tmp, just in case
cp id_rsa /tmp
cp id_rsa.pub /tmp
cp config /tmp
echo "Saved old files in /tmp, just in case"

rm id_rsa
rm id_rsa.pub
rm config
echo "Removed current links/files"

ln git_dhoerl id_rsa
ln git_dhoerl.pub id_rsa.pub
ln config_dhoerl config

git config --global user.email "dhoerl@<company>.com"
git config --global github.user "dhoerl"        
git config --global github.token "whatever_it_is"

ssh-add -D

我在这方面运气不错。我还在Xcode中创建了一个运行脚本(为Mac用户),所以它不会构建我的项目,除非我有适当的设置(因为它使用git):

运行脚本放在Dependencies之后(使用/bin/ksh作为shell):

if [ "$(git config --global --get user.email)" != "dhoerl@<company>.com" ]
then
    exit 1
fi

编辑:添加测试新文件是否存在,并将旧文件复制到/tmp以解决下面@naomik的注释。

以防你不想弄乱~/。这里提到的Ssh /config文件,你可以运行git config core。ssh -i ~/.使用实例Ssh /custom_id_rsa”,你想从一个不同的帐户提交。

其余的设置是相同的:

使用SSH -keygen -t rsa -f ~/为第二个帐户创建新的SSH密钥。SSH -f ~/.ssh/custom_id_rsa 使用您的其他帐户登录github,转到https://github.com/settings/keys,并粘贴~/.ssh/custom_id_rsa.pub的内容 确保您使用SSH而不是HTTPS作为远程url: git remote set-url origin git@github.com:upstream_project_teamname/upstream_project.git

通过在~/.ssh/config中为github.com创建不同的主机别名, 并为每个主机别名提供自己的SSH密钥,您可以轻松地使用多个 Github账户没有混淆。 这是因为github.com不是用用户来区分的,用户总是用git来区分, 但用的是你用来连接的SSH密钥。 只要使用自己的主机别名配置远程源即可。”

以上摘要来自下面博客文章的评论。

我发现这个解释是最清楚的。至少在2012年4月,这对我来说是有效的。

http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/