多账号或多网站使用Git

GitHub 的Public Repositories 免费,Private Repositories 收费,而Bitbucket 正相反,所以自然而然,愿意公开项目放到GitHub 上面,而不想公开的项目放到Bitbucket。

配置方法

(以下说明均在Windows 环境下)


  • 注册GitHubBitbucket 账号。
  • 下载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,内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Host acountAgit
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
    Host acountBgit
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/acountB
    Host acountAbit
    HostName bitbucket.org
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
    Host acountBbit
    HostName bitbucket.org
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/acountB
  • C:\Users\you 目录创建文件.profile,内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    # 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 running
    ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]
    else
    false
    fi
    }
    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; then
    agent_load_env
    fi
    # if your keys are not stored in ~/.ssh/id_rsa or ~/.ssh/id_dsa, you'll need
    # to paste the proper path after ssh-add
    if ! agent_is_running; then
    agent_start
    ssh-add
    elif ! agent_has_keys; then
    ssh-add
    fi
    unset 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
如果本文对你有所帮助,可以支持一下博主。