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
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)
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
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
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)
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
- Go to GitHub β Settings β Developer Settings β Personal Access Tokens π GitHub Token Settings
- Click "Generate new token" (or "Generate new token (fine-grained)" for more control).
- Select the scopes (permissions) you need. For Git operations, check:
-
repo
(for private repositories) -
workflow
(for GitHub Actions, if needed)
-
- 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
Or store them permanently (recommended for personal machines):
git config --global credential.helper store
5. Test Your Connection
To verify that everything is set up correctly, try pushing or pulling from the repository:
git push origin main
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
Then try pushing again, and re-enter your username and token.
Top comments (0)