Search This Blog

Showing posts with label github. Show all posts
Showing posts with label github. Show all posts

SSH access to Github from multiple accounts

  • Generate SSH Keys:
    ssh-keygen  -t rsa -b 4096 -C "email@personal.com" -f ~/.ssh/id_rsa_personal
    ssh-keygen  -t rsa -b 4096 -C "email@work.com" -f ~/.ssh/id_rsa_work
    
    The two commands above generate two key pairs: id_rsa_personal/id_rsa_personal.pub, id_rsa_work/id_rsa_work.pub. 
  • Add SSH Keys to your github accounts.
    • Add id_rsa_personal.pub to your personal github account:
      • Login to your github
      • Go to Settings -> Add SSH Keys
      • Copy of the content of id_rsa_personal.pub to the key field
      • Click Add Key
    • Add id_rsa_work.pub to your github account for work:
      • Login to your github
      • Go to Settings -> Add SSH Keys
      • Copy of the content of id_rsa_work.pub to the key field
      • Click Add Key
  • Create a file: ~/.ssh/config and add:
    • Host personal
         HostName github.com
         User git
         IdentityFile ~/.ssh/id_rsa_personal
      
      Host work
         HostName github.com
         User git
         IdentityFile ~/.ssh/id_rsa_work
      
  • Update stored identities:
    • ssh-add -D
      ssh-add id_rsa_personal
      ssh-add id_rsa_work
      
  • Test
    • ssh -T personal
      Hi githubPersonal! You've successfully authenticated, but GitHub does not provide shell access.
      $ ssh -T work
      Hi githubWork! You've successfully authenticated, but GitHub does not provide shell access.
      

Github: Use Access Token to authenticate over HTTPS

  • Login to Github
  • If you have not enabled Two-factor authentication, you need to enable it:
    • Go to "Settings" -> "Security"
  • Generate Personal Access Tokens:
    • Go to "Settings" -> "Personal Access Tokens"
  • Click "Generate new token" at the top right corner.
  • Remember/Copy the token.
  • Insert the token to your repository url:
    https://<token>@github.com/owner/repo.git
  • Now you can push to the repository without typing username & password.

Github: fork a repository and sync the fork

  1. Fork a repository
    • Clone the repository:
      git clone https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git
    • Add the original repository as upstream:
      git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git
    • Check the status:
      git remote -v
      You will see:
      # origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
      # origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
      # upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git (fetch)
      # upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git (push)
      
  2. Sync the fork with upstream(original):
    • Fetch the branches and their respective commits from the upstream repository:
      git fetch upstream
    • Check out your fork's local master branch:
      git checkout master
    • Merge the changes from upstream/master into your local master branch. This brings your fork's master branch into sync with the upstream repository, without losing your local changes.
      git merge upstream/master
    • Push local repository:
      git push --tags