Multiple github accounts on the same computer?

后端 未结 25 2269
野的像风
野的像风 2020-11-22 02:56

Trying to work on my both my actual \"work\" repos, and my personal repos on git hub, from my computer.

The work account was set up first, and everything works flawl

相关标签:
25条回答
  • 2020-11-22 03:31

    Use HTTPS:

    change remote url to https:

    git remote set-url origin https://USERNAME@github.com/USERNAME/PROJECTNAME.git
    

    and you are good to go:

    git push
    

    To ensure that the commits appear as performed by USERNAME, one can setup the user.name and user.email for this project, too:

    git config user.name USERNAME
    git config user.email USERNAME@example.com
    
    0 讨论(0)
  • 2020-11-22 03:31

    Option 0: you dont want to mess around with OS settings.. you just want to commit to a different github account with a different public key for one repo.

    solution:

    1. create the new key: ssh-keygen -t rsa -b 4096 -f ~/.ssh/alt_rsa

    2. add the key to the keyset: ssh-add -K ~/.ssh/alt_rsa

    3. copy and add the pub key to the github account: (see github instructions)

    4. test the key with github: ssh -i ~/.ssh/alt_rsa T git@github.com

    5. clone the repo using the git protocol (not HTTP): git clone git@github:myaccount...

    6. in the cloned repo:

      git config core.sshCommand "ssh -i ~/.ssh/alt_rsa -F /dev/null"
      git config user.name [myaccount]
      git config user.email [myaccount email]

    7. now you should be able to git push correctly without interferring with your everyday git account

    0 讨论(0)
  • 2020-11-22 03:32

    I found this gem to be very useful: sshwitch

    https://github.com/agush22/sshwitch
    http://rubygems.org/gems/sshwitch

    It helps to switch out ssh keys. Remember to back up everything first!

    Also to make sure that commits have the correct email address associated with them, I made sure that the ~/.gitconfig file had the proper email address.

    0 讨论(0)
  • 2020-11-22 03:34

    I use shell scripts to switch me to whatever account I want to be "active". Essentially you start from a fresh start, get one account configured properly and working, then move the these files to a name with the proper prefix. From then on you can use the command "github", or "gitxyz" to switch:

    # 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
    

    I've had great luck with this. I also created a run script in Xcode (for you Mac users) so it would not build my project unless I had the proper setting (since its using git):

    Run Script placed after Dependencies (using /bin/ksh as the shell):

    if [ "$(git config --global --get user.email)" != "dhoerl@<company>.com" ]
    then
        exit 1
    fi
    

    EDIT: added tests for new files existence and copying old files to /tmp to address comment by @naomik below.

    0 讨论(0)
  • 2020-11-22 03:35

    just figured this out for Windows, using credentials for each repo:

    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
    

    The format of credential.https://github.com. tells the credential helper the URL for the credential. The 'useHttpPath' tells the credential manager to use the path for the credential. If useHttpPath is omitted then the credential manager will store one credential for https://github.com. If it is included then the credential manager will store multiple credentials, which is what I really wanted.

    0 讨论(0)
  • 2020-11-22 03:35

    You should and must not push to the project with some common credentials. Once starting on a new machine use the following steps to setup and use correctly your gitlab credentials:

    • create the pubic / private ssh keys on the machine
    • copy paste the public key to the gitlab/github ui interface ( anyone hinting how-to do via the cmd line gets a free beer ... )
    • make sure you clone the repo via the git and not http url
    • set the git alias to avoid constant typing of the same prefix to the git command
    • during git commit ALWAYS use the author and e-mail flags
    • use git as normal you would do it

    All this as follows:

     # 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 
    
    0 讨论(0)
提交回复
热议问题