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

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

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

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

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


当前回答

您应该也不应该使用一些常见凭证推送到项目。在新机器上启动后,按照以下步骤正确设置和使用gitlab凭证:

在计算机上创建公共/私有SSH密钥 复制粘贴公钥到gitlab/github UI界面(任何通过CMD行提示如何操作的人都可以获得免费啤酒…) 请确保您通过git而不是HTTP url克隆回购 设置git别名,以避免git命令经常输入相同的前缀 在git提交期间,总是使用author和e-mail标记 使用git正常你会这样做

这一切如下:

 # create the public / private key credentials on that specific machine
 ssh-keygen -t rsa -b 4096 -C "<<you>>@org.net" -f ~/.ssh/id_rsa.<<you>>.`hostname -s`

 # setup your public key in the gitlab ui 
 cat ~/.ssh/id_rsa.<<you>>.`hostname -s`

 # make sure you clone the repo via the git and not http url
 git clone git@git.in.org.net:org/some-repo.git

 # set the git alias to avoid constant typing of the repeating prefix to the git cmd
 alias git='GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa.<<you>>.`hostname -s`" git'

 # during git commit ALWAYS use the author and e-mail flags
 git add --all ; git commit -nm "$git_msg" --author "YourFirstName YourLastName <you@phz.fi>"

 # use git as normal
 git fetch --all; git pull --all 

其他回答

在我的情况下,我有我的工作帐户在Git-Lab和我的个人帐户在GitHub。 我的Git-Lab帐户被全局配置为可以从我笔记本电脑的所有目录访问,如下所示:

git config --global user.name WORK_USERNAME
git config --global user.email WORK_USERNAME@example.com

因此,如果你正在寻找一个不使用SSL的解决方案,你可以在一个空文件夹中使用git init,然后在该文件夹中插入你的个人帐户凭据:

git config user.name PERSONAL_USERNAME
git config user.email PERSONAL_USERNAME@example.com

注意这里,global没有设置,因为你只想从那里访问你的个人git,而不是在任何地方,所以文件夹内的任何东西都将其git凭据连接到你的个人帐户,而文件夹外的将连接到你的工作帐户。

之后,你可以克隆你的存储库,比如git克隆your_repo_link.git。然后会弹出一个新窗口,要求你登录你的github帐户。

要验证您的过程,请在您创建的文件夹中尝试git config——list,您应该会看到工作和个人用户名以及电子邮件及其目录。

在这个文件夹之外,如果你运行git config——list,你应该只看到你的工作用户名和电子邮件。

就是这样,希望这对没有配置SSL的人有所帮助。

我使用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的注释。

mishaba链接到http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/上的详细信息对我来说非常有用。

从那一页开始:

$ touch ~/.ssh/config

然后编辑该文件如下所示(每个帐户一个条目):

#Default GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

Host github-COMPANY
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_COMPANY

只要在你最喜欢的编辑器中添加这一行,你就完成了一生

git remote set-url origin https://user-token-of-particular-user@github.com/profile-name/repo-name

使用个人访问令牌的个人目录.gitconfig

如果您不想修改您的主机文件,使用SSH密钥,或者为每个repo设置一个.gitconfig,那么您可以使用一个个人的.gitconfig,它基本上包含在根级配置中。给定一个OSX目录结构

# root level git config
~/.gitconfig

# your personal repos under some folder like
../personal/
../home/
~/Dropbox/

在你的个人文件夹中添加一个.gitconfig,比如~/Dropbox/.gitconfig

[user]
    email = first.last@home.com
    name = First Last
[credential]
    username = PersonalGithubUsername
    helper = osxkeychain

在根级.gitconfig中添加includeIf部分,以便在您的个人目录中获取您的个人配置。这里的任何设置都将覆盖根配置,只要includeIf出现在您想要覆盖的设置之后。

[user]
    email = first.last@work.com
    name = "First Last"
[credential]
    helper = osxkeychain
[includeIf "gitdir:~/Dropbox/**"]
    path = ~/Dropbox/.gitconfig

尝试推动你的个人回购或从你的私人回购

git push
# prompts for password

当出现提示时,输入您的个人密码,或者最好输入您在帐户开发人员设置中创建的个人访问令牌。输入该令牌作为密码。

假设你已经在使用git-credential-osxkeychain,你的个人凭证应该存储在你的钥匙链中,所以两个github条目将显示,但使用不同的帐户。