Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scootcho/3427756b3ad31dff90490b02819eeff5 to your computer and use it in GitHub Desktop.
Save scootcho/3427756b3ad31dff90490b02819eeff5 to your computer and use it in GitHub Desktop.

Revisions

  1. @rahularity rahularity revised this gist Jul 10, 2023. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions work-with-multiple-github-accounts.md
    Original file line number Diff line number Diff line change
    @@ -180,3 +180,6 @@ Now you can use:
    git pull
    ```

    > P.S: <br> If this gist has been helpful to you, kindly consider leaving a star.
    > <br> If you'd like, let's connect on [LinkedIn](https://www.linkedin.com/in/rahularity) and build a supportive community together.
  2. @rahularity rahularity revised this gist Jan 12, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions work-with-multiple-github-accounts.md
    Original file line number Diff line number Diff line change
    @@ -41,13 +41,13 @@ Notice here **rahul-office** and **rahul-work** are the username of my github ac

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

    ![Passphrase Image](https://github.com/rahularity/github-essentials/blob/master/screenshots/passphrase.png)
    ![Passphrase Image](https://raw.githubusercontent.com/rahularity/github-essentials/bc3dafc37b36f5fb4ebcffcba63a7689a36fc7ff/screenshots/passphrase.png)

    > Now after adding keys , in your .ssh folder, a public key and a private will get generated.
    >The public key will have an extention __.pub__ and private key will be there without any extention both having same name which you have passed after __-f__ option in the above command. (in my case __github-rahul-office__ and __github-rahu-personal__)
    ![Added Key Image](https://github.com/rahularity/github-essentials/blob/master/screenshots/ssh_keys_added.png)
    ![Added Key Image](https://raw.githubusercontent.com/rahularity/github-essentials/master/screenshots/ssh_keys_added.png)

    <br>

  3. @rahularity rahularity revised this gist Jul 19, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion work-with-multiple-github-accounts.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # How To Work With Multiple Github Accounts on your PC
    # How To Work With Multiple Github Accounts on a single Machine

    Let suppose I have two github accounts, **https:/<span></span>/github.com<span></span>/rahul-office** and **https:/<span></span>/github.com<span></span>/rahul-personal**. Now i want to setup my mac to easily talk to both the github accounts.

  4. @rahularity rahularity revised this gist Jul 5, 2020. 1 changed file with 17 additions and 3 deletions.
    20 changes: 17 additions & 3 deletions work-with-multiple-github-accounts.md
    Original file line number Diff line number Diff line change
    @@ -143,7 +143,7 @@ Make a new project folder where you want to clone your repository and go to that
    For Example:
    I am making a repository on my personal github account and naming it **TestRepo**
    Now for cloning the repo use the below command:
    ```sh
    ```git
    git clone [email protected]{your-username}:{owner-user-name}/{the-repo-name}.git
    [e.g.] git clone [email protected]:rahul-personal/TestRepo.git
    @@ -157,12 +157,26 @@ From now on, to ensure that our commits and pushes from each repository on the s

    To do this use the following commands.

    ```sh
    ```git
    git config user.email "[email protected]"
    git config user.name "Rahul Pandey"
    git config user.email "[email protected]"
    git config user.name "Rahul Pandey"
    ```

    Pick the correct pair for your repository accordingly.


    To push or pull to the correct account we need to add the remote origin to the project
    ```git
    git remote add origin [email protected]:rahul-personal
    git remote add origin [email protected]:rahul-office
    ```

    Now you can use:
    ```git
    git push
    git pull
    ```
  5. @rahularity rahularity revised this gist Jul 5, 2020. No changes.
  6. @rahularity rahularity created this gist Jul 5, 2020.
    168 changes: 168 additions & 0 deletions work-with-multiple-github-accounts.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,168 @@
    # How To Work With Multiple Github Accounts on your PC

    Let suppose I have two github accounts, **https:/<span></span>/github.com<span></span>/rahul-office** and **https:/<span></span>/github.com<span></span>/rahul-personal**. Now i want to setup my mac to easily talk to both the github accounts.

    > NOTE: This logic can be extended to more than two accounts also. :)
    The setup can be done in 5 easy steps:
    ## Steps:
    - [Step 1](#step-1) : Create SSH keys for all accounts
    - [Step 2](#step-2) : Add SSH keys to SSH Agent
    - [Step 3](#step-3) : Add SSH public key to the Github
    - [Step 4](#step-4) : Create a Config File and Make Host Entries
    - [Step 5](#step-5) : Cloning GitHub repositories using different accounts

    <br>

    ## Step 1
    ### Create SSH keys for all accounts
    First make sure your current directory is your **.ssh** folder.
    ```sh
    $ cd ~/.ssh
    ```
    Syntax for generating unique ssh key for ann account is:
    ```sh
    ssh-keygen -t rsa -C "your-email-address" -f "github-username"
    ```
    here,

    **-C** stands for comment to help identify your ssh key

    **-f** stands for the file name where your ssh key get saved


    #### Now generating SSH keys for my two accounts
    ```sh
    ssh-keygen -t rsa -C "[email protected]" -f "github-rahul-office"
    ssh-keygen -t rsa -C "[email protected]" -f "github-rahul-personal"
    ```

    Notice here **rahul-office** and **rahul-work** are the username of my github accounts corresponding to **my_office_email<span></span>@gmail.com** and **my_personal_email<span></span>@gmail.com** email ids respectively.

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

    ![Passphrase Image](https://github.com/rahularity/github-essentials/blob/master/screenshots/passphrase.png)

    > Now after adding keys , in your .ssh folder, a public key and a private will get generated.
    >The public key will have an extention __.pub__ and private key will be there without any extention both having same name which you have passed after __-f__ option in the above command. (in my case __github-rahul-office__ and __github-rahu-personal__)
    ![Added Key Image](https://github.com/rahularity/github-essentials/blob/master/screenshots/ssh_keys_added.png)

    <br>

    ## Step 2
    ### Add SSH keys to SSH Agent
    Now we have the keys but it cannot be used until we add them to the SSH Agent.
    ```sh
    ssh-add -K ~/.ssh/github-rahul-office
    ssh-add -K ~/.ssh/github-rahul-personal
    ```

    You can read more about adding keys to SSH Agent [here.](https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)

    <br>

    ## Step 3
    ### Add SSH public key to the Github
    For the next step we need to add our public key (that we have generated in our previous step) and add it to corresponding github accounts.

    For doing this we need to:

    __1. Copy the public key__

    We can copy the public key either by opening the github-rahul-office.pub file in vim and then copying the content of it.
    ```sh
    vim ~/.ssh/github-rahul-office.pub
    vim ~/.ssh/github-rahul-personal.pub
    ```

    <p align="center">OR

    We can directly copy the content of the public key file in the clipboard.

    ```sh
    pbcopy < ~/.ssh/github-rahul-office.pub
    pbcopy < ~/.ssh/github-rahul-personal.pub
    ```


    __2. Paste the public key on Github__

    * Sign in to Github Account
    * Goto **Settings** > **SSH and GPG keys** > **New SSH Key**
    * Paste your copied public key and give it a Title of your choice.

    ___OR___

    * Sign in to Github
    * Paste this link in your browser (https://github.com/settings/keys) or click [here](https://github.com/settings/keys)
    * Click on **New SSH Key** and paste your copied key.

    <br>

    ## Step 4
    ### Create a Config File and Make Host Entries

    The **~/.ssh/config** file allows us specify many config options for SSH.

    If **config** file not already exists then create one (make sure you are in **~/.ssh** directory)

    ```sh
    touch config
    ```

    The commands below opens config in your default editor....Likely TextEdit, VS Code.
    ```sh
    open config
    ```
    Now we need to add these lines to the file, each block corresponding to each account we created earlier.
    ```config
    #rahul-office account
    Host github.com-rahul-office
    HostName github.com
    User git
    IdentityFile ~/.ssh/github-rahul-office
    #rahul-personal account
    Host github.com-rahul-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/github-rahul-personal
    ```

    <br>

    ## Step 5
    ### Cloning GitHub repositories using different accounts

    So we are done with our setups and now its time to see it in action. We will clone a repository using one of the account we have added.

    Make a new project folder where you want to clone your repository and go to that directory from your terminal.

    For Example:
    I am making a repository on my personal github account and naming it **TestRepo**
    Now for cloning the repo use the below command:
    ```sh
    git clone [email protected]{your-username}:{owner-user-name}/{the-repo-name}.git

    [e.g.] git clone [email protected]:rahul-personal/TestRepo.git
    ```

    <br>

    ## Finally

    From now on, to ensure that our commits and pushes from each repository on the system uses the correct GitHub user — we will have to configure **user.email** and **user.name** in every repository freshly cloned or existing before.

    To do this use the following commands.

    ```sh
    git config user.email "[email protected]"
    git config user.name "Rahul Pandey"

    git config user.email "[email protected]"
    git config user.name "Rahul Pandey"
    ```

    Pick the correct pair for your repository accordingly.