
Github Add Ssh in Git
To use Git with GitHub using SSH, you need to set up an SSH key for secure authentication between your local machine and GitHub. Here’s a step-by-step guide on how to add SSH to GitHub and use it for your Git operations:
Step 1: Check for Existing SSH Keys
Before creating a new SSH key, check if you already have one on your system.
- Open your terminal (Git Bash on Windows or Terminal on macOS/Linux).
- Run the following command to see if there are any existing SSH keys:
If you see files likels -al ~/.ssh
id_rsa.pub
orid_ed25519.pub
, you already have SSH keys.
Step 2: Generate a New SSH Key (if necessary)
If you don’t have an existing SSH key or want to generate a new one, follow these steps:
Generate the SSH key using the
ssh-keygen
command:ssh-keygen -t ed25519 -C "your_email@example.com"
Replace
"your_email@example.com"
with the email address associated with your GitHub account. Theed25519
algorithm is recommended as it's more secure and efficient than RSA.If your system doesn’t support
ed25519
, you can use RSA:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Choose a file location:By default, the key is saved to
~/.ssh/id_ed25519
(or~/.ssh/id_rsa
for RSA). Press Enter to accept the default location.Set a passphrase (optional):You can add an extra layer of security with a passphrase, but it’s not mandatory. If you don’t want one, press Enter to leave it empty.
Step 3: Add Your SSH Key to the SSH Agent
To manage your SSH keys, use the SSH agent. The agent runs in the background and handles your keys automatically.
Start the SSH agent:
eval "$(ssh-agent -s)"
Add your SSH private key to the agent:If you used the default key name, run:
ssh-add ~/.ssh/id_ed25519
If you used RSA, replace
id_ed25519
withid_rsa
:ssh-add ~/.ssh/id_rsa
Step 4: Add Your SSH Key to GitHub
Now, you need to add the public SSH key to your GitHub account.
Copy the public key:To copy the public key to your clipboard, run:
cat ~/.ssh/id_ed25519.pub
This will display the key. Copy the entire key (it starts with
ssh-ed25519
orssh-rsa
and ends with your email).Log in to GitHub:Go to GitHub and log in to your account.
Add SSH key to GitHub:
- Go to Settings (click your profile picture in the upper right corner, then click Settings).
- On the left sidebar, click SSH and GPG keys.
- Click the New SSH key button.
- In the Title field, give your key a name (e.g., “My Laptop”).
- Paste your public SSH key (the one you copied) into the Key field.
- Click Add SSH key.
Step 5: Test the SSH Connection
To ensure everything is set up correctly, test the SSH connection to GitHub:
ssh -T git@github.com
You should see a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
This means your SSH setup is working!
Step 6: Use SSH with Git Commands
Now that your SSH key is set up, you can use it to clone, pull, and push to repositories on GitHub without needing to enter your username and password every time.
Clone a repository using SSH:
git clone git@github.com:username/repository-name.git
Push changes:
git push origin main
Pull changes:
git pull origin main
Optional: Configure SSH to Use a Specific Key (if you have multiple keys)
If you have multiple SSH keys for different services, you can create a custom configuration for GitHub to ensure it uses the correct key.
Open (or create) the SSH config file:
nano ~/.ssh/config
Add the following configuration:
Host github.com User git HostName github.com IdentityFile ~/.ssh/id_ed25519
Replace
id_ed25519
with the name of your SSH key if you're using a different key.Save and exit the file (
CTRL + X
to exit, thenY
to confirm).
Troubleshooting
Permission denied (publickey): If you encounter an error like
Permission denied (publickey)
, make sure that:- The SSH key is added to the SSH agent.
- You have correctly added the public key to GitHub.
- You’re using the correct SSH URL for the repository (e.g.,
git@github.com:username/repository.git
).
Multiple SSH Keys: If you have multiple SSH keys, ensure that GitHub is using the right one by specifying the key in the SSH config file as described above.