Connecting to Remotes: HTTPS Tokens, Credential Helpers, SSH Keys and Proxies
How Git proves who you are to a server: HTTPS with personal access tokens and credential helpers, Git Credential Manager, SSH keys from generation to ssh-agent and ~/.ssh/config, several accounts on one machine, proxies, certificates, and fixing common authentication errors.
kvi5hnuCode Sprout team member10 min readOn this page
Public repositories can be cloned by anyone, but pushing, and reading private repositories, needs authentication. Git supports two families of transport for this: HTTPS, which uses a username with a token, and SSH, which uses a key pair. Hosting services support both. This article explains each, how to set it up once so you aren't asked repeatedly, and how to diagnose the errors people hit most.
The course's sandbox has no network, so the examples here that run show Git's own credential machinery. Commands that contact real servers are shown as plain code blocks.
HTTPS: tokens, not passwords
With an HTTPS URL, such as https://github.com/acme/trail-guide.git, Git asks for a username and password when the server requires them. Major hosting services no longer accept your account password for Git over HTTPS. Instead you use:
- a personal access token (PAT): a long random string you create in the service's settings, with chosen permissions (scopes) and an expiry date. You enter it where Git asks for a password. GitHub's fine-grained tokens can be limited to specific repositories and permissions, which is safer than broad classic tokens;
- or a browser sign-in handled by Git Credential Manager (below), which obtains and refreshes tokens for you with OAuth, including two-factor authentication.
Treat a token like a password: never commit it, never put it in a remote URL (https://user:token@github.com/... stores it in plain text in .git/config), and give it the shortest expiry and narrowest scope that works.
Credential helpers: entering it once
Without help, Git would ask for the token on every push. A credential helper stores or caches credentials and hands them to Git when needed:
| Helper | Where credentials live |
|---|---|
manager (Git Credential Manager) | the operating system's secure store. Supports browser sign-in, OAuth and two-factor authentication for GitHub, GitLab, Bitbucket and Azure DevOps. Installed with Git for Windows, and available for macOS and Linux |
osxkeychain | the macOS Keychain. The default for Git installed with Apple's tools or Homebrew |
libsecret | the GNOME keyring or KWallet on Linux |
cache | memory only, for a limited time (15 minutes by default). Nothing is written to disk |
store | a plain-text file, ~/.git-credentials. Simple, but anyone who can read the file has your token |
git config --global credential.helper manager # Windows, or GCM on macOS/Linux
git config --global credential.helper osxkeychain # macOS Keychain
git config --global credential.helper "cache --timeout=3600"
Git talks to helpers through a simple text protocol, which you can try directly. Here the store helper saves a credential and gives it back:
# "approve" tells the helper a credential worked, so it stores it
printf 'protocol=https\nhost=git.example.com\nusername=sam\npassword=example-token-123\n' |
git -c credential.helper=store credential approve
echo "=== what the store helper wrote (plain text!)"
cat ~/.git-credentials
echo "=== later, Git asks for host git.example.com and gets the stored values"
printf 'protocol=https\nhost=git.example.com\n' | git -c credential.helper=store credential fill
echo "=== reject: remove it, as Git does when a credential stops working"
printf 'protocol=https\nhost=git.example.com\nusername=sam\n' | git -c credential.helper=store credential reject
wc -c < ~/.git-credentials
=== what the store helper wrote (plain text!)
https://sam:example-token-123@git.example.com
=== later, Git asks for host git.example.com and gets the stored values
protocol=https
host=git.example.com
username=sam
password=example-token-123
=== reject: remove it, as Git does when a credential stops working
0
fill is what Git runs before connecting, approve after a credential worked, and reject after it failed. That's why an expired token is forgotten automatically: the next push fails, the helper drops it, and Git asks again.
Credentials can be configured per host, which matters when you use several services or accounts:
git config --global credential.https://github.com.username sam-lee
git config --global credential.https://git.example.com.helper store
git config --global credential.useHttpPath true # separate credentials per repository path
To change a stored token, for example after it expired or was revoked, run git credential reject with the host, as above. It works with every configured helper, including Git Credential Manager and the macOS Keychain. Alternatively, delete the github.com entry in Keychain Access on macOS or in Windows Credential Manager, or edit ~/.git-credentials for the store helper.
SSH: key pairs
SSH authenticates with a key pair: a private key that stays on your computer, and a public key you add to your account on the hosting service. Nothing secret is sent over the network. SSH URLs look like git@github.com:acme/trail-guide.git.
Creating a key
ls ~/.ssh # existing keys: id_ed25519.pub, id_rsa.pub...
ssh-keygen -t ed25519 -C "sam@example.com" # create a new key
- Ed25519 is the recommended key type today: short, fast and secure. Use
-t rsa -b 4096only for old servers that don't support it. - Accept the default file,
~/.ssh/id_ed25519, unless you need several keys. - Set a passphrase. It encrypts the private key on disk, so a stolen laptop or backup doesn't give away your access. The agent below means you type it rarely.
- The
-Ccomment only labels the key.
This creates two files: id_ed25519, the private key, which must never be shared, and id_ed25519.pub, the public key. Copy the public key's contents into the hosting service's SSH key settings:
cat ~/.ssh/id_ed25519.pub # copy this line into GitHub/GitLab/Bitbucket settings
The SSH agent
The ssh-agent holds your decrypted key in memory, so you enter the passphrase once per session:
eval "$(ssh-agent -s)" # start the agent (Linux, Git Bash)
ssh-add ~/.ssh/id_ed25519 # add the key; asks for the passphrase
ssh-add -l # list keys the agent holds
macOS runs an agent automatically and can keep the passphrase in the Keychain (ssh-add --apple-use-keychain, with UseKeychain yes in ~/.ssh/config). Windows has an OpenSSH agent service, and Git for Windows can use it. Password managers such as 1Password and Bitwarden can act as SSH agents too, keeping private keys out of ~/.ssh entirely.
Testing the connection
ssh -T git@github.com
# Hi sam-lee! You've successfully authenticated, but GitHub does not provide shell access.
The first connection to a host asks you to confirm its host key fingerprint. Compare it with the fingerprint the service publishes in its documentation before answering yes. It's then saved in ~/.ssh/known_hosts, and SSH warns loudly if it ever changes, which could mean someone is intercepting the connection, or that the service rotated its key (check its announcements).
~/.ssh/config
The SSH configuration file sets options per host, and lets you define aliases:
# ~/.ssh/config
Host github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
# A server on a non-standard port
Host gitserver
HostName git.example.com
Port 2222
User git
IdentityFile ~/.ssh/id_work
With that, git clone gitserver:team/app.git connects to git.example.com on port 2222 with the work key.
Several accounts on one machine
A common situation: a personal and a work account on the same hosting service. The service identifies you by the key or token, so you need a different one per account.
With SSH, define a host alias per account, each with its own key:
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_work
IdentitiesOnly yes
Then use the alias in the work repositories' URLs: git clone github-work:acme-corp/app.git, or git remote set-url origin github-work:acme-corp/app.git. IdentitiesOnly yes stops SSH from trying your other keys first, which would log you in as the wrong account.
Alternatively, choose the key per repository without aliases:
git config core.sshCommand "ssh -i ~/.ssh/id_work -o IdentitiesOnly=yes"
With HTTPS, Git Credential Manager can hold several accounts per host, and credential.<url>.username or credential.useHttpPath=true tells Git which one to use.
Also remember the commit identity: the author name and email in commits are separate from authentication. Use conditional includes (article 33) to set a work email for repositories under a work folder.
Proxies and certificates
Behind a company proxy, HTTPS traffic needs to go through it:
git config --global http.proxy http://proxy.example.com:8080
git config --global http.https://git.example.com.proxy "" # no proxy for this host
git config --global --unset http.proxy # remove it
Git also honours the https_proxy, http_proxy and no_proxy environment variables. Settings can be scoped to a URL, as above, and git config get --url=<url> http.proxy shows which value applies to a given URL:
git config --global http.https://git.example.com.proxy http://proxy.example.com:8080
echo "for git.example.com: $(git config get --url=https://git.example.com/acme/app.git http.proxy)"
git config get --url=https://github.com/acme/app.git http.proxy || echo "for github.com: no proxy configured"
for git.example.com: http://proxy.example.com:8080
for github.com: no proxy configured
SSH over a proxy uses SSH's own ProxyCommand or ProxyJump settings in ~/.ssh/config. Some networks block port 22. GitHub and GitLab accept SSH on port 443 too, at ssh.github.com and altssh.gitlab.com.
Companies that inspect HTTPS traffic use their own certificate authority, and Git reports SSL certificate problem: unable to get local issuer certificate. The fix is to trust that certificate, with git config --global http.sslCAInfo /path/to/company-ca.pem, or on Windows http.sslBackend=schannel to use the Windows certificate store. Don't set http.sslVerify=false: it switches off the protection against interception completely.
HTTPS or SSH?
| HTTPS | SSH | |
|---|---|---|
| Setup | nothing until the first push; then a token or browser sign-in | create a key and add it to your account |
| Firewalls | port 443, almost always open | port 22, sometimes blocked (443 alternatives exist) |
| Secrets | tokens, which expire and can be scoped per repository | keys, which don't expire unless you remove them |
| Multiple accounts | through the credential manager | through host aliases or core.sshCommand |
Both are secure. HTTPS with Git Credential Manager is the easiest start, especially on Windows. SSH is popular with developers who like one key for everything. CI systems typically use short-lived tokens or deploy keys, which are SSH keys limited to one repository.
Troubleshooting
| Error | Usual cause and fix |
|---|---|
Support for password authentication was removed | an account password was used over HTTPS. Use a token or Git Credential Manager |
Authentication failed / 403 | wrong or expired token, or missing scopes. Remove the stored credential (git credential reject, or the helper's own tool) and try again |
Repository not found on a private repository | the service hides private repositories from people without access. Often the wrong account's credentials are being used |
Permission denied (publickey) | the server doesn't accept any key you offered. Check ssh -T git@github.com, ssh-add -l, and that the public key is on the right account |
Host key verification failed | the host isn't in known_hosts, or its key changed. Check the published fingerprint before accepting |
SSL certificate problem | a company proxy's certificate isn't trusted. Set http.sslCAInfo, don't disable verification |
For a detailed log of what's happening:
GIT_TRACE=1 GIT_TRACE_CURL=1 git fetch # HTTPS: shows requests and headers
GIT_SSH_COMMAND="ssh -v" git fetch # SSH: shows which keys are tried
ssh -vT git@github.com
Git redacts authorization headers and cookies in GIT_TRACE_CURL output by default, but it can still include secrets, such as tokens in URLs, so check it before sharing it.
What's current
- Hosting services require tokens (fine-grained where available) or SSH keys, and many require two-factor authentication on accounts.
- Git Credential Manager is the recommended helper on all platforms, with browser-based sign-in.
- Ed25519 is the recommended SSH key type. Hardware security keys (FIDO2,
ssh-keygen -t ed25519-sk) are supported by GitHub and GitLab. - SSH keys can also sign commits (article 39).
Common questions
Git keeps asking for my username and password. How do I stop it?
Configure a credential helper: manager (Git Credential Manager), osxkeychain on macOS, or cache. Or switch the remote to SSH with a key loaded in the agent.
I changed my token. Why does Git still use the old one?
The helper still has the old one stored. Remove it with git credential reject (feed it protocol=https and host=github.com lines, as shown above), or delete the entry in Keychain Access or Windows Credential Manager. The next push asks for the new one.
Is it safe to use credential.helper store?
It writes tokens in plain text to ~/.git-credentials. On a personal, encrypted machine with a short-lived token it may be acceptable. Elsewhere, use a secure store such as Git Credential Manager or the OS keychain.
Should my SSH key have a passphrase? Yes. With an agent, you type it once per session, and a copied private key file is useless without it.
How do I switch an existing clone from HTTPS to SSH?
git remote set-url origin git@github.com:acme/trail-guide.git (article 17). Or rewrite all GitHub URLs with url."git@github.com:".insteadOf "https://github.com/".
Summary
- HTTPS uses a username and a token (or a browser sign-in through Git Credential Manager), never your account password.
- Credential helpers remember credentials:
manager,osxkeychain,libsecretandcacheare secure choices, andstorewrites plain text. - SSH uses a key pair: create one with
ssh-keygen -t ed25519, protect it with a passphrase, load it intossh-agent, and add the public key to your account. Test withssh -T git@github.com. ~/.ssh/confighost aliases,IdentitiesOnlyandcore.sshCommandhandle several accounts.- Configure proxies with
http.proxy(scoped per URL if needed), and trust company certificates withhttp.sslCAInfoinstead of disabling verification. GIT_TRACE,GIT_CURL_VERBOSEandssh -vshow what's going wrong.
Practice questions
A push over HTTPS fails with "Support for password authentication was removed". What do you do?
Answer
Create a personal access token on the hosting service, with permission to write to the repository, and enter it instead of your password when Git asks. Better, install or enable Git Credential Manager (credential.helper manager), which signs in through the browser and stores the token securely. Or switch the remote to SSH.
You have personal and work accounts on GitHub. Work repositories keep being pushed as your personal account over SSH. How do you fix it?
Answer
Create a separate key for work, add its public key to the work account, and define a host alias in ~/.ssh/config:
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_work
IdentitiesOnly yes
Then point the work repositories at the alias: git remote set-url origin github-work:acme-corp/app.git. IdentitiesOnly yes makes SSH offer only that key.
On a company network, cloning over HTTPS fails with "SSL certificate problem: unable to get local issuer certificate". Why is http.sslVerify=false the wrong fix, and what's the right one?
Answer
Turning off verification means Git accepts any certificate, so anyone between you and the server could read or change your traffic, including tokens. The error usually means the company's inspecting proxy uses its own certificate authority. Get that CA certificate from IT and trust it: git config --global http.sslCAInfo /path/to/company-ca.pem, or on Windows use http.sslBackend=schannel.
Previous: Pushing: Upstream Branches, push.default, Force-With-Lease, Deleting and Refspecs
Next: Git Workflows: Feature Branches, Pull Requests, GitHub Flow, Gitflow, Forking and Trunk-Based

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.