GitHub 的Public Repositories 免费,Private Repositories 收费,而Bitbucket 正相反,所以自然而然,愿意公开项目放到GitHub 上面,而不想公开的项目放到Bitbucket。
配置方法
- 注册GitHub 和Bitbucket 账号。
- 下载Git Bash。
生成SSH key,以GitHub 为例:
- 假如你有两个账号:countA@gamil.com (常用) 和 countB@gmail.com,都分别注册了GitHub 和Bitbucket。
在C:\Users\you\.ssh 目录右键打开安装好的Git Bash,输入
ssh-keygen -t rsa -b 4096 -C "countA@gamil.com"
回车,会弹出Enter a file in which to save the key (/Users/you/.ssh/id_rsa):
直接回车,再输入两遍密码,在C:\Users\you\.ssh 目录会生成两个文件:id_rsa 和id_rsa.pub。
再次在Git Bash 中输入
ssh-keygen -t rsa -b 4096 -C "countB@gamil.com"
回车,弹出Enter a file in which to save the key (/Users/you/.ssh/id_rsa):
这回输入countB,回车,输入两遍密码,在C:\Users\you\.ssh 目录会生成两个文件:countB 和countB.pub。
把生成的和id_rsa.pub 和acountB.pub 的内容添加到GitHub 上。
在C:\Users\you\.ssh 目录创建文件config,内容如下:
1234567891011121314151617181920Host acountAgitHostName github.comUser gitPreferredAuthentications publickeyIdentityFile ~/.ssh/id_rsaHost acountBgitHostName github.comUser gitPreferredAuthentications publickeyIdentityFile ~/.ssh/acountBHost acountAbitHostName bitbucket.orgUser gitPreferredAuthentications publickeyIdentityFile ~/.ssh/id_rsaHost acountBbitHostName bitbucket.orgUser gitPreferredAuthentications publickeyIdentityFile ~/.ssh/acountB在C:\Users\you 目录创建文件.profile,内容如下:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748# Note: ~/.ssh/environment should not be used, as it# already has a different purpose in SSH.env=~/.ssh/agent.env# Note: Don't bother checking SSH_AGENT_PID. It's not used# by SSH itself, and it might even be incorrect# (for example, when using agent-forwarding over SSH).agent_is_running() {if [ "$SSH_AUTH_SOCK" ]; then# ssh-add returns:# 0 = agent running, has keys# 1 = agent running, no keys# 2 = agent not runningssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]elsefalsefi}agent_has_keys() {ssh-add -l >/dev/null 2>&1}agent_load_env() {. "$env" >/dev/null}agent_start() {(umask 077; ssh-agent >"$env"). "$env" >/dev/null}if ! agent_is_running; thenagent_load_envfi# if your keys are not stored in ~/.ssh/id_rsa or ~/.ssh/id_dsa, you'll need# to paste the proper path after ssh-addif ! agent_is_running; thenagent_startssh-addelif ! agent_has_keys; thenssh-addfiunset env关闭Git Bash,再重新打开,会弹出
Enter passphrase for /c/Users/you/.ssh/id_rsa:
输入密码后回车,会弹出
Identity added: /c/Users/you/.ssh/id_rsa (/c/Users/you/.ssh/id_rsa)
在Git Bash 中输入
ssh -T acountAgit
会弹出Hi yourGitHubacountA! You’ve successfully authenticated, but GitHub does not provide shell access.
使用第二个账号
在C:\Users\you\.ssh 中右键打开Git Bash 输入ssh-add acountB
会弹出Enter passphrase for /c/Users/you/.ssh/
接着输入密码后回车,会弹出
Identity added: /c/Users/you/.ssh/id_rsa (/c/Users/you/.ssh/id_rsa)接着输入
ssh -T acountBgit
会弹出Hi yourGitHubAcountB! You’ve successfully authenticated, but GitHub does not provide shell access.
至此,配置完成!
一些解释
- config 文件是配置多网站或多账号的基础
- .profile 文件是使每次启动Git Bash 时自动登陆ssh-agent。
- 使用三个以上的网站,比如还有使用国内的gitcafe 等,使用三个以上的账号,配置方法都是类似的,在config 中增加相应配置就好了。
- 以github 的clone 为例,假如你的acountA账号create 或clone 了一个repository,叫yourProject,想clone 到本地,本地命名为yourProj
git clone acountAgit:yourGitHubAcountA/yourProject.git yourProj