天天看點

多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

看他們是否是成功的。