git 同一台电脑上有多个github账号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3860112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple github accounts on the same computer?
提问by Kevin Whitaker
Trying to work on my both my actual "work" repos, and my personal repos on git hub, from my computer.
试图从我的计算机上处理我的实际“工作”存储库和我在 git hub 上的个人存储库。
The work account was set up first, and everything works flawlessly.
首先设置工作帐户,一切正常。
My personal account, however cannot seem to push to my personal repo, which is set up under a different account/email.
但是,我的个人帐户似乎无法推送到我在不同帐户/电子邮件下设置的个人存储库。
I've tried copying my work key up to my personal account, but that throws an error, because of course a key can be only attached to one account.
我曾尝试将我的工作密钥复制到我的个人帐户,但这会引发错误,因为当然一个密钥只能附加到一个帐户。
How can I push/pull to and from both accounts, from their respective github credentials?
如何从两个帐户各自的 github 凭据推送/拉取?
采纳答案by Pavan
All you need to do is configure your SSH setup with multiple SSH keypairs.
您需要做的就是使用多个 SSH 密钥对配置您的 SSH 设置。
This link is easy to follow (Thanks Eric): http://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574
Generating SSH keys (Win/msysgit) https://help.github.com/articles/generating-an-ssh-key/
这个链接很容易理解(感谢 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/generate-an-ssh-key/
Also, if you're working with multiple repositories using different personas, you need to make sure that your individual repositories have the user settings overridden accordingly:
此外,如果您使用不同的角色处理多个存储库,则需要确保您的各个存储库相应地覆盖了用户设置:
Setting user name, email and GitHub token – Overriding settings for individual repos https://help.github.com/articles/setting-your-commit-email-address-in-git/
设置用户名、电子邮件和 GitHub 令牌 – 覆盖单个存储库的设置 https://help.github.com/articles/setting-your-commit-email-address-in-git/
Hope this helps.
希望这可以帮助。
Note:Some of you may require different emails to be used for different repositories, from git 2.13you can set the email on a directory basis by editing the global config file found at: ~/.gitconfig
using conditionals like so:
注意:有些人可能需要将不同的电子邮件用于不同的存储库,从 git 2.13 开始,您可以通过编辑在以下位置找到的全局配置文件以目录为基础设置电子邮件:~/.gitconfig
使用如下条件:
[user]
name = Pavan Kataria
email = [email protected]
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
And then your work specific config ~/work/.gitconfig would look like this:
然后你的工作特定配置 ~/work/.gitconfig 看起来像这样:
[user]
email = [email protected]
Thank you @alexg for informing me of this in the comments.
感谢@alexg 在评论中告知我这一点。
回答by Greg Leszek
Use HTTPS:
使用 HTTPS:
change remote url to https:
将远程 url 更改为 https:
git remote set-url origin https://[email protected]/USERNAME/PROJECTNAME.git
and you are good to go:
你很高兴去:
git push
To ensure that the commits appear as performed by USERNAME, one can setup the user.nameand user.emailfor this project, too:
为确保提交显示为由 USERNAME 执行,您也可以为该项目设置user.name和user.email:
git config user.name USERNAME
git config user.email [email protected]
回答by Thank you
Getting into shape
进入体型
To manage a git repo under a separate github/bitbucket/whatever account, you simply need to generate a new SSH key.
要在单独的 github/bitbucket/whatever 帐户下管理 git repo,您只需要生成一个新的 SSH 密钥。
But before we can start pushing/pulling repos with your second identity, we gotta get you into shape – Let's assume your system is setup with a typical id_rsa
and id_rsa.pub
key pair. Right now your tree ~/.ssh
looks like this
但在此之前,我们可以开始推/拉式回购与你的第二个身份,我们得让你进入形状-让我们假设你的系统是设置具有典型id_rsa
和id_rsa.pub
密钥对。现在你的tree ~/.ssh
样子
$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── id_rsa
└── id_rsa.pub
First, name that key pair – adding a descriptivename will help you remember which key is used for which user/remote
首先,命名该密钥对 - 添加描述性名称将帮助您记住哪个密钥用于哪个用户/远程
# change to your ~/.ssh directory
$ cd ~/.ssh
# rename the private key
$ mv id_rsa github-mainuser
# rename the public key
$ mv id_rsa.pub github-mainuser.pub
Next, let's generate a new key pair– here I'll name the new key github-otheruser
接下来,让我们生成一个新的密钥对——这里我将命名新密钥github-otheruser
$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/github-otheruser
Now, when we look at tree ~/.ssh
we see
现在,当我们看时,tree ~/.ssh
我们看到
$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── github-mainuser
├── github-mainuser.pub
├── github-otheruser
└── github-otheruser.pub
Next, we need to setup a ~/.ssh/config
file that will define our key configurations. We'll create it with the proper owner-read/write-only permissions
接下来,我们需要设置一个~/.ssh/config
文件来定义我们的关键配置。我们将使用适当的所有者读/写权限创建它
$ (umask 077; touch ~/.ssh/config)
Open that with your favourite editor, and add the following contents
用你喜欢的编辑器打开它,并添加以下内容
Host github.com
User git
IdentityFile ~/.ssh/github-mainuser
Host github.com-otheruser
HostName github.com
User git
IdentityFile ~/.ssh/github-otheruser
Presumably, you'll have some existing repos associated with your primary github identity. For that reason, the "default" github.com Host
is setup to use your mainuser
key. If you don't want to favour one account over another, I'll show you how to update existingrepos on your system to use an updated ssh configuration.
据推测,您将拥有一些与您的主要 github 身份相关联的现有存储库。因此,“默认” github.comHost
设置为使用您的mainuser
密钥。如果您不想偏爱一个帐户,我将向您展示如何更新系统上的现有存储库以使用更新的 ssh 配置。
Add your new SSH key to github
将新的 SSH 密钥添加到 github
Head over to github.com/settings/keysto add your new publickey
头部到github.com/settings/keys添加新的公共密钥
You can get the public key contents using: copy/paste it to github
您可以使用以下方法获取公钥内容:将其复制/粘贴到 github
$ cat ~/.ssh/github-otheruser.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDBVvWNQ2nO5...
Now your new user identity is all setup – below we'll show you how to use it.
现在您的新用户身份已全部设置完毕 – 下面我们将向您展示如何使用它。
Getting stuff done: cloning a repo
完成任务:克隆一个 repo
So how does this come together to work with git and github? Well because you can't have a chicken without and egg, we'll look at cloning an existing repo. This situation might apply to you if you have a new github account for your workplace and you were added to a company project.
那么它是如何与 git 和 github 一起工作的呢?好吧,因为没有鸡蛋就不能有鸡,所以我们将考虑克隆现有的存储库。如果您的工作场所有一个新的 github 帐户并且您被添加到公司项目中,则这种情况可能适用于您。
Let's say github.com/someorg/somerepo
already exists and you were added to it – cloning is as easy as
假设github.com/someorg/somerepo
已经存在并且您被添加到其中 – 克隆就像
$ git clone github.com-otheruser:someorg/somerepo.git
That boldedportion mustmatch the Host
name we setup in your ~/.ssh/config
file. That correctly connects git to the corresponding IdentityFile
and properly authenticates you with github
该粗体部分必须与Host
我们在您的~/.ssh/config
文件中设置的名称相匹配。这将 git 正确连接到相应的IdentityFile
并正确地使用 github 对您进行身份验证
Getting stuff done: creating a new repo
完成任务:创建一个新的仓库
Well because you can't have a chicken without and egg, we'll look at publishing a new repo on your secondary account. This situation applies to users that are create newcontent using their secondary github account.
好吧,因为您不能没有鸡蛋和鸡蛋,所以我们将考虑在您的辅助帐户上发布一个新的存储库。这种情况适用于使用其辅助 github 帐户创建新内容的用户。
Let's assume you've already done a little work locally and you're now ready to push to github. You can follow along with me if you'd like
假设您已经在本地完成了一些工作,现在准备推送到 github。如果你愿意,你可以跟我一起
$ cd ~
$ mkdir somerepo
$ cd somerepo
$ git init
Now configure this repo to use your identity
现在配置此存储库以使用您的身份
$ git config user.name "Mister Manager"
$ git config user.email "[email protected]"
Now make your first commit
现在做你的第一次提交
$ echo "hello world" > readme
$ git add .
$ git commit -m "first commit"
Check the commit to see your new identity was used using git log
检查提交以查看使用git log使用的新身份
$ git log --pretty="%H %an <%ae>"
f397a7cfbf55d44ffdf87aa24974f0a5001e1921 Mister Manager <[email protected]>
Alright, time to push to github! Since github doesn't know about our new repo yet, first go to github.com/newand create your new repo – name it somerepo
好了,是时候推送到github了!由于 github 还不知道我们的新仓库,首先去github.com/new创建你的新仓库——命名为somerepo
Now, to configure your repo to "talk" to github using the correct identity/credentials, we have add a remote. Assuming your github username for your new account is someuser
...
现在,要将您的存储库配置为使用正确的身份/凭证与 github 进行“对话”,我们添加了一个遥控器。假设您的新帐户的 github 用户名是someuser
...
$ git remote add origin github.com-otheruser:someuser/somerepo.git
That boldedportion is absolutely critical and it mustmatch the Host
that we defined in your ~/.ssh/config
file
这加粗部分绝对是至关重要的,它必须符合Host
我们在定义~/.ssh/config
文件
Lastly, push the repo
最后,推送 repo
$ git push origin master
Update an existing repo to use a new SSH configuration
更新现有存储库以使用新的 SSH 配置
Say you already have some repo cloned, but now you want to use a new SSH configuration. In the example above, we kept your existing repos in tact by assigning your previous id_rsa
/id_rsa.pub
key pair to Host github.com
in your SSH config file. There's nothing wrong with this, but I have at least 5 github configurations now and I don't like thinking of one of them as the "default" configuration – I'd rather be explicit about each one.
假设您已经克隆了一些存储库,但现在您想使用新的 SSH 配置。在上面的示例中,我们通过在您的 SSH 配置文件中分配您之前的id_rsa
/id_rsa.pub
密钥对来保持您现有的存储库完好无损Host github.com
。这没有任何问题,但我现在至少有 5 个 github 配置,我不喜欢将其中之一视为“默认”配置——我宁愿对每个配置都明确。
Before we had this
在我们有这个之前
Host github.com
User git
IdentityFile ~/.ssh/github-mainuser
Host github.com-otheruser
HostName github.com
User git
IdentityFile ~/.ssh/github-otheruser
So we will now update that to this (changes in bold)
所以我们现在将更新到这个(粗体更改)
Host github.com-mainuser
HostName github.com
User git
IdentityFile ~/.ssh/github-mainuser
Host github.com-otheruser
HostName github.com
User git
IdentityFile ~/.ssh/github-otheruser
But that means that now any existing repo with a github.com
remote will no longer work with this identity file. But don't worry, it's a simple fix.
但这意味着现在任何带有github.com
遥控器的现有存储库将不再使用此身份文件。不过别担心,这是一个简单的修复。
To update any existing repo to use your new SSH configuration, simply open the repo's git config file and update the url!
要更新任何现有存储库以使用新的 SSH 配置,只需打开存储库的 git 配置文件并更新 url!
$ cd existingrepo
$ nano .git/config
Update the remote origin field (changes in bold)
更新远程源字段(粗体更改)
[remote "origin"]
url = github.com-mainuser:someuser/existingrepo.git
fetch = +refs/heads/*:refs/remotes/origin/*
That's it. Now you can push
/pull
to your heart's content
就是这样。现在你可以push
/pull
心满意足
SSH key file permissions
SSH 密钥文件权限
If you're running into trouble with your public keys not working correctly, SSH is quite strict on the file permissionsallowed on your ~/.ssh
directory and corresponding key files
如果您遇到公钥无法正常工作的问题,SSH对您的目录和相应密钥文件所允许的文件权限非常严格~/.ssh
As a rule of thumb, any directories should be 700
and any files should be 600
- this means they are owner-read/write-only – no other group/user can read/write them
根据经验,任何目录都应该是700
,任何文件都应该是600
——这意味着它们是所有者可读/只写的——没有其他组/用户可以读/写它们
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/config
$ chmod 600 ~/.ssh/github-mainuser
$ chmod 600 ~/.ssh/github-mainuser.pub
$ chmod 600 ~/.ssh/github-otheruser
$ chmod 600 ~/.ssh/github-otheruser.pub
How I manage my SSH keys
我如何管理我的 SSH 密钥
I manage separate SSH keys for every host I connect to, such that if any one key is ever compromised, I don't have to update keys on every other place I've used that key. This is like when you get that notification from Adobe that 150 million of their users' information was stolen – now you have to cancel that credit card and update every service that depends on it – what a nuisance.
我为我连接的每台主机管理单独的 SSH 密钥,这样如果任何一个密钥被泄露,我就不必在我使用过该密钥的所有其他地方更新密钥。这就像当您从 Adobe 那里收到 1.5 亿用户信息被盗的通知时——现在您必须取消该信用卡并更新依赖于它的每项服务——多么令人讨厌。
Here's what my ~/.ssh
directory looks like: I have one .pem
key for each user, in a folder for each domain I connect to. I use .pem
keys to so I only need onefile per key.
这是我的~/.ssh
目录的样子:我.pem
为每个用户都有一个密钥,在我连接到的每个域的文件夹中。我使用.pem
密钥,所以每个密钥只需要一个文件。
$ tree ~/.ssh
/Users/naomik/.ssh
├── config
├── github.com
│?? ├── naomik.pem
│?? ├── someusername.pem
├── known_hosts
├── naomi.makes.software
│?? ├── naomi.pem
├── somedomain.com
│?? ├── someuser.pem
└── someotherdomain.org
?? └── someuser.pem
And here's my corresponding /.ssh/config
file – obviously the github stuff is relevant to answering this question about github, but this answer aims to equip you with the knowledge to manage your ssh identities on any number of services/machines.
这是我的相应/.ssh/config
文件——显然 github 的内容与回答有关 github 的这个问题有关,但这个答案旨在让您掌握在任意数量的服务/机器上管理您的 ssh 身份的知识。
Host github.com-naomik
HostName github.com
User git
IdentityFile ~/.ssh/github.com/naomik.pem
Host github.com-someuser
HostName github.com
User git
IdentityFile ~/.ssh/github.com/someusername.pem
Host naomi.makes.software
User naomi
IdentityFile ~/.ssh/naomi.makes.software/naomi.pem
Host somedomain.com
HostName 162.10.20.30
User someuser
IdentityFile ~/.ssh/somedomain.com/someuser.pem
Host someotherdomain.org
User someuser
IdentityFile ~/.ssh/someotherdomain.org/someuser.pem
Getting your SSH public key from a PEM key
从 PEM 密钥获取 SSH 公钥
Above you noticed that I only have onefile for each key. When I need to provide a public key, I simply generateit as needed.
上面你注意到我每个键只有一个文件。当我需要提供公钥时,我只需根据需要生成它。
So when github asks for your ssh public key, run this command to output the public key to stdout – copy/paste where needed
因此,当 github 要求您提供 ssh 公钥时,请运行此命令将公钥输出到 stdout – 在需要的地方复制/粘贴
$ ssh-keygen -y -f someuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAA...
Note, this is also the same process I use for adding my key to any remote machine. The ssh-rsa AAAA...
value is copied to the remote's ~/.ssh/authorized_keys
file
请注意,这也是我用于将密钥添加到任何远程机器的相同过程。该ssh-rsa AAAA...
值被复制到远程~/.ssh/authorized_keys
文件
Converting your id_rsa
/id_rsa.pub
key pairs to PEM format
将您的id_rsa
/id_rsa.pub
密钥对转换为 PEM 格式
So you want to tame you key files and cut down on some file system cruft? Converting your key pair to a singlePEM is easy
所以你想驯服你的关键文件并减少一些文件系统的碎片吗?将您的密钥对转换为单个PEM 很容易
$ cd ~/.ssh
$ openssl rsa -in id_rsa -outform pem > id_rsa.pem
Or, following along with our examples above, we renamed id_rsa -> github-mainuser
and id_rsa.pub -> github-mainuser.pub
– so
或者,随着我们的例子上面,下面我们改名id_rsa -> github-mainuser
和id_rsa.pub -> github-mainuser.pub
-所以
$ cd ~/.ssh
$ openssl rsa -in github-mainuser -outform pem > github-mainuser.pem
Now just to make sure that we've converted this correct, you will want to verify that the generatedpublic key matches your old public key
现在只是为了确保我们已经正确地转换了这个,您需要验证生成的公钥是否与您的旧公钥匹配
# display the public key
$ cat github-mainuser.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAA ... R++Nu+wDj7tCQ==
# generate public key from your new PEM
$ ssh-keygen -y -f someuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAA ... R++Nu+wDj7tCQ==
Now that you have your github-mainuser.pem
file, you can safely delete your old github-mainuser
and github-mainuser.pub
files – only the PEM file is necessary; just generate the public key whenever you need it ^_^
现在,你有你的github-mainuser.pem
文件,你可以安全地删除旧的github-mainuser
和github-mainuser.pub
文件-只有PEM文件是必要的; 只需在需要时生成公钥^_^
Creating PEM keys from scratch
从头开始创建 PEM 密钥
You don't need to create the private/public key pair and thenconvert to a single PEM key. You can create the PEM key directly.
您不需要创建私钥/公钥对,然后转换为单个 PEM 密钥。您可以直接创建 PEM 密钥。
Let's create a newuser.pem
让我们创建一个 newuser.pem
$ openssl genrsa -out ~/.ssh/newuser.pem 4096
Getting the SSH public key is the same
获取SSH公钥是一样的
$ ssh-keygen -y -f ~/.ssh/newuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACA ... FUNZvoKPRQ==
回答by mishaba
By creating different host aliases to github.com in your ~/.ssh/config, and giving each host alias its own ssh key, you can easily use multiple github accounts without confusion. That's because github.com distinguishes not by user, which is always just git, but by the ssh key you used to connect. Just configure your remote origins using your own host aliases.”
通过在 ~/.ssh/config 中为 github.com 创建不同的主机别名,并为每个主机别名提供自己的 ssh 密钥,您可以轻松地使用多个 github 帐户而不会混淆。那是因为 github.com 不是通过用户来区分的,用户总是只是 git,而是通过你用来连接的 ssh 密钥来区分。只需使用您自己的主机别名配置您的远程源。”
The above summary is courtesy of comments on the blog post below.
以上摘要是对下面博客文章的评论的礼貌。
I've found this explanation the clearest. And it works for me, at least as of April 2012.
我发现这个解释最清楚。它对我有用,至少在 2012 年 4 月是这样。
http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/
http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/
回答by Matthew Skelton
The details at http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/linked to by mishaba work very well for me.
由 mishaba 链接的http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/ 上的详细信息对我来说非常有效。
From that page:
从该页面:
$ touch ~/.ssh/config
Then edit that file to be something like this (one entry per account):
然后将该文件编辑为如下所示(每个帐户一个条目):
#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
回答by Pranav V R
- Go to ~/.ssh
- Create a file named config(have no extension )
Open config file & add below codes. (change according to your account)
Account 1
# account_1 Host gitlab.com-account_1 HostName gitlab.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_account_1
Account 2
# Account2 Host gitlab.com-Account2 HostName gitlab.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_Account2
Account 3
# Account_3 Host github.com-Account3 HostName github.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_Account_3
Add remote url as follows
Account 1
git remote add origin [email protected]_1:group_name/repo_name.git
Account 2
git remote add origin [email protected]:group_name/repo_name.git
Account 3
git remote add origin github.com-Account3:github_username/repo_name.git
- 转到 ~/.ssh
- 创建一个名为 config 的文件(没有扩展名)
打开配置文件并添加以下代码。(根据您的帐户更改)
帐号 1
# account_1 Host gitlab.com-account_1 HostName gitlab.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_account_1
帐号 2
# Account2 Host gitlab.com-Account2 HostName gitlab.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_Account2
帐号 3
# Account_3 Host github.com-Account3 HostName github.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_Account_3
添加远程url如下
帐号 1
git remote add origin [email protected]_1:group_name/repo_name.git
帐号 2
git remote add origin [email protected]:group_name/repo_name.git
帐号 3
git remote add origin github.com-Account3:github_username/repo_name.git
Make sure that IdentityFile names are same as you created during ssh key generation.
确保 IdentityFile 名称与您在 ssh 密钥生成期间创建的名称相同。
回答by David H
I use shell scripts to switch me to whatever account I want to be "active". Essentially you start from a fresh start, get one account configured properly and working, then move the these files to a name with the proper prefix. From then on you can use the command "github", or "gitxyz" to switch:
我使用 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
I've had great luck with this. I also created a run script in Xcode (for you Mac users) so it would not build my project unless I had the proper setting (since its using git):
我在这方面很幸运。我还在 Xcode 中创建了一个运行脚本(适用于 Mac 用户),因此除非我有正确的设置(因为它使用的是 git),否则它不会构建我的项目:
Run Script placed after Dependencies (using /bin/ksh as the shell):
运行放置在依赖项之后的脚本(使用 /bin/ksh 作为 shell):
if [ "$(git config --global --get user.email)" != "dhoerl@<company>.com" ]
then
exit 1
fi
EDIT: added tests for new files existence and copying old files to /tmp to address comment by @naomik below.
编辑:添加了新文件存在的测试,并将旧文件复制到 /tmp 以解决下面@naomik 的评论。
回答by Lukas
This answer is for beginners (none-git gurus). I recently had this problem and maybe its just me but most of the answers seemed to require rather advance understanding of git. After reading several stack overflow answers including this thread, here are the steps I needed to take in order to easily switch between GitHub accounts (e.g. assume two GitHub accounts, github.com/personaland gitHub.com/work):
这个答案适用于初学者(非 git 大师)。我最近遇到了这个问题,也许只是我自己,但大多数答案似乎都需要对 git 有相当深入的了解。在阅读了包括此线程在内的几个堆栈溢出答案后,以下是我需要采取的步骤,以便在 GitHub 帐户之间轻松切换(例如,假设有两个 GitHub 帐户,github.com/personal和gitHub.com/work):
- Check for existing ssh keys:Open Terminal and run this command to see/list existing ssh keys
ls -al ~/.ssh
files with extension.pub
are your ssh keys so you should have two for thepersonal
andwork
accounts. If there is only one or none, its time to generate other wise skip this.
- Generating ssh key: login to github (either the personal or work acc.), navigate to Settingsand copy the associated email.
now go back to Terminal and runssh-keygen -t rsa -C "the copied email"
, you'll see:
Generating public/private rsa key pair.
Enter file in which to save the key (/.../.ssh/id_rsa):
id_rsais the default name for the soon to be generated ssh key so copy the path and rename the default, e.g./.../.ssh/id_rsa_work
if generating for work account. provide a password or just enter to ignore and, you'll read something like The key's randomart image is:and the image. done.
Repeat this step once more for your second github account. Make sure you use the right email address and a different ssh key name (e.g. id_rsa_personal) to avoid overwriting.
At this stage, you should see two ssh keys when runningls -al ~/.ssh
again. - Associate ssh key with gitHub account:Next step is to copy one of the ssh keys, run this but replacing your own ssh key name:
pbcopy < ~/.ssh/id_rsa_work.pub
, replaceid_rsa_work.pub
with what you called yours.
Now that our ssh key is copied to clipboard, go back to github account [Make sure you're logged in to work account if the ssh key you copied isid_rsa_work
] and navigate to
Settings - SSH and GPG Keysand click on New SSH keybutton (not New GPG keybtw :D)
give some title for this key, paste the key and click on Add SSH key. You've now either successfully added the ssh key or noticed it has been there all along which is fine (or you got an error because you selected New GPG keyinstead of New SSH key:D). - Associate ssh key with gitHub account: Repeat the above step for your second account.
Edit the global git configuration:Last step is to make sure the global configuration file is aware of all github accounts (so to say).
Rungit config --global --edit
to edit this global file, if this opens vim and you don't know how to use it, pressi
to enter Insert mode, edit the file as below, and press esc followed by:wq
to exit insert mode:[inside this square brackets give a name to the followed acc.] name = github_username email = github_emailaddress [any other name] name = github_username email = github_email [credential] helper = osxkeychain useHttpPath = true
- 检查现有的 ssh 密钥:打开终端并运行此命令以查看/列出
ls -al ~/.ssh
扩展名为.pub
您的 ssh 密钥的现有 ssh 密钥文件,因此您应该有两个用于personal
和work
帐户。如果只有一个或没有,则是生成其他明智的时候跳过这个。
-生成 ssh 密钥:登录 github(个人或工作帐户),导航到“设置”并复制关联的电子邮件。
现在返回终端并运行ssh-keygen -t rsa -C "the copied email"
,您将看到:
Generating public/private rsa key pair。
输入文件中保存密钥(/.../.ssh/id_rsa):
id_rsa是为即将生成SSH密钥的缺省名称,以便复制的路径和重命名默认,如/.../.ssh/id_rsa_work
如果为工作帐户生成。提供密码或直接输入忽略,你会读到类似The key's randomart image is:and the image. 完毕。
为您的第二个 github 帐户再次重复此步骤。确保使用正确的电子邮件地址和不同的 ssh 密钥名称(例如 id_rsa_personal)以避免覆盖。
在此阶段,您ls -al ~/.ssh
再次运行时应该会看到两个 ssh 密钥。 - 将 ssh 密钥与 gitHub 帐户关联:下一步是复制其中一个 ssh 密钥,运行此命令但替换您自己的 ssh 密钥名称:
pbcopy < ~/.ssh/id_rsa_work.pub
,替换id_rsa_work.pub
为您所说的。
现在我们的 ssh 密钥已复制到剪贴板,返回 github 帐户 [如果您复制的 ssh 密钥是 ,请确保您已登录到工作帐户id_rsa_work
] 并导航到
设置 - SSH 和 GPG 密钥,然后单击新建 SSH 密钥按钮(不是新的 GPG 密钥顺便说一句:D)
给这个密钥一些标题,粘贴密钥并点击添加 SSH 密钥。您现在要么成功添加了 ssh 密钥,要么注意到它一直存在,这很好(或者您收到错误,因为您选择了新 GPG 密钥而不是新的 SSH 密钥:D)。 - 将 ssh 密钥与 gitHub 帐户关联:为您的第二个帐户重复上述步骤。
编辑全局 git 配置:最后一步是确保全局配置文件知道所有 github 帐户(可以这么说)。
运行git config --global --edit
编辑这个全局文件,如果打开vim不知道怎么用,按i
进入插入模式,编辑文件如下,按esc:wq
退出插入模式:[inside this square brackets give a name to the followed acc.] name = github_username email = github_emailaddress [any other name] name = github_username email = github_email [credential] helper = osxkeychain useHttpPath = true
Done!, now when trying to push or pull from a repo, you'll be asked which GitHub account should be linked with this repo and its asked only once, the local configuration will remember this link and not the global configuration so you can work on different repos that are linked with different accounts without having to edit global configuration each time.
大功告成!,现在当试图从一个仓库推送或拉取时,你会被问到哪个 GitHub 账户应该与这个仓库链接并且它只被询问一次,本地配置会记住这个链接而不是全局配置,所以你可以工作在与不同帐户链接的不同存储库上,而无需每次编辑全局配置。
回答by Raja Shakir
Simpler and Easy fix to avoid confusions..
更简单和容易的修复,以避免混淆..
For Windows users to use multiple or different git accounts for different projects.
供 Windows 用户为不同的项目使用多个或不同的 git 帐户。
Following steps: Go Control Panel and Search for Credential Manager. Then Go to Credential Manager -> Windows Credentials
以下步骤:转到控制面板并搜索凭据管理器。然后转到凭据管理器-> Windows 凭据
Now remove the git:https//github.comnode under Generic Credentials Heading
现在删除Generic Credentials Heading 下的git:https//github.com节点
This will remove the current credentials. Now you can add any project through git pull it will ask for username and password.
这将删除当前凭据。现在你可以通过 git pull 添加任何项目,它会要求输入用户名和密码。
When you face any issue with other account do the same process.
当您遇到其他帐户的任何问题时,请执行相同的过程。
Thanks
谢谢
回答by Craig - MSFT
just figured this out for Windows, using credentials for each repo:
刚刚为 Windows 解决了这个问题,使用每个 repo 的凭据:
cd c:\User1\SomeRepo
git config --local credential.https://github.com.user1 user1
git config --local credential.useHttpPath true
git config --local credential.helper manager
git remote set-url origin https://[email protected]/USERNAME/PROJECTNAME.git
The format of credential.https://github.com. tells the credential helper the URL for the credential. The 'useHttpPath' tells the credential manager to use the path for the credential. If useHttpPath is omitted then the credential manager will store one credential for https://github.com. If it is included then the credential manager will store multiple credentials, which is what I really wanted.
凭证的格式。https://github.com。告诉凭证助手凭证的 URL。'useHttpPath' 告诉凭证管理器使用凭证的路径。如果省略 useHttpPath,则凭证管理器将为https://github.com存储一个凭证。如果包含它,那么凭证管理器将存储多个凭证,这正是我真正想要的。