zoukankan      html  css  js  c++  java
  • 如何切换多个GitHub账号

    1 前言

    很惭愧做了几年开发,刚刚使用Git。入职的时候使用公司邮箱注册的GitHub账号,后来使用私人邮箱又注册一个。为了解决同一系统上多个账号切换的问题,查了一些资料,在此总结一下。

    注:当前操作系统是Windows10,shell工具是PowerShell, Git版本为Git for Windows-2.9.2-64bit。

    2 本地配置

    遇到的第一个问题是Author。每次查看提交记录时,总会看到Author的用户名和邮箱。如果使用两个不同的账户当然希望看到不同的Author提交。Git把信息都存储在本地的config文件中,包括后面要说到的本地凭证存储也和config有关,所以在此有必要说一下。

    Git共有三个级别的config文件,分别是system、global和local。在当前环境中,分别对应%GitPath%mingw64etcgitconfig文件、$home.gitconfig文件和%RepoPath%.gitconfig文件。其中%GitPath%为Git的安装路径,%RepoPath%为某仓库的本地路径。所以system配置整个系统只有一个,global配置每个账户只有一个,而local配置和git仓库的数目相同,并且只有在仓库目录才能看到该配置。

    2.1 查看配置

    # list git all config about user.   tips: D:	estTestUser is  a dictionary of Repo
    PS D:	estTestUser> git config --list | sls user
    user.email=SystemUser@outlook.com
    user.name=SystemUser
    user.name=GlobalUser
    user.email=GlobalUser@outlook.com
    user.name=LocalUser
    user.email=LocalUser@outlook.com
    

    以上可见,筛选出的user.nameuser.email就是前面提到的Author信息。此时,上面提到的三个级别都配置了user信息。当git commit时,Author信息依次读取local、global和system的配置,如果找到则不再继续读取。其他配置的读取顺序也是如此

    2.2 更改配置

    更改local级别的user.name(本地Repo中的config):

    # set user.name in local config and query.  tips: D:	estTestUser is  a dictionary of Repo
    PS D:	estTestUser> git config --local user.name NinputerWonder
    PS D:	estTestUser> git config --local user.name
    NinputerWonder
    

    依此类推,可以更改global和system的配置。其他配置的更改和读取操作类似
    接下来会用到上面类似的操作更改本地凭证存储。

    3 使用https协议

    GitHub支持https和ssh协议连接。当执行类似命令git clone https://github.com/NinputerWonder/xxx.git后,在此仓库中的上传下载均采用https协议;类似git clone git@github.com:NinputerWonder/xxx.git则使用ssh协议。首先讨论使用https协议。

    当提交代码后,向服务器push的时候,这时git会提示输入用户名和密码。输入正确之后,git会有多种策略存储本地凭证,以免下次再输入账户信息。

    切换到`%GitPath%mingw64libexecgit-core路径下,可以查看本地存储工具:

    PS C:Program FilesGitmingw64libexecgit-core> (ls).Name | sls credential
    git-credential-manager.exe
    git-credential-store.exe
    git-credential-wincred.exe
    git-credential.exe
    

    以上列出当前支持的三种存储辅助工具,即mananger、wincred和store。Git可以指定辅助工具(通过配置credential.helper),用来存储本地凭证。

    3.1 manager

    若安装Git时安装了GitGUI,自动会在system级别中设置credential.helper为manager。并且不配置所处级别(system、global或者local)如何,一旦设置了manager,都优先使用该方式。

    查看不同级别的credential.helper

    PS D:	estTestUser> git config --global credential.helper
    manager
    PS D:	estTestUser> git config --local credential.helper
    store
    

    若本地没有存储凭证,第一次push的时候,会弹出窗口,要求输入用户名密码(图1)。

    图 1 manager工具提示输入账户信息


    输入并验证成功后并将其存储至Windows的凭据管理器中(图2)。

    图 2 Windows凭据管理器中记录的信息(通过manager工具保存)


    当再次在本机push的时候,会直接读取凭据管理器的中账户信息。经个人测试,除非手动点击“编辑”或者“删除”,否则无法更改账户

    3.2 wincred

    设置credential.helper,如wincred

    # query current credential.helper config
    PS D:	estTestUser> git config --list | sls credential.helper
    credential.helper=manager
    credential.helper=store
    # remove credential setting in local config
    PS D:	estTestUser> git config --local --remove-section credential
    # change credential.helper to wincred in global config
    PS D:	estTestUser> git config --global credential.helper wincred
    #  query current credential.helper config. only wincred left
    PS D:	estTestUser> git config --list | sls credential.helper
    credential.helper=wincred
    

    上面删除了local中的credential.helper配置并且设置了global中的改配置,所以会使用后者。
    如果本地没有账户信息,当push时会提示输入。如果输入正确也会记录在Windows的凭据管理器中(图3)

    PS D:	estTestUser> git push
    Username for 'https://github.com': NinputerWonder
    Password for 'https://NinputerWonder@github.com':
    Everything up-to-date
    

    图 3 Windows凭据管理器中记录的信息(通过wincred工具保存)


    除此之外,该工具还可以使用命令行管理本地存储。
    查看本地记录账号(protocol和host是输入,注意空行,其余为输出

    PS D:	estTestUser> git credential-wincred get
    protocol=https
    host=github.com
    
    username=NinputerWonder
    password=123456
    

    删除本地账号(以下全部为输入,注意空行

    PS D:	estTestUser> git credential-wincred erase
    protocol=https
    host=github.com
    username=NinputerWonder
    password=123456
    
    # after erase, the account was removed
    PS D:	estTestUser> git credential-wincred get
    protocol=https
    host=github.com
    
    

    同时Windows的凭据管理器中该账户信息也被删除。

    添加账号(以下全部为输入,注意空行

    PS D:	estTestUser> git credential-wincred store
    protocol=https
    host=github.com
    username=NinputerWonder
    password=123456
    
    

    同时,该账号信息出现在Windows的凭据管理器中。

    但是,经个人测试, 如果同一个host使用wincred存储多份凭证,只会有一个账户生效。

    3.3 store

    将当前local级别的credential.helper设置成store(过程和前面类似),此时的存储方式变成了store。如果本地没有存储账号信息,当push时输入正确信息,将此保存至home目录下的.git-credentials文件,并且以明文存储,内容如下:

    https://NinputerWonder:123456@github.com

    官方文档指出可以使用类似如下参数的形式指定存储上述信息文件。

    git config --global credential.helper store --file=~/cred.txt
    

    但经个人测试并未生效,不过可以直接修改config文件来达到目的,如:

    [credential]
    helper = store --file=D:/credential/cred1.txt

    再次push,便会将账户信息存至D:/credential/cred1.txt中。若想清除账户,删除该文件即可。

    3.4 小结

    综上,因为store使用明文保存账户信息,存在安全隐患。使用manager和wincred方式比较安全,但不能满足多账户(GitHub账户)切换(除非删除重录)。
    使用store方式时,配置每个repository的local级别credential.helper,并且通过--file配置不同的文件,这样每个repository可以读取独立的账户信息,可满足需求。

    4 使用ssh协议

    当使用ssh协议时,不需要像上面存储账户信息,但是需要在客户端和服务器上分别存储成对的private key和public key,校验成功,方可与服务器通信(包括git clone命令)。

    4.1 生成Key

    使用如下命令生成key

    PS D:ssh> ssh-keygen -t rsa -b 4096 -C "NinputerWonder@outlook.com"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Users/kingw/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /c/Users/kingw/.ssh/id_rsa.
    Your public key has been saved in /c/Users/kingw/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:lmAq9yVuN2u6r4zffwfadSsxn78nwJghg/StRGSRk0 NinputerWonder@outlook.com
    The key's randomart image is:
    +---[RSA 4096]----+
    |o=o=E+           |
    |A ++o*           |
    |o+. * o          |
    | ... = . .       |
    |o o = + S        |
    |.. = B +  +       |
    |  . . B o        |
    |     * +.o       |
    |    ..**+        |
    +----[SHA256]-----+
    

    如果不显式指定目录和文件名,生成文件public key文件为$home/.ssh/ id_rsa.pub, private key为$home/.ssh/ id_rsa,并且自动添加至ssh-agent。

    4.2 将public key上传至服务器

    登陆个人账户,打开Setting->SSH and GPB keys->New SSH Key,将public key上传至GItHub服务器(图4)。

    图 4 New SSH key


    此时,在本地git clone git@github.com:NinputerWonder/xxx.git即可下载Repository。

    4.3 配置多账户

    假设上一个账户为私人账户,现在为公司账户创建key,并且将public key添加至GitHub服务器。

    ssh-keygen -t rsa -b 4096 -f $home/.ssh/company -C "wdwangtw@gmail.com"
    

    将新生成的private key添加至ssh-agent。

    PS D:	est> ssh-agent bash
    kingwtw@DESKTOP-IIKCT85  /d/test
    $ ssh-add ~/.ssh/rsa-company
    Identity added: /c/Users/kingwtw/.ssh/company (/c/Users/kingwtw/.ssh/company)
    

    接下来创建ssh的config文件(注意该config文件和前面提到的配置文件不是同一类型),该文件位置为$home/.ssh/config。内容如下:

    Host myhost personal
    User NinputerWonder
    HostName github.com
    IdentityFile ~/.ssh/id_rsa
    
    Host myhost company
    User wdwangtw
    HostName github.com
    IdentityFile ~/.ssh/company
    

    不难看出,其格式为

    Host myhost USER_HOST     ;USER_HOST为自定义host名字,如上面的personal和company
    User USER_NAME                 ;USER_NAME为自定义名称
    HostName SERVER_HOST   ;SERVER_HOST为实际服务器host,此时为GitHub
    IdentityFile PRIVATE_KEY      ;PRIVATE_KEY为本地key
    

    当再次clone一个新Repos时,如果其ssh地址为git@github.com:wdwangtw/xxx.git,使用git clone git@company:wdwangtw/TestUserwd.git即可clone到本地(注意github.com换成了自定义的company),并且push时也不用输入任何验证。

    4.4 小结

    使用ssh协议时,通过配置key的方式,从clone到push每一步都会进行key的校验,使用起来更加安全可靠。

    总结

    使用https和ssh协议都可以做到多个GitHub账号在同一系统分别工作。使用https时,通过配置local的credential.helper为store,并且指定本地存储文件的方式达到目的。但是store方式明文存储用户信息,容易泄露账号。使用ssh协议时,如果更换机器,需要将本地private key拷贝至新系统,或者在新系统上生成新的key,并且把新生成的private key添加到GitHub后才能正常使用。

    后记

    以上为个人在当前环境下测试,某些官方提供的命令并未生效。如果读者发现不正确或者不明确的地方,欢迎指正。

    参考文档

    1 Customizing Git
    2 GItHub Help - SSH
    3 http://www.cnblogs.com/BeginMan/p/3548139.html



    作者:脑袋Mini
    链接:https://www.jianshu.com/p/0ad3d88c51f4
    来源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 相关阅读:
    动态规划>0/1背包问题 小强斋
    【C/C++和指针】auto_ptr智能指针
    WEBSHELL
    sql反模式分析1
    SQLServer之Compute/ComputeBy实现数据汇总
    链表算法大全
    【C++ Primer】自己动手编写函数 atoi(char *str)
    MySQL 5.5 的COMPRESSED INNODB 表
    【设计模式】加薪非要老总批 职责链模式
    【C和指针】const指针
  • 原文地址:https://www.cnblogs.com/oneDongHua/p/14264087.html
Copyright © 2011-2022 走看看