Skip to content

chalokodekare/work-with-multiple-github-accounts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

How to work with multiple Github Accounts on a single Machine

Suppose I have two accounts,

  • https://github.com/chalokodekare
  • https://github.com/ritubhandari

I would configure my system with both the accounts so that I can work on my official and personal projects both.

To do this, follow the below steps

1. Generate SSH keys for all the accounts

     cd ~/.ssh
     ssh-keygen -t rsa -C "chalocodekare@gmail.com" -f "chalocodekare"
     ssh-keygen -t rsa -C "ritubhandari@gmail.com" -f "ritubhandari"

here,

  • -C is for adding a comment, it can be anything, but it will be helpful to know the email associated with your account.
  • -f is to define a filename, and creating a key with a descriptive name will help you remember which key is used for which user

After entering the command the terminal will ask for passphrase, leave it empty and proceed.

Generate Keys Image

Now in .ssh folder, a public and a private key will be generated. Public key will have an extension .pub and private key will be without any extension both having same name which we have passed after -f option in the above command.

Generated Keys Image

2. Add SSH keys to SSH Agent

We will add above generated keys to ssh-agent.

     ssh-add ~/.ssh/chalocodekare
     ssh-add ~/.ssh/ritubhandari
 after writing ssh-add ~/.ssh/chalocodekare, hit tab button and it'll take full system path of the file.

Adding SSH Keys Image

3. Add SSH public key to the Github

Now we need to add our public keys to corresponding github accounts.

1. Copy the public key

     cat ~/.ssh/chalocodekare.pub & copy the content
     cat ~/.ssh/ritubhandari.pub & copy the content

Copy public key content Image

2. Paste the public key in Github

  • Do this for each key/account one by one.
  • Sign in to corresponding Github account.
  • Go to Settings > SSH and GPG keys > New SSH Key
  • Paste respective copied public key content. Title can be of your choice.

Adding SSH Keys In Github Image

4. Create or Edit Config File and Make Host Entries

In .ssh folder, look for config file. If config file doesn't exist then create one (make sure to be in ~/.ssh directory)

     touch config (create this file however you want)
     open config (open it in edit mode however you want)

Add below lines in config file.

     Host chalocodekare
         HostName github.com
         User git
         IdentityFile ~/.ssh/chalocodekare
    
     Host ritubhandari
         HostName github.com
         User git
         IdentityFile ~/.ssh/ritubhandari

Create/Update config file Image

5. Clone GitHub repositories using different accounts

Now to clone https://github.com/chalokodekare/work-with-multiple-github-accounts.git, we can use any account:

    git clone {your-host-in-config-file}:{owner-user-name}/{the-repo-name}.git

    [e.g.] git clone chalocodekare:chalokodekare/work-with-multiple-github-accounts.git
        or git clone ritubhandari:chalokodekare/work-with-multiple-github-accounts.git

Clone repository Image

6. Configure User Name & Email

To make sure our commits in each repository uses the correct GitHub user — we will have to configure user.email and user.name in all existing or newly cloned repositories.

     git config user.email "chalocodekare@gmail.com"
     git config user.name "Chalo Code Kare"
     
     or 
     git config user.email "ritubhndari@gmail.com"
     git config user.name "Ritu Bhandari"
     
     depending on which account you're using.

Pick the correct pair for your repository accordingly.

You can verify global user name and email id using below commands.

     git config --global user.email   
     git config --global user.name

Now you can use:

     git push
     
     git pull

7. Verify remote

To verify if your remote is pointing correctly, you can do:

    git remote -v

Verify remote Image

remove & add correct remote if incorrect.

To remove existing remote

git remote remove origin

To add new remote

git remote add origin chalocodekare:chalokodekare/work-with-multiple-github-accounts.git

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published