Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RaymondSHANG/0ff65d5f102a1e62761d76a83e69d9ea to your computer and use it in GitHub Desktop.
Save RaymondSHANG/0ff65d5f102a1e62761d76a83e69d9ea to your computer and use it in GitHub Desktop.

Revisions

  1. @oanhnn oanhnn revised this gist Aug 30, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion using-multiple-github-accounts-with-ssh-keys.md
    Original file line number Diff line number Diff line change
    @@ -100,7 +100,7 @@ The bash script that prompts for your git account. Thank @davorpa
    #!/bin/bash
    # silent prompt
    input -sp 'GIT profile: ' profile
    read -p 'GIT profile: ' profile
    # switch
    case $profile in
  2. @oanhnn oanhnn revised this gist Aug 30, 2023. 1 changed file with 65 additions and 11 deletions.
    76 changes: 65 additions & 11 deletions using-multiple-github-accounts-with-ssh-keys.md
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ Use ssh keys and define host aliases in ssh config file (each alias for an accou

    ## How to?
    1. [Generate ssh key pairs for accounts](https://help.github.com/articles/generating-a-new-ssh-key/) and [add them to GitHub accounts](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/).

    2. Edit/Create ssh config file (`~/.ssh/config`):

    ```conf
    @@ -24,6 +25,8 @@ Use ssh keys and define host aliases in ssh config file (each alias for an accou
    IdentitiesOnly yes
    ```

    > NOTE: If you use any account frequently, you should use the default hostname (`github.com`).
    3. [Add ssh private keys to your agent](https://help.github.com/articles/adding-a-new-ssh-key-to-the-ssh-agent/):

    ```shell
    @@ -34,18 +37,11 @@ Use ssh keys and define host aliases in ssh config file (each alias for an accou
    4. Test your connection

    ```shell
    $ ssh-keyscan github.com >> ~/.ssh/known_hosts
    $ ssh -T [email protected]
    $ ssh -T git@github-superman
    ```

    With each command, you may see this kind of warning, type `yes`:

    ```shell
    The authenticity of host 'github.com (192.30.252.1)' can't be established.
    RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:
    Are you sure you want to continue connecting (yes/no)?
    ```
    If everything is OK, you will see these messages:

    ```shell
    @@ -56,13 +52,71 @@ Use ssh keys and define host aliases in ssh config file (each alias for an accou
    Hi superman! You've successfully authenticated, but GitHub does not provide shell access.
    ```

    5. Now all are set, just clone your repositories
    5. Now all are set, you need remeber

    ```
    git@github-superman:org/project.git => user is superman
    [email protected]:org/project.git. => user is oanhnn
    ```

    - If you need clone a repository, just do:

    ```shell
    $ git clone git@github-superman:org2/project2.git /path/to/project2
    $ git clone git@github-superman:org1/project1.git /path/to/project1
    $ cd /path/to/project1
    $ git config user.email "[email protected]"
    $ git config user.name "Super Man"
    ```

    - If you already have the repo set up, after the ssh config instructions, you need change the URL of `origin`, just do:

    ```
    $ cd /path/to/project2
    $ git config user.email "[email protected]"
    $ git remote set-url origin git@github-superman:org2/project2.git
    $ git config user.email "[email protected]"
    $ git config user.name "Super Man"
    ```

    - If you are creating a new repository on local:

    ```
    $ cd /path/to/project3
    $ git init
    $ git remote add origin git@github-superman:org3/project3.git
    $ git config user.email "[email protected]"
    $ git config user.name "Super Man"
    $ git add .
    $ git commit -m "Initial commit"
    $ git push -u origin master
    ```

    Done! Goodluck!

    ## Addon:

    The bash script that prompts for your git account. Thank @davorpa

    ```
    #!/bin/bash
    # silent prompt
    input -sp 'GIT profile: ' profile
    # switch
    case $profile in
    superman)
    git config user.email "[email protected]"
    git config user.name "superman"
    git config user.signingKey "superman_gpg_public_key"
    ;;
    oanhnn)
    git config user.email "[email protected]"
    git config user.name "oanhnn"
    git config user.signingKey "oanhnn_gpg_public_key"
    ;;
    # default case: raise error
    *)
    >&2 echo "ERR: Unknown profile: $profile"
    exit 1
    esac
    ```
  3. Oanh Nguyen revised this gist Feb 23, 2016. 1 changed file with 39 additions and 21 deletions.
    60 changes: 39 additions & 21 deletions using-multiple-github-accounts-with-ssh-keys.md
    Original file line number Diff line number Diff line change
    @@ -1,50 +1,68 @@

    ## Problem
    I have a GitHub account, it is *oanhnn*, but at office, I couldn't use my personal GitHub account. I have to use an other GitHub account, eg *supermen*.
    I want use both two accounts on same computer (use without typing password each times, when git push or pull).
    I have two Github accounts: *oanhnn* (personal) and *superman* (for work).
    I want to use both accounts on same computer (without typing password everytime, when doing git push or pull).

    ## Solution
    Using ssh keys and ssh config file to create an alias host and use multiple GitHub accounts
    Use ssh keys and define host aliases in ssh config file (each alias for an account).

    ## How to?
    1. [Make key pair for each accounts](https://help.github.com/articles/generating-a-new-ssh-key/) and [add it to GitHub accounts](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/).
    2. Make ssh configure file (`~/.ssh/config`) like:
    1. [Generate ssh key pairs for accounts](https://help.github.com/articles/generating-a-new-ssh-key/) and [add them to GitHub accounts](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/).
    2. Edit/Create ssh config file (`~/.ssh/config`):

    ```
    ```conf
    # Default github account: oanhnn
    Host github.com
    HostName github.com
    IdentityFile ~/.ssh/oanhnn_private_key
    IdentitiesOnly yes
    # Other github account: supermen
    Host github-supermen
    # Other github account: superman
    Host github-superman
    HostName github.com
    IdentityFile ~/.ssh/supermen_private_key
    IdentityFile ~/.ssh/superman_private_key
    IdentitiesOnly yes
    ```

    3. [Added ssh key to your agent](https://help.github.com/articles/adding-a-new-ssh-key-to-the-ssh-agent/) by command:
    3. [Add ssh private keys to your agent](https://help.github.com/articles/adding-a-new-ssh-key-to-the-ssh-agent/):

    ```
    ```shell
    $ ssh-add ~/.ssh/oanhnn_private_key
    $ ssh-add ~/.ssh/supermen_private_key
    $ ssh-add ~/.ssh/superman_private_key
    ```

    4. Check that repo recognizes keys.
    4. Test your connection

    ```
    ```shell
    $ ssh -T [email protected]
    $ ssh -T git@github-supermen
    $ ssh -T git@github-superman
    ```

    5. Clone projects
    With each command, you may see this kind of warning, type `yes`:

    ```shell
    The authenticity of host 'github.com (192.30.252.1)' can't be established.
    RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:
    Are you sure you want to continue connecting (yes/no)?
    ```
    $ git clone git@github-supermen:org2/project2.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "[email protected]"
    $ git config user.name "Super Men"
    If everything is OK, you will see these messages:
    ```shell
    Hi oanhnn! You've successfully authenticated, but GitHub does not provide shell access.
    ```

    ```shell
    Hi superman! You've successfully authenticated, but GitHub does not provide shell access.
    ```
    5. Now all are set, just clone your repositories
    ```shell
    $ git clone git@github-superman:org2/project2.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "[email protected]"
    $ git config user.name "Super Man"
    ```
    Done! Have goodluck!
    Done! Goodluck!
  4. Oanh Nguyen revised this gist Feb 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion using-multiple-github-accounts-with-ssh-keys.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ I have a GitHub account, it is *oanhnn*, but at office, I couldn't use my person
    I want use both two accounts on same computer (use without typing password each times, when git push or pull).

    ## Solution
    Using ssh keys and ssh configure file to alias host and using multiple GitHub accounts
    Using ssh keys and ssh config file to create an alias host and use multiple GitHub accounts

    ## How to?
    1. [Make key pair for each accounts](https://help.github.com/articles/generating-a-new-ssh-key/) and [add it to GitHub accounts](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/).
  5. Oanh Nguyen revised this gist Feb 1, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions using-multiple-github-accounts-with-ssh-keys.md
    Original file line number Diff line number Diff line change
    @@ -14,11 +14,13 @@ Using ssh keys and ssh configure file to alias host and using multiple GitHub ac
    Host github.com
    HostName github.com
    IdentityFile ~/.ssh/oanhnn_private_key
    IdentitiesOnly yes
    # Other github account: supermen
    Host github-supermen
    HostName github.com
    IdentityFile ~/.ssh/supermen_private_key
    IdentitiesOnly yes
    ```

    3. [Added ssh key to your agent](https://help.github.com/articles/adding-a-new-ssh-key-to-the-ssh-agent/) by command:
  6. Oanh Nguyen revised this gist Feb 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion using-multiple-github-accounts-with-ssh-keys.md
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ Using ssh keys and ssh configure file to alias host and using multiple GitHub ac
    $ git clone git@github-supermen:org2/project2.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "[email protected]"
    $ git config user.name "Supper Men"
    $ git config user.name "Super Men"
    ```


  7. Oanh Nguyen revised this gist Feb 1, 2016. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions using-multiple-github-accounts-with-ssh-keys.md
    Original file line number Diff line number Diff line change
    @@ -1,46 +1,46 @@
    ## Problem
    I have a github account, it is *oanhnn*, but when i join a private project in company, i have to use an other github account, eg *suppermen*.
    I want using two accounts without typing password each times, when git push or pull.
    I have a GitHub account, it is *oanhnn*, but at office, I couldn't use my personal GitHub account. I have to use an other GitHub account, eg *supermen*.
    I want use both two accounts on same computer (use without typing password each times, when git push or pull).

    ## Solution
    Using ssh keys and ssh configure file to alias host and using multiple github accounts
    Using ssh keys and ssh configure file to alias host and using multiple GitHub accounts

    ## How to?
    1. Make key pair for each accounts and add it to github accounts.
    1. [Make key pair for each accounts](https://help.github.com/articles/generating-a-new-ssh-key/) and [add it to GitHub accounts](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/).
    2. Make ssh configure file (`~/.ssh/config`) like:

    ```
    # Default github account: oanhnn
    Host github.com
    HostName github.com
    IdentityFile ~/.ssh/oanhnn_key
    IdentityFile ~/.ssh/oanhnn_private_key
    # Other github account: suppermen
    Host github-suppermen
    # Other github account: supermen
    Host github-supermen
    HostName github.com
    IdentityFile ~/.ssh/suppermen_key
    IdentityFile ~/.ssh/supermen_private_key
    ```

    3. Added ssh key to your agent by command:
    3. [Added ssh key to your agent](https://help.github.com/articles/adding-a-new-ssh-key-to-the-ssh-agent/) by command:

    ```
    $ ssh-add ~/.ssh/oanhnn_key
    $ ssh-add ~/.ssh/suppermen_key
    $ ssh-add ~/.ssh/oanhnn_private_key
    $ ssh-add ~/.ssh/supermen_private_key
    ```

    4. Check that repo recognizes keys.

    ```
    $ ssh -T [email protected]
    $ ssh -T git@github-suppermen
    $ ssh -T git@github-supermen
    ```

    5. Clone projects

    ```
    $ git clone git@github-suppermen:org2/project2.git /path/to/project2
    $ git clone git@github-supermen:org2/project2.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "suppermen@org2.com"
    $ git config user.email "supermen@org2.com"
    $ git config user.name "Supper Men"
    ```

  8. Oanh Nguyen revised this gist Jan 26, 2016. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion using-multiple-github-accounts-with-ssh-keys.md
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ Using ssh keys and ssh configure file to alias host and using multiple github ac
    ## How to?
    1. Make key pair for each accounts and add it to github accounts.
    2. Make ssh configure file (`~/.ssh/config`) like:

    ```
    # Default github account: oanhnn
    Host github.com
    @@ -19,23 +20,29 @@ Using ssh keys and ssh configure file to alias host and using multiple github ac
    HostName github.com
    IdentityFile ~/.ssh/suppermen_key
    ```

    3. Added ssh key to your agent by command:

    ```
    $ ssh-add ~/.ssh/oanhnn_key
    $ ssh-add ~/.ssh/suppermen_key
    ```

    4. Check that repo recognizes keys.

    ```
    $ ssh -T [email protected]
    $ ssh -T git@github-suppermen
    ```

    5. Clone projects

    ```
    $ git clone git@github-suppermen:org2/project2.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "[email protected]"
    $ git config user.name "Supper Men"
    ```


    Done! Have goodluck!
  9. Oanh Nguyen created this gist Jan 26, 2016.
    41 changes: 41 additions & 0 deletions using-multiple-github-accounts-with-ssh-keys.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    ## Problem
    I have a github account, it is *oanhnn*, but when i join a private project in company, i have to use an other github account, eg *suppermen*.
    I want using two accounts without typing password each times, when git push or pull.

    ## Solution
    Using ssh keys and ssh configure file to alias host and using multiple github accounts

    ## How to?
    1. Make key pair for each accounts and add it to github accounts.
    2. Make ssh configure file (`~/.ssh/config`) like:
    ```
    # Default github account: oanhnn
    Host github.com
    HostName github.com
    IdentityFile ~/.ssh/oanhnn_key
    # Other github account: suppermen
    Host github-suppermen
    HostName github.com
    IdentityFile ~/.ssh/suppermen_key
    ```
    3. Added ssh key to your agent by command:
    ```
    $ ssh-add ~/.ssh/oanhnn_key
    $ ssh-add ~/.ssh/suppermen_key
    ```
    4. Check that repo recognizes keys.
    ```
    $ ssh -T [email protected]
    $ ssh -T git@github-suppermen
    ```
    5. Clone projects
    ```
    $ git clone git@github-suppermen:org2/project2.git /path/to/project2
    $ cd /path/to/project2
    $ git config user.email "[email protected]"
    $ git config user.name "Supper Men"
    ```


    Done! Have goodluck!