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

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

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

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

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


您所需要做的就是使用多个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在评论中告诉我这件事。


我使用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中为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/


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

我发现这个宝石非常有用:开关

https://github.com/agush22/sshwitch http://rubygems.org/gems/sshwitch

它有助于切换ssh密钥。记得先备份所有东西!

另外,为了确保提交有正确的电子邮件地址与它们相关联,我确保~/。Gitconfig文件有正确的电子邮件地址。


使用HTTPS:

将远程url更改为https:

git remote set-url origin https://USERNAME@github.com/USERNAME/PROJECTNAME.git

你可以开始了:

git push

为了确保提交显示为USERNAME执行,可以设置user.name和user。为这个项目发送电子邮件:

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

除了为多个帐户创建多个SSH密钥,您还可以考虑使用相同的帐户电子邮件在每个项目上添加合作者,并永久存储密码。

#this store the password permanently
$ git config --global credential.helper wincred

我用不同的电子邮件设置了多个帐户,然后把相同的用户和电子邮件放在每个帐户作为合作者之一。通过这种方式,我可以访问所有的帐户,无需添加SSH密钥,或切换到另一个用户名,和电子邮件进行身份验证。


塑形

要在单独的github/bitbucket/任何帐户下管理git回购,你只需要生成一个新的SSH密钥。

但在我们开始用你的第二个身份推/拉回购之前,我们必须让你进入状态-让我们假设你的系统是用典型的id_rsa和id_rsa设置的。Pub密钥对。现在你的树~/。SSH是这样的

$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── id_rsa
└── id_rsa.pub

首先,命名该密钥对——添加一个描述性的名称将帮助您记住哪个密钥用于哪个用户/远程

# 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

接下来,让我们生成一个新的密钥对——这里我将新密钥命名为github-otheruser

$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/github-otheruser

现在,当我们看树~/。SSH我们看到了

$ tree ~/.ssh
/Users/you/.ssh
├── known_hosts
├── github-mainuser
├── github-mainuser.pub
├── github-otheruser
└── github-otheruser.pub    

接下来,我们需要设置一个~/。Ssh /config文件将定义我们的关键配置。我们将使用适当的所有者只读/只写权限创建它

$ (umask 077; touch ~/.ssh/config)

用你最喜欢的编辑器打开它,并添加以下内容

Host github.com
  User git
  IdentityFile ~/.ssh/github-mainuser

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

假设,你会有一些现有的回购与你的主要github身份相关联。因此,“默认”github.com主机设置为使用mainuser密钥。如果您不想偏爱某个帐户而不是另一个帐户,我将向您展示如何更新系统上现有的回购以使用更新后的ssh配置。


将新的SSH密钥添加到github

转到github.com/settings/keys,添加新的公钥

你可以使用:复制/粘贴到github来获取公钥内容

$ cat ~/.ssh/github-otheruser.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDBVvWNQ2nO5...

现在您的新用户身份已经全部设置完毕-下面我们将向您展示如何使用它。


完成工作:克隆回购

那么这是如何一起工作与git和github?因为没有蛋就没有鸡,所以我们将研究克隆现有的回购。如果你的工作场所有一个新的github帐户,并且你被添加到一个公司项目中,这种情况可能适用于你。

假设github.com/someorg/somerepo已经存在,你被添加到它-克隆是一样容易

$ git clone github.com-otheruser:someorg/somerepo.git

粗体部分必须与我们在~/中设置的主机名匹配。ssh / config文件。正确地将git连接到相应的IdentityFile,并使用github正确地验证您的身份


完成工作:创建一个新的回购

好吧,因为你不能没有鸡和蛋,我们会考虑在你的二级账户上发布一个新的回购。这种情况适用于使用其次要github帐户创建新内容的用户。

让我们假设你已经在本地做了一些工作,现在准备推送到github。如果你愿意,你可以跟我一起走

$ cd ~
$ mkdir somerepo
$ cd somerepo
$ git init

现在配置这个回购使用您的身份

$ git config user.name "Mister Manager"
$ git config user.email "someuser@some.org"

现在做出你的第一个承诺

$ echo "hello world" > readme
$ git add .
$ git commit -m "first commit"

检查提交,看看你的新身份是使用git日志

$ git log --pretty="%H %an <%ae>"
f397a7cfbf55d44ffdf87aa24974f0a5001e1921 Mister Manager <someuser@some.org>

好了,该推到github了!因为github还不知道我们的新回购,首先去github.com/new并创建您的新回购-命名为somerepo

现在,为了配置你的repo使用正确的身份/凭据与github“对话”,我们已经添加了一个遥控器。假设你的github用户名为你的新帐户是someuser…

$ git remote add origin github.com-otheruser:someuser/somerepo.git

粗体部分非常重要,它必须与我们在~/中定义的Host相匹配。ssh / config文件

最后,推动回购

$ git push origin master

更新现有的repo以使用新的SSH配置

假设您已经克隆了一些回购,但现在想使用新的SSH配置。在上面的例子中,我们通过分配你之前的id_rsa/id_rsa来保持你现有的回购。pub密钥对到主机github.com在您的SSH配置文件。这没有什么问题,但我现在至少有5个github配置,我不喜欢把它们中的一个视为“默认”配置-我宁愿明确地说明每一个。

在这之前

Host github.com
  User git
  IdentityFile ~/.ssh/github-mainuser

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

现在我们将它更新为this(粗体部分的改动)

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

但现在任何现有的回购与github.com远程将无法与此身份文件工作。但别担心,这是一个简单的解决办法。

要更新任何现有的repo以使用新的SSH配置,请使用set-url -更新repo的远程原点字段

$ cd existingrepo
$ git remote set-url origin github.com-mainuser:someuser/existingrepo.git

就是这样。现在你可以尽情地推/拉了


SSH密钥文件权限

如果您遇到了公钥不能正常工作的问题,SSH对~/上允许的文件权限非常严格。SSH目录和相应的密钥文件

根据经验,任何目录都应该是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

如何管理SSH密钥

我为我连接到的每个主机管理单独的SSH密钥,这样,如果任何一个密钥被泄露,我不必在我使用该密钥的每个其他地方更新密钥。这就像你收到Adobe的通知,说他们有1.5亿用户的信息被盗了——现在你必须取消那张信用卡,并更新所有依赖于它的服务——多么讨厌。

这是我的~/。ssh目录如下:每个用户都有一个.pem密钥,在连接到的每个域的文件夹中。我使用.pem键,所以每个键只需要一个文件。

$ tree ~/.ssh
/Users/myuser/.ssh
├── another.site
│   ├── myuser.pem
├── config
├── github.com
│   ├── myuser.pem
│   ├── someusername.pem
├── known_hosts
├── somedomain.com
│   ├── someuser.pem
└── someotherdomain.org
     └── root.pem

这是我对应的/。Ssh /配置文件——显然,github的东西与回答关于github的这个问题有关,但这个答案旨在让你具备在任何数量的服务/机器上管理Ssh身份的知识。

Host another.site
  User muyuser
  IdentityFile ~/.ssh/another.site/muyuser.pem

Host github.com-myuser
  HostName github.com
  User git
  IdentityFile ~/.ssh/github.com/myuser.pem

Host github.com-someuser
  HostName github.com
  User git
  IdentityFile ~/.ssh/github.com/someusername.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/root.pem

从PEM密钥中获取SSH公钥

上面您注意到,每个键只有一个文件。当我需要提供公钥时,我只需根据需要生成它。

因此,当github要求您的ssh公钥时,运行此命令将公钥输出到标准输出-在需要的地方复制/粘贴

$ ssh-keygen -y -f someuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAA...

注意,这也是我用于向任何远程机器添加密钥的相同过程。ssh-rsa AAAA…值复制到远程的~/。ssh / authorized_keys文件


转换您的id_rsa/id_rsa。pub密钥对为PEM格式

所以你想驯服你的密钥文件,减少一些文件系统的麻烦?将密钥对转换为单个PEM非常简单

$ cd ~/.ssh
$ openssl rsa -in id_rsa -outform pem > id_rsa.pem

或者,按照上面的例子,我们重命名id_rsa -> github-mainuser和id_rsa。Pub -> github-mainuser。酒吧- so

$ cd ~/.ssh
$ openssl rsa -in github-mainuser -outform pem > github-mainuser.pem

现在,为了确保转换正确,您需要验证生成的公钥是否与旧的公钥匹配

# 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==

现在你有了你的github-mainuser。Pem文件,您可以安全地删除旧的github-mainuser和github-mainuser。pub文件-只需要PEM文件;只要在你需要的时候生成公钥^_^


从头开始创建PEM密钥

您不需要创建私钥/公钥对,然后再转换为单个PEM密钥。可以直接创建PEM密钥。

让我们创建一个newuser.pem

$ openssl genrsa -out ~/.ssh/newuser.pem 4096

SSH公钥的获取方法相同

$ ssh-keygen -y -f ~/.ssh/newuser.pem
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACA ... FUNZvoKPRQ==

这个答案是给初学者的(非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 the personal and work 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 Settings and copy the associated email.now go back to Terminal and run ssh-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_rsa is 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 running ls -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, replace id_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 is id_rsa_work] and navigate toSettings - SSH and GPG Keys and click on New SSH key button (not New GPG key btw :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 key instead 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). Run git config --global --edit to edit this global file, if this opens vim and you don't know how to use it, press i 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

完成了!现在,当尝试从一个回购中推或拉,你会被问到哪个GitHub账户应该与这个回购链接,它只被问到一次,本地配置将记住这个链接,而不是全局配置,所以你可以在不同的回购上工作,链接到不同的账户,而不必每次编辑全局配置。


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 git@gitlab.com-account_1:group_name/repo_name.git Account 2 git remote add origin git@gitlab.com-Account2:group_name/repo_name.git Account 3 git remote add origin github.com-Account3:github_username/repo_name.git

确保IdentityFile名称与您在ssh密钥生成过程中创建的名称相同。


更简单和容易修复,以避免混乱..

Windows用户可以为不同的项目使用多个或不同的git帐户。

以下步骤: 进入控制面板,搜索凭据管理器。 然后转到凭证管理器-> Windows凭证

现在在Generic Credentials Heading下删除git:https//github.com节点

这将删除当前凭据。现在你可以通过git添加任何项目,它会要求用户名和密码。

当你遇到任何问题与其他帐户做同样的过程。


你不需要维护两个不同的个人和工作账户。事实上,Github建议你保持一个单一的帐户,并帮助你合并两者。

如果您决定不需要维护多个帐户,请按照下面的链接进行合并。

https://help.github.com/articles/merging-multiple-user-accounts/


不像其他答案,在那里你需要遵循几个步骤来使用两个不同的github帐户从同一台机器,对我来说,它在两个步骤。

你只需要:

1)在~/下为您的每个帐户生成SSH公私钥对。使用不同的名称和SSH位置

2)在“设置”中,将生成的公钥添加到对应的帐号“>> SSH和GPG密钥>> SSH新密钥”。

使用实例生成SSH公私钥对。

cd ~/.ssh
ssh-keygen -t rsa -C "email@work.com" -f "id_rsa_WORK"
ssh-keygen -t rsa -C "email@gmail.com" -f "id_rsa_PERSONAL"

作为上述命令的结果,id_rsa_WORK和id_rsa_WORK。pub文件将为您的工作帐户(ex - git.work.com)和id_rsa_PERSONAL和id_rsa_PERSONAL创建。Pub将为您的个人帐户(ex - github.com)创建。

创建之后,从每个公共(*.pub)文件复制内容,并对每个帐户执行步骤2。

PS:没有必要为~/下的每个git帐户创建一个主机条目。Ssh /配置文件,如在其他答案中提到的,如果您的两个帐户的主机名不同。


另一种更简单的方法是使用多个桌面应用程序,就像我在做什么,使用帐户A在Github桌面,而使用帐户B在Github Kraken


如果你碰巧安装了WSL,你可以有两个独立的git帐户——一个在WSL上,一个在windows上。


最简单和直接的方法(IMHO) -没有配置文件,没有太多的麻烦

只需创建另一个ssh密钥。

假设你有一个新的GitHub工作帐户,只需为它创建一个新密钥:

ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"`

您只需要运行一次上面的程序。

现在你应该有了旧的和新的,要查看它们,运行:

ls -al ~/.ssh

从现在开始,每次你想在两者之间切换时,只需运行:

ssh-add -D
ssh-add ~/.ssh/id_rsa_work_user1 #make to use this without the suffix .pub

为了切换到旧的,再次运行:

 ssh-add -D
 ssh-add ~/.ssh/<previous id_rsa>

刚刚在Windows上发现了这一点,为每次回购使用凭证:

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://USERNAME@github.com/USERNAME/PROJECTNAME.git

credential.https://github.com的格式。告诉凭据助手凭据的URL。 “useHttpPath”告诉凭据管理器使用凭据的路径。如果省略了useHttpPath,则凭据管理器将为https://github.com存储一个凭据。如果包含了它,那么凭据管理器将存储多个凭据,这正是我真正想要的。


IntelliJ Idea有内置的支持 https://www.jetbrains.com/help/idea/github.html#da8d32ae


您应该也不应该使用一些常见凭证推送到项目。在新机器上启动后,按照以下步骤正确设置和使用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 

以防你不想弄乱~/。这里提到的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


导航到要将更改推到另一个GitHub帐户的目录。 在终端/命令行中创建一个新的SSH密钥。 ssh-keygen -t rsa -C " your-email-address " 下面将显示: 生成rsa公私钥对。 输入保存密钥的文件(/home/your_username/.ssh/id_rsa):

复制并粘贴路径,后面跟着一个可识别的文件名:

/home/your_username/.ssh/id_rsa_personal

4)然后它会问你以下问题:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

5)您现在可以输入以下命令查看您在本地机器上拥有的所有SSH密钥:

ls -al ~/.ssh

您应该能够看到新的SSH密钥文件。正如你可以看到在我的一个,我有id_rsa_test和id_rsa_personal.pub。

drwx------  2 gmadmin gmadmin 4096 Nov 16 22:20 .
drwxr-xr-x 42 gmadmin gmadmin 4096 Nov 16 21:03 ..
-rw-------  1 gmadmin gmadmin 1766 Nov 16 22:20 id_rsa_personal
-rw-r--r--  1 gmadmin gmadmin  414 Nov 16 22:20 id_rsa_personal.pub
-rw-r--r--  1 gmadmin gmadmin  444 Nov  6 11:32 known_hosts

6)接下来需要复制存储在id_rsa_personal中的SSH密钥。酒吧文件。您可以在您选择的文本编辑器中打开此文件。我目前正在使用atom,所以我使用以下命令打开文件:

atom ~/.ssh/id_rsa_personal.pub

然后你会得到类似这样的结果:

ssh-rsa AAB3HKJLKC1yc2EAAAADAQABAAAAQCgU5+ELtwsKkmcoeF3hNd7d6CjW+dWut83R/DC01E/YzLc5ZFri18doOwuQoeTPpmIRVDGuQQsZshjDrTkFy8rwKWMlXl7va5olnGICcpg4qydEtsW+MELDmayW1HHsi2xHMMGHlNv

7)复制并导航到您的GitHub帐户→设置→SSH和GPG密钥 8)单击“新建SSH密钥”。复制键,给它一个标题并添加它。 9)从终端添加密钥

ssh-add ~/.ssh/id_rsa_personal
Enter passphrase for /home/your_username/.ssh/id_rsa_personal: 

10)配置用户和密码。

git config --global user.name "gitusername"
git config --global user.email "gitemail"

11)我们已经做好了承诺和推进的准备。

git init
git add .
git commit 
git push

如果您已经创建或克隆了另一个存储库,并且无法从原点或上游提取,则使用以下命令在该目录中添加ssh密钥是有效的。

这是我得到的错误:

Warning: Permanently added the RSA host key for IP address '61:fd9b::8c52:7203' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

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

我使用了下面的命令,这是有效的:

ssh-add ~ / . ssh / id_rsa_YOUR_COMPANY_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条目将显示,但使用不同的帐户。


选项0:你不想乱搞操作系统设置..你只是想为一次回购提交一个不同的github账户,使用不同的公钥。

解决方案:

create the new key: ssh-keygen -t rsa -b 4096 -f ~/.ssh/alt_rsa add the key to the keyset: ssh-add -K ~/.ssh/alt_rsa copy and add the pub key to the github account: (see github instructions) test the key with github: ssh -i ~/.ssh/alt_rsa T git@github.com clone the repo using the git protocol (not HTTP): git clone git@github:myaccount... in the cloned repo: git config core.sshCommand "ssh -i ~/.ssh/alt_rsa -F /dev/null" git config user.name [myaccount] git config user.email [myaccount email] now you should be able to git push correctly without interferring with your everyday git account


让我的私人回购工作使用SSH密钥对。这是在Windows git上测试的。

来源:https://docs.github.com/en/free-pro-team@latest github / authenticating-to-github / generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

A.生成公钥和私钥对

启动git bash 执行ssh-keygen -t ed25519 -C "your_email@example.com" 当提示您“输入要保存密钥的文件”时,按Enter接受默认值。 按enter键输入空白密码。 启动ssh代理:eval $(ssh-agent) 将私钥添加到ssh代理,并存储密码:ssh- Add ~/.ssh/id_ed25519

B.添加SSH密钥到GitHub账户

将公钥复制到剪贴板:clip < ~/.ssh/id_ed25519.pub 在GitHub中,进入“配置文件->设置-> SSH密钥-> SSH新密钥” 给出一个标题。如。“MacBook Pro上的Windows” 粘贴密钥并点击“添加SSH密钥”。

C.测试SSH连接

输入:ssh -T git@github.com 点击“是”查看任何警告信息。 它应该显示:“Hi username!…”,表示测试成功。

D.设置本地存储库以使用SSH密钥

更改邮箱和用户名:

git config user.email your_email@example.com
git config user.name github_username

更新远程链接以使用git。首先列出远程URI:

git remote -v
git remote set-url origin git@github.com:github_username/your-repo-name.git

e .测试

git remote show origin

在一台Windows机器上管理多个GitHub帐户(HTTPS)

假设你之前在你的机器上使用git并配置了git全局配置文件。要检查它,打开终端,然后:

git config --global -e

它打开你的编辑器,你可能会看到这样的东西:

[user]
    email = yourEmail@gmail.com
    name = Your_Name
...

这很好,因为你可以把你的代码推送到GitHub账户,而不用每次都输入凭证。 但如果它需要从另一个账户推送回购呢?在这种情况下,git将拒绝403 err,您必须更改全局git凭据。要在凭据管理器中存储一个回购名称,可以使用这个lat集:

git config --global credential.github.com.useHttpPath true

要检查它打开配置一次 Git配置——global -e 您将看到新的配置行

[credential]
    useHttpPath = true
...

就是它。现在,当你第一次推到任何帐户,你会看到一个弹出 Screenshot_1

输入特定的此回购帐户凭据,这将“绑定”此帐户的回购。因此,在您的机器中,您可以指定任意数量的帐户/回购。

想要更详细的解释,你可以看看我在youtube上找到的这个很酷的视频: https://youtu.be/2MGGJtTH0bQ


将github中的认证方式由SSh修改为HTTP。这样它就不会关心你是否登录了你的工作github帐户,你试图远程与你的个人github帐户(它有不同的凭据,而不是你目前登录到你的本地机器)交互。


可以有多种方法来做到这一点,但以下解决方案为我工作,非常简单。 我不是试图用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帐户。 您可以将您的更改从主分支合并到主分支。

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


在我的情况下,我有我的工作帐户在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的人有所帮助。


相反,您只需将新的电子邮件id添加到您的个人git帐户。这样您就不需要添加另一个SSH密钥。只需按以下步骤配置新邮箱即可 Git配置——全局用户。电子邮件newemail。然后你就可以复制那个回购了。


我看到这里有很多可行的变通办法。作为权宜之计,@Greg的方法似乎相对简单。但是,从长远来看,最好为所有不同的帐户设置单独的ssh密钥。这个视频简单演示了一下。以下是视频博客中提到的步骤。

步骤1 -为新帐户创建一个新的SSH密钥,并将其保存在一个单独的文件中(例如~/. SSH /id_rsa_new_account_name),而不是在原来的文件中,例如~/. SSH /id_rsa

ssh-keygen -t rsa -C "your-email-address"

步骤2 -附加新密钥

接下来,登录到你的第二个GitHub账户 浏览到Account Overview,并附加新密钥~/.ssh/id_rsa_new_account_name。在SSH Public Keys部分中。 在终端中,通过键入SSH -add ~/. SSH /id_rsa_new_account_name告诉SSH添加新的标识。如果成功,您将看到Identity Added的响应。

步骤3 -创建配置文件

touch ~/.ssh/config

并将以下内容保存到文件中

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

#New Account
Host github-new-account-name
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_new_account_name

第四步——尝试一下 现在,每当您想要使用新帐户和相关的回购时,请为相应的回购键入此选项

git remote add origin git@github-new-account-name:your-domain.com/your-repo.git

简单的方法,不需要处理SSH键盘

只需更改user.name & user。在回购的本地电子邮件。给那些你想要推到回购的人。

示例:我在gitlab有一个工作帐户,有一个项目。克隆这个项目/回购后,在终端中,我输入:

git config user.name "my-work-username"
git config user.email "my-work-email-id"

现在,假设我在Gitlab中有另一个个人项目/回购,我想将它与我的个人帐户关联起来。就像上面一样,克隆后,我输入:

git config user.name "my-personal-username"
git config user.email "my-personal-email-id"

希望这能有所帮助。如果对你有用就给它点赞!:)


使用码头工人!

这可能不适合每个人,但这是我最后为自己做的。

我所有的开发现在都在docker容器中完成。我是一个vim用户,但我知道VSCode有插件来使用docker容器。

因此,由于我有不同的docker容器为不同的项目/语言,我可以在每个内部使用不同的git配置。问题解决了。


有很多答案解释如何完成你的要求,但我想提供一个不同的角度。

我认为把工作和个人事情分开是有道理的。在所有操作系统上切换用户帐户是非常简单的,所以你可以为工作和娱乐创建单独的操作系统帐户,并在每个操作系统中登录不同的github帐户。

保持清晰的关注点分离也可以……

防止你的工作文件和个人文件混在一起 降低对错误资源执行操作的风险 可能会将安全漏洞限制在一组帐户/凭据上


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

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

2023年更新:

你可以使用Github桌面。在这里,你只需要在本地克隆存储库,并使用Github Desktop管理代码。