Black Friday & Cyber Monday: more dev deals See the deals

Stop Putting Tokens in Your Git Remote URL (Do This Instead)

 Â· 5 min read  Â·

If your Git remote looks like https://[email protected]/you/repo.git, your GitHub token is sitting in plain text inside .git/config. Anyone who runs git remote -v on your machine sees it. Anyone you screen-share with sees it. Any script or backup that copies the repo folder copies it too.

I showed that exact setup in my post on updating your GitHub personal access token, because it’s the fastest fix when a token expires. It’s not where you want to stay. Git has had a proper place to keep credentials for years, and GitHub’s own docs recommend using it.

Here are the three options I’d pick from, and how to switch an existing repo over.

First, find the repos that have a token in them

Before fixing anything, see how bad it is. GitHub tokens start with ghp_ (classic) or github_pat_ (fine-grained), so you can grep for them. Point this at whatever folder holds your projects:

grep -lE "ghp_|github_pat_" ~/code/*/.git/config

Every file it prints is a repo with a token baked into its remote URL. For each one, reset the remote to the plain URL:

git remote set-url origin https://github.com/[USERNAME]/[REPO].git

set-url only changes the URL. Your branches and their upstream tracking stay exactly as they were.

Now pick one of the options below so Git has somewhere else to get your credentials.

Diagram comparing a token embedded in a Git remote URL with three safer options: an OS credential helper, the GitHub CLI, and an SSH key

Option 1: Let your OS credential helper remember it

Git doesn’t store passwords itself. It asks a credential helper, a small program that saves and retrieves credentials for it. Every major OS has one that uses the system’s secure storage.

On a Mac, you may already be set up without knowing it. Apple’s Git ships with the Keychain helper turned on. Check with:

git config --show-origin --get-all credential.helper

On my machine that prints osxkeychain, set in the system gitconfig that comes with Xcode’s command line tools. With that in place, the first time you push over HTTPS, Git prompts for a username and password. Enter your GitHub username and paste your token as the password. Keychain stores it, and Git won’t ask again until the token stops working.

On Windows, Git for Windows bundles Git Credential Manager (GCM), which saves credentials in Windows Credential Manager. GCM also runs on macOS and Linux. On a Mac you can install it with Homebrew if you want its browser-based GitHub sign-in instead of pasting tokens:

brew install --cask git-credential-manager

On Linux, either install GCM or use libsecret, which stores credentials in your desktop keyring.

Two built-in helpers come up a lot in Stack Overflow answers, and you should know what they actually do before you copy them. git config --global credential.helper store writes your credentials, unencrypted, to ~/.git-credentials. That moves the plain-text token from one file to another. cache keeps credentials in memory for 15 minutes by default, so you’ll keep getting prompted. Neither one is a real fix for a long-lived token.

Option 2: Use the GitHub CLI (my pick for most people)

If you already use gh, this is the least effort. Run:

gh auth login

Pick GitHub.com, choose HTTPS as your preferred protocol, and answer yes when it asks whether to authenticate Git with your GitHub credentials. You sign in through the browser, so there’s no token to create, copy, or paste. gh stores its credential in your system credential store and registers itself as Git’s credential helper for github.com.

If you logged in with gh before and skipped that prompt, you can wire it up afterward:

gh auth setup-git

To see what it changed, run git config --global --get-regexp credential. When the credential needs refreshing, you run gh auth login once and every repo on your machine picks it up. No more editing remotes one by one.

Option 3: Switch to SSH

SSH skips tokens entirely. Personal access tokens only work over HTTPS, so once your remote is an SSH URL, Git authenticates with a key pair instead. Generate a key:

ssh-keygen -t ed25519 -C "[email protected]"

On a Mac, add this to ~/.ssh/config so the key loads automatically and its passphrase lives in Keychain:

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

Then add the key to the agent:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Copy the public key (pbcopy < ~/.ssh/id_ed25519.pub), paste it into GitHub under Settings > SSH and GPG keys > New SSH key, and switch the remote:

git remote set-url origin [email protected]:[USERNAME]/[REPO].git

Test it with ssh -T [email protected]. GitHub should greet you by username.

SSH keys don’t expire on a schedule the way tokens do, which is either a feature or a risk depending on how you look at it. Use a passphrase. The gh auth login flow can also generate and upload a key for you if you choose SSH as the protocol.

Which one should you use?

If you live in the terminal and already have gh installed, use the GitHub CLI. It’s two prompts and you’re done, and token rotation stops being a chore.

If you don’t want another tool, the OS credential helper is fine. On a Mac it’s probably already on. Paste a fine-grained token once and move on.

Pick SSH if you work across a lot of machines or servers, or if you’d rather never think about token expiration again.

Any of the three beats a token in the URL.

Clean up the old token

If a token sat in a remote URL, treat it as exposed. Go to your token settings on GitHub, delete it, and create a new one if you still need it (or don’t, if you moved to gh or SSH).

If your credential helper still has the old token cached and pushes start failing, tell Git to forget it:

printf "protocol=https\nhost=github.com\n\n" | git credential reject

That asks every configured helper to erase what it has stored for github.com. Your next push or fetch prompts for fresh credentials.

This page may contain affiliate links. Please see my affiliate disclaimer for more info.

Related Posts

View All Posts »