同一台计算机上有多个github帐户?

同一台计算机上有多个github帐户?,git,version-control,github,Git,Version Control,Github,试图从我的计算机上处理我的实际“工作”回购和我在git hub上的个人回购 首先建立了工作账户,一切都完美无瑕 然而,我的个人帐户似乎无法推送到我的个人回购协议,这是在不同的帐户/电子邮件下设置的 我尝试将我的工作密钥复制到我的个人帐户,但这会引发一个错误,因为一个密钥当然只能附加到一个帐户 如何从这两个帐户各自的github凭据推/拉入帐户?您需要做的就是使用多个SSH密钥对配置SSH设置 此链接很容易理解(谢谢Eric): 生成SSH密钥(Win/msysgit) 第一个链接的相

试图从我的计算机上处理我的实际“工作”回购和我在git hub上的个人回购

首先建立了工作账户,一切都完美无瑕

然而,我的个人帐户似乎无法推送到我的个人回购协议,这是在不同的帐户/电子邮件下设置的


我尝试将我的工作密钥复制到我的个人帐户,但这会引发一个错误,因为一个密钥当然只能附加到一个帐户


如何从这两个帐户各自的github凭据推/拉入帐户?

您需要做的就是使用多个SSH密钥对配置SSH设置

  • 此链接很容易理解(谢谢Eric):

  • 生成SSH密钥(Win/msysgit)

第一个链接的相关步骤:

  • 生成SSH密钥
    SSH-keygen-t rsa-C“john@doe.com“
    ,按照提示确定名称,例如,
    id\u rsa\u doe\u company
  • 将SSH公钥从
    ~/.SSH/id\u rsa\u doe\u company.pub
    复制到GitHub,并告诉SSH密钥:
    SSH add ~/.SSH/id\u rsa\u doe\u company
  • ~/.ssh
    中创建具有以下内容的
    config
    文件:
    Host github-doe-company
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_doe_company
    
  • 添加您的远程
    git远程添加源git@github-doe公司:用户名/repo.git
    或使用
    git远程设置url源进行更改git@github-doe公司:用户名/repo.git

  • 此外,如果您使用不同的角色处理多个存储库,则需要确保您的各个存储库相应地覆盖了用户设置:

    设置用户名、电子邮件和GitHub令牌–覆盖单个回购的设置

    希望这有帮助

    注意: 有些人可能需要为不同的存储库使用不同的电子邮件,从git2.13可以使用如下条件编辑位于:
    ~/.gitconfig
    的全局配置文件,在目录基础上设置电子邮件:

    [user]
        name = Pavan Kataria
        email = defaultemail@gmail.com
    
    [includeIf "gitdir:~/work/"]
        path = ~/work/.gitconfig
    
    然后您的特定于工作的config~/work/.gitconfig将如下所示:

    [user]
        email = pavan.kataria@company.tld
    
    [user]
        email = yourEmail@gmail.com
        name = Your_Name
    ...
    
    git config --global user.name WORK_USERNAME
    git config --global user.email WORK_USERNAME@example.com
    

    感谢@alexg在评论中告知我这一点。

    您需要做的就是使用多个SSH密钥对配置SSH设置

    • 此链接很容易理解(谢谢Eric):

    • 生成SSH密钥(Win/msysgit)

    第一个链接的相关步骤:

  • 生成SSH密钥
    SSH-keygen-t rsa-C“john@doe.com“
    ,按照提示确定名称,例如,
    id\u rsa\u doe\u company
  • 将SSH公钥从
    ~/.SSH/id\u rsa\u doe\u company.pub
    复制到GitHub,并告诉SSH密钥:
    SSH add ~/.SSH/id\u rsa\u doe\u company
  • ~/.ssh
    中创建具有以下内容的
    config
    文件:
    Host github-doe-company
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_doe_company
    
  • 添加您的远程
    git远程添加源git@github-doe公司:用户名/repo.git
    或使用
    git远程设置url源进行更改git@github-doe公司:用户名/repo.git

  • 此外,如果您使用不同的角色处理多个存储库,则需要确保您的各个存储库相应地覆盖了用户设置:

    设置用户名、电子邮件和GitHub令牌–覆盖单个回购的设置

    希望这有帮助

    注意: 有些人可能需要为不同的存储库使用不同的电子邮件,从git2.13可以使用如下条件编辑位于:
    ~/.gitconfig
    的全局配置文件,在目录基础上设置电子邮件:

    [user]
        name = Pavan Kataria
        email = defaultemail@gmail.com
    
    [includeIf "gitdir:~/work/"]
        path = ~/work/.gitconfig
    
    然后您的特定于工作的config~/work/.gitconfig将如下所示:

    [user]
        email = pavan.kataria@company.tld
    
    [user]
        email = yourEmail@gmail.com
        name = Your_Name
    ...
    
    git config --global user.name WORK_USERNAME
    git config --global user.email WORK_USERNAME@example.com
    

    感谢@alexg在评论中通知我这一点。

    我使用shell脚本将我切换到任何我想“激活”的帐户。基本上,您可以从头开始,正确配置一个帐户并使其正常工作,然后将这些文件移动到具有正确前缀的名称。从那时起,您可以使用命令“github”或“gitxyz”切换:

    # my github script
    cd ~/.ssh
    
    if [ -f git_dhoerl -a -f git_dhoerl.pub -a -f config_dhoerl ]
    then
        ; 
    else 
        echo "Error: missing new files"
        exit 1
    fi 
    
    # Save a copy in /tmp, just in case
    cp id_rsa /tmp
    cp id_rsa.pub /tmp
    cp config /tmp
    echo "Saved old files in /tmp, just in case"
    
    rm id_rsa
    rm id_rsa.pub
    rm config
    echo "Removed current links/files"
    
    ln git_dhoerl id_rsa
    ln git_dhoerl.pub id_rsa.pub
    ln config_dhoerl config
    
    git config --global user.email "dhoerl@<company>.com"
    git config --global github.user "dhoerl"        
    git config --global github.token "whatever_it_is"
    
    ssh-add -D
    
    #我的github脚本
    cd~/.ssh
    if[-f git_dhoerl-a-f git_dhoerl.pub-a-f config_dhoerl]
    然后
    ; 
    其他的
    echo“错误:缺少新文件”
    出口1
    fi
    #在/tmp中保存一份副本,以防万一
    cp id_rsa/tmp
    cp id_rsa.pub/tmp
    cp配置/tmp
    echo“将旧文件保存在/tmp中,以防万一”
    rm id_rsa
    rm id_rsa.pub
    rm配置
    回显“已删除当前链接/文件”
    ln git_dhoerl id_rsa
    在git_dhoerl.pub id_rsa.pub中
    ln配置_dhoerl配置
    git config--global user.email“dhoerl@.com"
    git config--全局github.user“dhoerl”
    git config——全局github.token“不管它是什么”
    ssh添加-D
    
    我在这方面运气很好。我还在Xcode中创建了一个运行脚本(适用于Mac用户),因此它不会构建我的项目,除非我有正确的设置(因为它使用git):

    运行放置在依赖项之后的脚本(使用/bin/ksh作为shell):

    if[“$(git config--global--get user.email)!=”dhoerl@.com" ]
    然后
    出口1
    fi
    

    编辑:添加了新文件存在性测试,并将旧文件复制到/tmp,以解决@naomik下面的评论。

    我使用shell脚本将我切换到任何我想“激活”的帐户。基本上,您可以从头开始,正确配置一个帐户并使其正常工作,然后将这些文件移动到具有正确前缀的名称。从那时起,您可以使用命令“github”或“gitxyz”切换:

    # my github script
    cd ~/.ssh
    
    if [ -f git_dhoerl -a -f git_dhoerl.pub -a -f config_dhoerl ]
    then
        ; 
    else 
        echo "Error: missing new files"
        exit 1
    fi 
    
    # Save a copy in /tmp, just in case
    cp id_rsa /tmp
    cp id_rsa.pub /tmp
    cp config /tmp
    echo "Saved old files in /tmp, just in case"
    
    rm id_rsa
    rm id_rsa.pub
    rm config
    echo "Removed current links/files"
    
    ln git_dhoerl id_rsa
    ln git_dhoerl.pub id_rsa.pub
    ln config_dhoerl config
    
    git config --global user.email "dhoerl@<company>.com"
    git config --global github.user "dhoerl"        
    git config --global github.token "whatever_it_is"
    
    ssh-add -D
    
    #我的github脚本
    cd~/.ssh
    if[-f git_dhoerl-a-f git_dhoerl.pub-a-f config_dhoerl]
    然后
    ; 
    其他的
    echo“错误:缺少新文件”
    出口1
    fi
    #在/tmp中保存一份副本,以防万一
    cp id_rsa/tmp
    cp id_rsa.pub/tmp
    cp配置/tmp
    echo“将旧文件保存在/tmp中,以防万一”
    rm id_rsa
    rm id_rsa.pub
    rm配置
    回显“已删除当前链接/文件”
    ln git_dhoerl id_rsa
    在git_dhoerl.pub id_rsa.pub中
    ln配置_dhoerl配置
    git config--global user.email“d
    
    $ git clone github.com-otheruser:someorg/somerepo.git
    $ git remote add origin github.com-otheruser:someuser/somerepo.git
    Host github.com-mainuser
      HostName github.com
      User git
      IdentityFile ~/.ssh/github-mainuser
    
    Host github.com-otheruser
      HostName github.com
      User git
      IdentityFile ~/.ssh/github-otheruser
    # account_1
    Host gitlab.com-account_1
    HostName gitlab.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_account_1
    
    # Account2
    Host gitlab.com-Account2
    HostName gitlab.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_Account2
    
    # Account_3
    Host github.com-Account3
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_Account_3
    
    git remote add origin git@gitlab.com-account_1:group_name/repo_name.git
    
    git remote add origin git@gitlab.com-Account2:group_name/repo_name.git
    
    git remote add origin github.com-Account3:github_username/repo_name.git
    
    cd ~/.ssh
    ssh-keygen -t rsa -C "email@work.com" -f "id_rsa_WORK"
    ssh-keygen -t rsa -C "email@gmail.com" -f "id_rsa_PERSONAL"
    
    sh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"`
    
    ls -al ~/.ssh
    
    ssh-add -D
    ssh-add ~/.ssh/id_rsa_work_user1 #make to use this without the suffix .pub
    
     ssh-add -D
     ssh-add ~/.ssh/<previous id_rsa>
    
    cd c:\User1\SomeRepo
    git config --local credential.https://github.com.user1 user1
    git config --local credential.useHttpPath true
    git config --local credential.helper manager
    git remote set-url origin https://USERNAME@github.com/USERNAME/PROJECTNAME.git
    
     # create the public / private key credentials on that specific machine
     ssh-keygen -t rsa -b 4096 -C "<<you>>@org.net" -f ~/.ssh/id_rsa.<<you>>.`hostname -s`
    
     # setup your public key in the gitlab ui 
     cat ~/.ssh/id_rsa.<<you>>.`hostname -s`
    
     # make sure you clone the repo via the git and not http url
     git clone git@git.in.org.net:org/some-repo.git
    
     # set the git alias to avoid constant typing of the repeating prefix to the git cmd
     alias git='GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa.<<you>>.`hostname -s`" git'
    
     # during git commit ALWAYS use the author and e-mail flags
     git add --all ; git commit -nm "$git_msg" --author "YourFirstName YourLastName <you@phz.fi>"
    
     # use git as normal
     git fetch --all; git pull --all 
    
    /home/your_username/.ssh/id_rsa_personal
    
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    
    ls -al ~/.ssh
    
    drwx------  2 gmadmin gmadmin 4096 Nov 16 22:20 .
    drwxr-xr-x 42 gmadmin gmadmin 4096 Nov 16 21:03 ..
    -rw-------  1 gmadmin gmadmin 1766 Nov 16 22:20 id_rsa_personal
    -rw-r--r--  1 gmadmin gmadmin  414 Nov 16 22:20 id_rsa_personal.pub
    -rw-r--r--  1 gmadmin gmadmin  444 Nov  6 11:32 known_hosts
    
    atom ~/.ssh/id_rsa_personal.pub
    
    ssh-add ~/.ssh/id_rsa_personal
    Enter passphrase for /home/your_username/.ssh/id_rsa_personal: 
    
    git config --global user.name "gitusername"
    git config --global user.email "gitemail"
    
    git init
    git add .
    git commit 
    git push
    
    Warning: Permanently added the RSA host key for IP address '61:fd9b::8c52:7203' to the list of known hosts.
    Permission denied (publickey).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    
    [user]
        email = first.last@home.com
        name = First Last
    [credential]
        username = PersonalGithubUsername
        helper = osxkeychain
    
    [user]
        email = first.last@work.com
        name = "First Last"
    [credential]
        helper = osxkeychain
    [includeIf "gitdir:~/Dropbox/**"]
        path = ~/Dropbox/.gitconfig
    
    git config user.email your_email@example.com
    git config user.name github_username
    
    git remote -v
    git remote set-url origin git@github.com:github_username/your-repo-name.git
    
    git remote show origin
    
    git config --global -e
    
    [user]
        email = yourEmail@gmail.com
        name = Your_Name
    ...
    
    git config --global credential.github.com.useHttpPath true
    
    [credential]
        useHttpPath = true
    ...
    
    a. git init // initialization 
    b. git add , // add 
    c. git commit -m "initial commit"
        you will get following output : in my case i use to add one python file created 
        from code.
        **[master (root-commit) d4defd9] initial commit
        2 files changed, 4 insertions(+)
        create mode 100644 .vscode/settings.json
        create mode 100644 Hello.py**
    
    d.  git remote add origin <HTTPS repo link>
    e.  git remote -v // check the repo version 
    f.  git push origin master
        it will ask your git hub user name and password via popup screen. 
        you will get the following output 
        Counting objects: 100% (5/5), done.
        Delta compression using up to 4 threads
        Compressing objects: 100% (3/3), done.
        Writing objects: 100% (5/5), 411 bytes | 31.00 KiB/s, done.
        Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
        remote:
        remote: Create a pull request for 'master' on GitHub by visiting:
        remote:      https://github.com/vishvadeepaktripathi/Hello_Py/pull/new/master
        remote:
        To https://github.com/vishvadeepaktripathi/Hello_Py.git
         * [new branch]      master -> master
    
    git config --global user.name WORK_USERNAME
    git config --global user.email WORK_USERNAME@example.com
    
    git config user.name PERSONAL_USERNAME
    git config user.email PERSONAL_USERNAME@example.com
    
    ssh-keygen -t rsa -C "your-email-address"
    
    touch ~/.ssh/config
    
    #Default GitHub
    Host github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa
    
    #New Account
    Host github-new-account-name
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_new_account_name
    
    git remote add origin git@github-new-account-name:your-domain.com/your-repo.git