Check Your Git Remotes for GitHub Tokens

Nov 22, 2026

Search for how to clone a private GitHub repo over HTTPS and you'll find plenty of answers that boil down to this:

git clone https://ghp_xxxxxxxxxxxx@github.com/owner/repo.git

It works, and that's the problem. What they usually skip is where the token goes afterward. Git saves the remote URL, token and all, to .git/config in plain text. It stays there long after you've forgotten about it, and it shows up any time you run git remote -v, paste terminal output into a support thread, or share your screen.

I went looking for this pattern across my own machine, and it's worth five minutes to check yours.

Finding Them

This loop checks the origin remote of every repo one and two folders below where you are. It prints the folder name rather than the URL, so you don't leak the token into your scrollback while hunting for it:

for d in */ */*/; do
  url=$(git -C "$d" remote get-url origin 2>/dev/null) || continue
  case "$url" in *//*@*) echo "Token in remote: $d" ;; esac
done

It flags anything with an @ between // and the host. Plain SSH remotes (git@github.com:...) won't match. An ssh://git@... remote will, and you can ignore it.

Removing Them

For each flagged repo, set the remote back to the plain URL:

git -C path/to/repo remote set-url origin https://github.com/owner/repo.git

Then revoke the token in GitHub under Settings → Developer settings → Personal access tokens and make a new one if you still need it. It's been sitting in a text file, so retiring it is cheap insurance.

Making Sure It Doesn't Happen Again

Tokens end up in URLs because git needs some way to authenticate over HTTPS, and pasting one into the URL is the path of least resistance. The GitHub CLI gives git a better one:

gh auth login
gh auth setup-git

setup-git registers gh as git's credential helper for github.com. Plain https://github.com/... URLs then work on their own, and the token lives in your system keychain instead of a config file. You can confirm it took:

$ git config --global --get-all credential.https://github.com.helper

!/opt/homebrew/bin/gh auth git-credential

(The empty first line is normal. It clears any helper set earlier so gh is the only one git asks.)

If you ever need to show someone a remote URL and you're not certain it's clean, pipe it through sed -E 's#//[^@]*@#//#' first. It strips anything between // and @.

Thanks for reading! I'd love to hear your thoughts - if you have something you want to share feel free to leave a comment or shoot me an email.

Comments

Leave a Comment