Skip to content

Git Authentication: SSH vs HTTPS

46 people viewed this

Git Authentication: SSH vs HTTPS

Git Authentication: SSH vs HTTPS

When you clone, pull, or push code to a remote Git repository, Git needs a way to verify that you have permission to access that repository.

This process is called Git authentication.

The two most common ways to connect to remote repositories are:

  • SSH
  • HTTPS

Both methods can securely authenticate Git operations, but they use different credentials and workflows.

In this article, we will understand how SSH and HTTPS authentication work, how to configure them, their differences, common errors, and which approach fits different development environments.

What Is Git Authentication?

Git authentication is the process of proving to a remote Git hosting service that you are authorized to perform an operation on a repository.

For example, when you run:

git push origin main

the remote server needs to determine whether your credentials have permission to push to that repository.

Depending on the remote URL, Git may authenticate using SSH keys or HTTPS credentials such as a personal access token and a credential helper.

Modern Git hosting platforms generally do not use your normal account password directly for Git operations over HTTPS. For example, GitHub requires a personal access token or another supported authentication mechanism for command-line HTTPS access.

How Does Git Know Whether to Use SSH or HTTPS?

The answer is in the repository's remote URL.

Check it using:

git remote -v

An HTTPS remote may look like:

https://github.com/username/project.git

An SSH remote may look like:

git@github.com:username/project.git

The protocol in the remote URL determines how Git connects to the hosting service.

What Is Git HTTPS Authentication?

HTTPS authentication uses an HTTPS remote URL to communicate with the Git hosting service.

Example:

git clone https://github.com/username/project.git

When authentication is required, the hosting provider can use a token-based credential rather than your normal account password.

Git can also use a credential helper so you do not have to enter credentials repeatedly.

What Is a Personal Access Token?

A personal access token, commonly called a PAT, is a credential that can be used instead of a traditional password for supported Git operations over HTTPS.

For example, GitHub documents HTTPS command-line authentication using a personal access token when Git asks for a password.

A token can also have limited permissions depending on the hosting provider and token type.

A token should be treated like a password or secret and should never be committed to your repository.

GitHub Authentication Using HTTPS

Clone a GitHub repository using HTTPS:

git clone https://github.com/username/project.git

When Git requests credentials, use your GitHub username where requested and your personal access token as the password.

You can also use Git Credential Manager to securely manage supported credentials instead of manually entering them repeatedly.

What Is a Git Credential Helper?

A credential helper allows Git to obtain and manage credentials without requiring you to manually enter them for every operation.

Check your configured credential helper:

git config --global credential.helper

Git supports several credential helpers, including helpers that integrate with operating-system credential storage.

Git Credential Manager is another commonly used option, especially on Windows.

Should You Use git credential-store?

Be careful with:

git config --global credential.helper store

The Git documentation notes that the credential-store helper saves credentials unencrypted on disk.

Therefore, it should not be treated as the default secure solution for sensitive credentials.

Prefer a credential helper that integrates with secure operating-system storage when possible.

What Is Git SSH Authentication?

SSH authentication uses an SSH key pair instead of entering a username and password for each Git operation.

An SSH key pair consists of:

  • Private key – kept securely on your computer
  • Public key – added to your Git hosting account

Never share your private SSH key.

The basic SSH workflow looks like this:

Your Computer
     |
     | Private Key
     |
     ↓
SSH Authentication
     |
     ↓
GitHub / Bitbucket
     |
     | Public Key
     ↓
Repository Access

How to Generate an SSH Key

Modern systems commonly support Ed25519 keys.

ssh-keygen -t ed25519 -C "your-email@example.com"

Follow the prompts and choose a secure passphrase for the private key when appropriate.

The generated files commonly include:

~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub

The file ending in .pub is the public key.

What Is ssh-agent?

ssh-agent can keep your loaded private key available to SSH operations.

Start an agent when necessary:

eval "$(ssh-agent -s)"

Add your private key:

ssh-add ~/.ssh/id_ed25519

This can prevent you from repeatedly entering the SSH key passphrase during Git operations.

How to Add an SSH Key to GitHub

First display your public key:

cat ~/.ssh/id_ed25519.pub

Copy the complete public key and add it to your GitHub account's SSH key settings.

The private key remains on your computer.

Test the connection:

ssh -T git@github.com

A successful authentication response indicates that GitHub recognized your SSH key.

Git Authentication with Bitbucket

Bitbucket Cloud also supports SSH and HTTPS-based Git workflows.

An SSH clone URL looks like:

git clone git@bitbucket.org:workspace/repository.git

An HTTPS clone URL looks like:

git clone https://username@bitbucket.org/workspace/repository.git

Bitbucket Cloud documents personal SSH keys as a method for everyday Git CLI usage. Its HTTPS workflows can use Git Credential Manager or token-based credentials depending on the account and use case.

SSH vs HTTPS: What Is the Difference?

SSH HTTPS
Uses SSH key authentication Uses HTTPS credentials
Private/public key pair Token and/or credential manager
Usually convenient for frequent Git CLI usage Easy to start with and commonly works through proxies
Requires SSH key setup May require token/credential-helper setup
Private key stays on your machine Credential handling depends on your helper/provider
Can be protected with a passphrase Can use secure credential storage

SSH vs HTTPS Clone Example

HTTPS

git clone https://github.com/username/project.git

SSH

git clone git@github.com:username/project.git

Both commands clone the same repository if the authenticated account has the required permissions.

How to Change a Git Remote from HTTPS to SSH

Suppose your current remote is HTTPS:

git remote -v

Change it to SSH:

git remote set-url origin git@github.com:username/project.git

Verify the change:

git remote -v

Your repository will now use SSH for future communication with that remote.

How to Change Git Remote from SSH to HTTPS

If your repository currently uses SSH:

git remote -v

Change it to HTTPS:

git remote set-url origin https://github.com/username/project.git

Verify:

git remote -v

How Git Authentication Works

SSH Flow

git push
   ↓
SSH connection
   ↓
SSH key authentication
   ↓
Remote verifies key
   ↓
Repository permission check
   ↓
Push allowed

HTTPS Flow

git push
   ↓
HTTPS connection
   ↓
Credential / token authentication
   ↓
Remote verifies credential
   ↓
Repository permission check
   ↓
Push allowed

How to Use Multiple Git Accounts with SSH

Developers sometimes need separate Git accounts for personal and company repositories.

For example:

~/.ssh/github_personal
~/.ssh/github_work

You can configure different SSH hosts in:

~/.ssh/config

Example:

Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_personal

Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_work

You can then use a custom host alias in your Git remote:

git remote set-url origin git@github-work:company/project.git

Common SSH Authentication Errors

1. Permission denied (publickey)

Permission denied (publickey).

Common causes include:

  • SSH key was not added to the hosting account
  • Wrong private key is being used
  • SSH agent does not have the key loaded
  • Incorrect SSH configuration
  • Repository permissions are missing

Check loaded keys:

ssh-add -l

Test GitHub SSH:

ssh -T git@github.com

Common HTTPS Authentication Errors

Authentication Failed

remote: Authentication failed

Possible causes include:

  • Invalid or expired token
  • Incorrect username
  • Insufficient token permissions
  • Incorrect stored credentials
  • Credential helper contains outdated credentials
  • Repository access has been removed

How to Check Which Authentication Method You Are Using

First check your remote:

git remote -v

If you see:

https://...

you are using HTTPS.

If you see:

git@github.com:...

you are using SSH.

Why Can HTTPS Work Better Behind Some Firewalls?

HTTPS commonly uses port 443, which is widely permitted by corporate networks and proxies.

SSH normally uses port 22, which can be blocked by some networks.

GitHub also documents an option for using SSH over port 443 when standard SSH connectivity is blocked and the environment permits it.

Git Authentication Security Best Practices

  1. Never commit private SSH keys.
  2. Never commit personal access tokens.
  3. Use a passphrase for important SSH keys.
  4. Use limited-scope tokens when the provider supports them.
  5. Use secure credential storage instead of plaintext credential files.
  6. Remove or revoke credentials that are no longer required.
  7. Do not share your SSH private key or personal token.
  8. Use separate credentials for different environments when appropriate.

SSH vs HTTPS for CI/CD

Authentication requirements are different in automated environments such as CI/CD pipelines.

For example, a pipeline may need to clone a private repository without interactive user input.

Possible approaches include:

  • Deploy keys
  • Repository or project access tokens
  • CI/CD platform credentials
  • SSH keys managed as protected secrets

The correct choice depends on the hosting provider and whether the automation needs read-only or write access.

Avoid putting secrets directly into repository URLs that can appear in logs or configuration output.

Example: Git Authentication for a Laravel Project

Suppose you have a Laravel project stored in a private Bitbucket repository.

Using SSH:

git clone git@bitbucket.org:workspace/pharmacy-erp.git

After making changes:

git add .
git commit -m "Fix invoice calculation"
git push origin main

With SSH configured correctly, Git can authenticate using the SSH key rather than asking for an HTTPS credential during each operation.

When Should You Use SSH?

SSH is often convenient when:

  • You use Git from the terminal frequently
  • You work with private repositories regularly
  • You use multiple repositories on the same machine
  • You want key-based authentication
  • You work with servers or deployment environments

When Should You Use HTTPS?

HTTPS can be convenient when:

  • You are working behind a restrictive firewall or proxy
  • Your organization uses a credential manager
  • You want browser-based authentication through supported tooling
  • You are working on a temporary development machine
  • Your organization's workflow is based around HTTPS credentials

SSH vs HTTPS: Quick Comparison

Feature SSH HTTPS
Authentication SSH key pair Token / credential helper
Setup Requires SSH key configuration Usually simpler initially
Frequent Git CLI use Convenient Convenient with credential manager
Firewall compatibility Can be blocked in some environments Often works through HTTP/HTTPS infrastructure
Credential type Private/public key Token or managed credential
Security Strong when keys are protected properly Strong when tokens and credential storage are handled properly

Git Authentication Interview Questions

  1. What is Git authentication?
  2. What is the difference between SSH and HTTPS in Git?
  3. How does SSH authentication work?
  4. What is an SSH key pair?
  5. What is the difference between a public key and a private key?
  6. Why should you never share a private SSH key?
  7. What is a personal access token?
  8. Why can you not normally use your GitHub password for Git HTTPS authentication?
  9. What is Git Credential Manager?
  10. What is a Git credential helper?
  11. How do you check whether a repository uses SSH or HTTPS?
  12. How do you change an HTTPS remote to SSH?
  13. How do you change an SSH remote to HTTPS?
  14. What does "Permission denied (publickey)" mean?
  15. How do you test a GitHub SSH connection?
  16. How do you check whether an SSH key is loaded into ssh-agent?
  17. What are the security risks of storing Git credentials in plaintext?
  18. How would you authenticate a private repository in CI/CD?
  19. Why might HTTPS work when SSH does not?
  20. How would you configure multiple Git accounts using SSH?

Interview Scenario: SSH or HTTPS?

An interviewer may ask:

"You have a private Git repository. Would you use SSH or HTTPS?"

A good technical answer is that both are valid secure approaches when configured correctly. The choice depends on the environment, credential management, network restrictions, automation requirements, and team standards.

For frequent Git CLI usage, SSH keys can provide a convenient key-based workflow. HTTPS can be particularly useful when an organization already uses credential managers or when network restrictions make SSH inconvenient.

Important Git Authentication Commands

Command Purpose
git remote -v View remote URLs
git remote set-url origin ... Change remote URL
ssh-keygen -t ed25519 Generate an SSH key
ssh-add ~/.ssh/id_ed25519 Add key to SSH agent
ssh-add -l List loaded SSH keys
ssh -T git@github.com Test GitHub SSH authentication
git config --global credential.helper Check credential helper

Conclusion

Git authentication allows Git to securely communicate with private and protected remote repositories.

SSH uses a public/private key pair and is commonly convenient for developers who frequently use Git from the command line.

HTTPS uses HTTPS-based credentials and can work well with personal access tokens and credential managers, particularly in environments where HTTPS connectivity is easier to maintain.

The important thing is not simply memorizing SSH and HTTPS commands. You should understand how credentials are stored, how remote URLs determine the connection method, how to troubleshoot authentication failures, and how to protect your credentials.

Quick Revision

# Check remote
git remote -v

# HTTPS remote
https://github.com/username/project.git

# SSH remote
git@github.com:username/project.git

# Generate SSH key
ssh-keygen -t ed25519 -C "your-email@example.com"

# Add SSH key
ssh-add ~/.ssh/id_ed25519

# Test GitHub SSH
ssh -T git@github.com

# Change HTTPS to SSH
git remote set-url origin git@github.com:username/project.git

# Change SSH to HTTPS
git remote set-url origin https://github.com/username/project.git

# Check credential helper
git config --global credential.helper

 

Was this useful?

No ratings yet — yours can be the first.

Discussion

Notes from readers. Comments appear after a short review.

No comments yet. Start the thread below.