多Git账户的Git使用

2015/12/30
背景:git 可能有用github.com,gitlab.com(自家搭建的git等),甚至于git.oschina.net等等。那么多Git账户的使用就发挥作用了

多Git账户的Git使用
Git

本博客假设你已经存在一个git账户。
旧账号:accounta(your-email-addressa),新添加的账号:accountb(your-email-addressb).
<h2>Step 1:</h2>


Create a New SSH Key for account 2;
为账户b新建一个SSH Key
We need to generate a unique SSH key for our second Git account.
给账户b创建一个唯一SSH key

# 切换到C:\Users\Administrator\.ssh
$ cd ~/.ssh  
#新建SSH key:
ssh-keygen -t rsa -C "accountb"

Be careful that you don't over-write your existing key for your personal account. Instead, when prompted, save the file as id_rsa_COMPANY.
注意:不要将你生成的SSH key覆盖到之前的账户SSH key. --,当出现提示的时候,保存文件为id_rsa_
In my case, I've saved the file to~/.ssh/id_rsa_accountb.
在我的示例中,我将文件保存在.ssh文件夹下,名为id_rsa_accountb.

# 设置名称为id_rsa_accountb
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_accountb

<h2>Step 2 </h2>


Attach the New Key
依赖新的SSH key
Next, login to your second GitHub account, browse to "Account Overview," and attach the new key, within the "SSH Public Keys" section.
登录你的账户B,在SSH Public Keys 部分 附加新的SSH key公钥
To retrieve the value of the key that you just created, return to the Terminal, and type: vim id_rsa_accountb.pub. Copy the entire string that is displayed, and paste this into the Git textarea. Feel free to give it any title you wish.
检索您刚刚创建的值的键,返回到终端,打印id_rsa_accountb.pub。复制整个字符串,粘贴到Git 文本。随便给它一个你想要的标题。

vim id_rsa_accountb.pub

Next, because we saved our key with a unique name, we need to tell SSH about it. Within the Terminal, type: ssh-add id_rsa_accountb. If successful, you'll see a response of "Identity Added."
下一步, 因为我们保存了一个独特名字的Key,所以我们需要告知SSH关于这个key的信息。在终端里面打印:ssh-add id_rsa_accountb,如果成功,你将看到一个"Identity Added"(身份添加)的响应

ssh-add id_rsa_accountb.pub

<h2>Step 3</h2>


Create a Config File
创建一个名为config的文件
We've done the bulk of the workload; but now we need a way to specify when we wish to push to our accountA, and when we should instead push to our accountB. To do so, let's create a config file.
我们已经做了大量的工作,但现在我们需要一种方法来指定当我们希望推动我们的账户A,当我们应该推到我们账户B。为此,让我们创建一个配置文件。

touch config
vim config

If you're not comfortable with Vim, feel free to open it within any editor of your choice. Paste in the following snippet.
如果你不习惯Vim,您可以选择使用编辑器将其打开。粘贴以下代码片段

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

This is the default setup for pushing to our Git accountA. Notice that we're able to attach an identity file to the host. Let's add another one for the accountB. Directly below the code above, add:
这是一个默认推送配置,推送到账户A,注意,我们能够将一个身份文件附加到主机。让我们添加另一个身份账户B。在config文件下添加以下代码:

Host GitB
HostName github.com
User gitaccountB
IdentityFile ~/.ssh/id_rsa_accountb

以上github.com可以使github.com,gitlab.com等你账户对应git所在的网址
This time, rather than setting the host to github.com, we've named it as github-COMPANY. The difference is that we're now attaching the new identity file that we created previously: id_rsa_COMPANY. Save the page and exit!
这时候,我们给Host命名为GitB 这一次,而不是设置Host名为GitA。不同的是,我们现在和我们先前创建的新身份文件:id_rsa_accountb。保存该页面,然后退出!
<h2>Step 4</h2>


Try it Out
尝试使用它吧。
It's time to see if our efforts were successful. Create a test directory, initialize git, and create your first commit.
是时候看看我们的努力是否是成功了。创建一个新的test,初始化他,并创建你的第一个提交

git init
git commit -am "first commit'

Login to your company account, create a new repository, give it a name of "Test," and then return to the Terminal and push your git repo to GitHub/GitLab.
登录你的账户,创建一个新的响应,命名为Test,获取推送地址,使用终端和这个地址推送你的文件。

git remote add origin git@github-B:XX/testing.git
git push origin master

<h2>注意:</h2>


测试:
$ ssh -T GitA
$ ssh -T GitB
看他们是否是成功的。

上一篇:可汗学院”(Khan Academy)提出的考验智商的谜题


下一篇:Markdown初体验