20 May 2024

How to Set up SSH Access to Github


==================================
How to Set up SSH Access to Github
==================================


1. Generate both public key and private key
-------------------------------------------

On your machine, change directory to ~/.ssh

$ ssh-keygen -t ed25519

You have got two files - 'id_ed25519' and 'id_ed25519.pub'. One is the public key, the other the private key. The public key file name has '.pub' at the end. The private key file has not.

2. Put public key on Github
---------------------------

Open github website. Log in. Click the Profile icon at the top right hand side corner > Settings > 'Access | SSH and GPG keys' > 'SSH keys | New SSH key'. Paste your public key there.

3. Copy your repository SSH address
-----------------------------------

Click profile icon > 'Your repositories'. Click one of the repositories > Code > SSH. Click the copy icon.

4. git clone
------------

Go back to your machine. You can git clone already.

$ git clone <Github SSH address> # (without <>)

5. git remote add origin
------------------------

Change directory to the branch. Append the SSH address to the following command, and run the command

$ git remote add origin <Github SSH address> # (without <>)

Change directory to .git/ under the branch directory.

Open ~/.git/config, you would see 

[remote "origin"]
    url = git@github.com:<githubUserName>/<branch>.git

6. Delete branch https address
------------------------------

If you used to use password/token access to Github, then there must be an http url in the config file. Remove it, otherwise Github will still ask for your password/token.

7. git pull or push
-------------------

You can now git pull and git push now.

$ git pull origin main

$ git push origin main

No comments:

Post a Comment