使用場(chǎng)景
想象這樣一種場(chǎng)景,工作時(shí)間開發(fā)公司項(xiàng)目;下班的后同一臺(tái)機(jī)器也可能需要維護(hù)個(gè)人的Github上的開源項(xiàng)目。配置多賬戶就是一個(gè)很現(xiàn)實(shí)的問題镣陕。
配置步驟
前提是已經(jīng)安裝Git客戶端了;
- 如果配置了全局用戶姻政,請(qǐng)取消該配置呆抑。Git命令如下:
--取消全局配置
git config --global --unset user.name
git config --global --unset user.email
--在項(xiàng)目所在目錄下 設(shè)置項(xiàng)目所用的賬戶信息
git config user.email "somebody@someweb.com"
git config user.name "somename"
- 生成與郵箱相關(guān)的sshkey并配置,如下:
---Windows為例,路徑可以自定義汁展,注意大小寫
$ ssh-keygen -t rsa -f C:/Users/用戶名/.ssh/id_rsa_alicode -C "somebody@someweb.com"
- 為不同的項(xiàng)目網(wǎng)址配置指定公鑰鹊碍,配置文件位于C:/Users/登錄用戶/.ssh/config,注意是一個(gè)文件不是文件夾食绿,如果不存在就新建一個(gè)同名文件侈咕,找到后按下面格式編輯該文件。
# First user 個(gè)人項(xiàng)目git配置
Host code.aliyun.com
HostName code.aliyun.com
User aliyun
IdentityFile C:\Users\用戶名\.ssh\id_rsa_alicode
# second user 公司項(xiàng)目配置
Host 192.168.1.1
HostName 192.168.1.1
User work
IdentityFile C:\Users\用戶名\.ssh\id_rsa
- 測(cè)試公鑰配置是否生效器紧,如下:
---查看是否采用了正確的id_rsa_alicode文件
$ ssh -vT code.aliyun.com
---生效的結(jié)果是下面這樣的
OpenSSH_7.5p1, OpenSSL 1.0.2k 26 Jan 2017
debug1: Reading configuration data /c/Users/ziyuo/.ssh/config
debug1: /c/Users/ziyuo/.ssh/config line 2: Applying options for code.aliyun.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to code.aliyun.com [120.55.150.20] port 22.
debug1: Connection established.
debug1: identity file C:\\Users\\ziyuo\\.ssh\\id_rsa_alicode type 1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\ziyuo\\.ssh\\id_rsa_alicode-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.5
debug1: Remote protocol version 2.0, remote software version Go
debug1: no match: Go
debug1: Authenticating to code.aliyun.com:22 as 'aliyun'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:ZrA2ZqYTVyPbw4zytCSAv74ZMaS2LDH74I7sMPtQIG0
debug1: Host 'code.aliyun.com' is known and matches the RSA host key.
debug1: Found key in /c/Users/ziyuo/.ssh/known_hosts:1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: C:\\Users\\ziyuo\\.ssh\\id_rsa_alicode
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Authentication succeeded (publickey).
Authenticated to code.aliyun.com ([120.55.150.20]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
debug1: channel 0: free: client-session, nchannels 1
Connection to code.aliyun.com closed by remote host.
Transferred: sent 2648, received 1472 bytes, in 0.0 seconds
Bytes per second: sent 8978590.9, received 4991120.0
debug1: Exit status -1
- 配置生效后耀销,就可以clone項(xiàng)目了;這時(shí)候可以按照步驟1的操作為單個(gè)項(xiàng)目設(shè)置獨(dú)立賬戶铲汪。
以上熊尉,祝順利罐柳!
參考鏈接:
多個(gè)git賬號(hào)之間的切換
同一臺(tái)電腦可以有2個(gè)git賬號(hào)(不同網(wǎng)站的)