DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

Switching from SSH to HTTPS Authentication on GitHub

Introduction

GitHub allows users to authenticate and interact with repositories using either SSH or HTTPS. While SSH provides a secure key-based authentication method, some users may encounter issues like Permission denied (publickey), especially when SSH keys are not properly configured. In such cases, switching to HTTPS authentication can be a simpler alternative.

This article provides step-by-step instructions on how to switch from SSH to HTTPS authentication when working with GitHub repositories.

1. Check Your Current Remote URL

Before switching to HTTPS, check whether your Git repository is currently using SSH by running the following command in your terminal:

git remote -v
Enter fullscreen mode Exit fullscreen mode

If the output looks like this:

origin  git@github.com:your-username/repository-name.git (fetch)
origin  git@github.com:your-username/repository-name.git (push)
Enter fullscreen mode Exit fullscreen mode

Then your repository is using SSH authentication.

2. Change the Remote URL to HTTPS

To switch from SSH to HTTPS, update the remote URL using the following command:

git remote set-url origin https://github.com/your-username/repository-name.git
Enter fullscreen mode Exit fullscreen mode

Replace your-username and repository-name with your actual GitHub username and repository name.

3. Verify the Change

After updating the remote URL, confirm that it has been successfully changed:

git remote -v
Enter fullscreen mode Exit fullscreen mode

You should now see an output similar to this:

origin  https://github.com/your-username/repository-name.git (fetch)
origin  https://github.com/your-username/repository-name.git (push)
Enter fullscreen mode Exit fullscreen mode

This confirms that your repository is now using HTTPS authentication.

4. Authenticate Using a Personal Access Token (PAT)

When using HTTPS, GitHub requires authentication for push and pull actions. Instead of using your password (which is no longer supported for Git operations), you must use a Personal Access Token (PAT).

Generating a Personal Access Token

  1. Go to GitHub β†’ Settings β†’ Developer Settings β†’ Personal Access Tokens πŸ‘‰ GitHub Token Settings
  2. Click "Generate new token" (or "Generate new token (fine-grained)" for more control).
  3. Select the scopes (permissions) you need. For Git operations, check:
    • repo (for private repositories)
    • workflow (for GitHub Actions, if needed)
  4. Click "Generate token" and copy it immediately (as it won’t be shown again).

Using the Token for Authentication

When pushing or pulling changes, Git will prompt for your username and password. Enter:

  • Username: Your GitHub username
  • Password: The generated Personal Access Token (not your actual GitHub password)

To avoid entering the token every time, you can cache your credentials:

git config --global credential.helper cache
Enter fullscreen mode Exit fullscreen mode

Or store them permanently (recommended for personal machines):

git config --global credential.helper store
Enter fullscreen mode Exit fullscreen mode

5. Test Your Connection

To verify that everything is set up correctly, try pushing or pulling from the repository:

git push origin main
Enter fullscreen mode Exit fullscreen mode

If everything is configured correctly, the operation should complete without authentication errors.

Conclusion

Switching from SSH to HTTPS authentication in GitHub can help resolve issues related to SSH key misconfiguration while still providing a secure and straightforward way to manage repositories. By using Personal Access Tokens, you can ensure secure authentication without relying on SSH keys.

If you frequently switch between devices, HTTPS authentication with a stored token can be a convenient alternative to SSH.


Troubleshooting

  • If authentication still fails, ensure that you are using the correct Personal Access Token and that it has the required repository access.
  • If Git keeps asking for credentials, try clearing stored credentials using:
git credential reject https://github.com
Enter fullscreen mode Exit fullscreen mode

Then try pushing again, and re-enter your username and token.

Top comments (0)