-
Check for existing SSH keys:
Open a terminal and run:
ls -al ~/.sshThis will list the files in the
.sshdirectory to see if you already have an SSH key. Look for files likeid_rsaandid_rsa.pub. -
Generate a new SSH key:
If you don't have an existing SSH key, generate a new one by running:
ssh-keygen -t ed25519 -C "[email protected]"If you're using an older system that doesn't support the
ed25519algorithm, use:ssh-keygen -t rsa -b 4096 -C "[email protected]"When prompted to "Enter a file in which to save the key," press Enter to accept the default file location. And when prompted to set a paraphrase, you can set a paraphrase for security reason. You have to remember this
-
Add your SSH key to the ssh-agent:
Start the ssh-agent in the background:
eval "$(ssh-agent -s)"
Add your SSH private key to the ssh-agent:
ssh-add ~/.ssh/id_ed25519or for RSA:
ssh-add ~/.ssh/id_rsa -
Add the SSH key to your GitHub account:
-
Copy the SSH key to your clipboard:
xclip -selection clipboard < ~/.ssh/id_ed25519.pub
or for RSA:
xclip -selection clipboard < ~/.ssh/id_rsa.pub
-
Install xclip if it's not installed
sudo apt install xclip
-
Go to GitHub and log in.
-
In the upper-right corner of any page, click your profile photo, then click Settings.
-
In the user settings sidebar, click SSH and GPG keys.
-
Click New SSH key or Add SSH key.
-
In the "Title" field, add a descriptive label for the new key.
-
Paste your key into the "Key" field.
-
Click Add SSH key.
-
Confirm your GitHub password, if prompted.
-
-
Test your SSH connection:
Run:
ssh -T [email protected]
You may see a message like this:
The authenticity of host 'github.com (IP ADDRESS)' can't be established. RSA key fingerprint is SHA256:....... Are you sure you want to continue connecting (yes/no)?Type
yesto continue.If your setup was successful, you should see a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Now, your local machine is set up to use SSH with GitHub!
Sometimes or should I say most of the time if you have cloned a repository using https such as git clone https://github.com/username/repo then git would still prompt you to Username for 'https://github.com': like this.
If Git is still prompting for a username and password, it's likely that your remote URL is using HTTPS instead of SSH. You need to change the remote URL to use the SSH protocol.
Here's how you can do that:
-
Check the current remote URL:
Open a terminal and navigate to your Git repository, then run:
git remote -v
You'll see something like this:
origin https://github.com/username/repo.git (fetch) origin https://github.com/username/repo.git (push) -
Change the remote URL to SSH:
Run the following command to change the remote URL to use SSH:
git remote set-url origin [email protected]:username/repo.git
Replace
usernamewith your GitHub username andrepowith your repository name. -
Verify the change:
Run the following command again to verify that the remote URL has been updated:
git remote -v
You should now see:
origin [email protected]:username/repo.git (fetch) origin [email protected]:username/repo.git (push) -
Push your changes:
Now, try pushing your changes again:
git push -u origin main
You should no longer be prompted for a username and password.
This should resolve the issue and allow you to push changes using SSH without being prompted for your GitHub credentials.
See the repo for details and
bashscript.