Skip to content

Authentication for Command Line Git

RubyRaven6 edited this page Dec 15, 2024 · 5 revisions

Authentication for Command Line Git

RavePossum

So you tried to push some code to GitHub, and it's refusing to connect because you're using a username and password?

You'll need to configure authentication. Unlike GitHub Desktop, command line git will not allow you to use your username and password for authentication. You'll need to set up an alternative method. Here I'll cover using SSH keys. You can also use a personal access token, which GitHub covers in detail here.

I'm going to assume you're using WSL, but these steps should be very similar for Linux in general.

At a high level, these are the steps:

  1. Set up new SSH keys in WSL
  2. Add the public SSH key to your GitHub account
  3. Change your remote URL to use SSH

Set up new SSH keys in WSL

Replace your_email@example.com with the email address you use for GitHub, and then run the following command in your WSL shell:

ssh-keygen -t ed25519 -C "your_email@example.com"

When you're prompted to "Enter a file in which to save the key", you can press Enter to accept the default file location.

Next, you'll be prompted to enter a secure passphrase. It's a good idea to do so because it ensures that your account can't be compromised if someone has access to your computer. Note that this will require you to enter the passphrase whenever you push. Alternatively, you can leave the passphrase blank, but do this at your own risk.

If all goes well, you've now successfully generated new SSH keys.

**IMPORTANT: The private key generated by this (the contents of the ~/.ssh/id_ed25519 file without .pub at the end) is sensitive information and should NOT be shared. You should protect it like a password.**

Add the public SSH Key to your GitHub account

First we'll need to copy the public SSH key to our clipboard to paste in later. To do so, run the following command in your WSL shell:

clip.exe < ~/.ssh/id_ed25519.pub

If you're on macOS, the command is as follows

pbcopy < ~/.ssh/id_ed25519.pub

Now go to github.com, log in, click your profile picture at the top right, and then select "Settings" in the list.

image

Next, select "SSH and GPG Keys" from the list of settings on the left.

image

Select "New SSH Key".

image

Give your key a descriptive title (e.g. "YourName's laptop" or something), select "Authentication Key" from the drop-down, and paste in the value we copied from the command line earlier.

Finally, simply click "Add SSH key".

Aside - testing your connection

To test your connection to GitHub, run the following command:

ssh -T git@github.com

The first time you do so, it will prompt you to verify that GitHub's fingerprint matches one of its public fingerprints. If it matches, you're good to type yes and hit Enter.

If you're successfully connected, you'll see the following output:

> Hi USERNAME! You've successfully authenticated, but GitHub does not
> provide shell access.

Change your remote URL to use SSH

If you've previously connected to your GitHub repository without SSH, you'll probably see something like this when you type git remote -v

$ git remote -v
origin  https://github.com/ravepossum/Team-Aquas-Asset-Repo (fetch)
origin  https://github.com/ravepossum/Team-Aquas-Asset-Repo (push)

This means your remote is configured to use HTTPS to connect. You'll need to change it to use SSH.

SSH URLs follow this pattern:

git@github.com:USERNAME/REPONAME

Where USERNAME is your GitHub username, and REPONAME is the name of the repository.

Using the example output above, I would rewrite my URL to the following:

git@github.com:ravepossum/Team-Aquas-Asset-Repo

To change the URL, you just run the following command:

git remote set-url <remote name> <new url>

Continuing the previous example, I would run the following command:

git remote set-url origin git@github.com:ravepossum/Team-Aquas-Asset-Repo

Now the remote connection is configured to use SSH instead of HTTPS, and you can successfully push to your GitHub repository with your new SSH keys.

You'll need to change the URL for each unique repository you plan to push code to (or private repos you plan to pull from). You can continue to use standard HTTPS URLs for public remote repos you don't intend to push code to.

When you're cloning or adding new remotes going forward, you can just grab the SSH URL instead of the HTTPS one by selecting SSH here:

image

(this is based on GitHub's documentation for SSH)

Clone this wiki locally