How can I switch Git credentials or “log in” as a different user without changing config variables?

I need to push to a different GitHub repository, but Git is still using credentials from an old account I plan to delete. Both repositories belong to me, but when I try to push, I get the following error:

remote: Permission to current_user/fav-front.git denied to user_to_delete. fatal: unable to access ‘https://github.com/current_user/repo.git/’: The requested URL returned error: 403

I know that user.name and user.email in Git config only affect commits, not actual authentication for pushing. I’m not looking to permanently update any Git config variables, I just want to log in once using my current GitHub credentials for this session.

I’ve run into this when switching between my work and personal GitHub accounts. A super clean trick to avoid messing with config variables is to embed your GitHub personal access token (PAT) directly into the clone URL like this:

git clone https://<USERNAME>:<TOKEN>@github.com/current_user/repo.git

If you’ve already cloned the repository, just update the remote URL like this:

git remote set-url origin https://<USERNAME>:<TOKEN>@github.com/current_user/repo.git

This approach avoids credential caching, letting you push as the correct user just for that session. Git won’t store the credentials unless you’re using a credential manager, so it’s a quick, session-based fix.

I’ve definitely encountered the same issue, especially since Git can cache credentials and continue using old ones, even with a new remote URL. Here’s how I handle it:

On Windows:

cmdkey /delete:git:https://github.com

On macOS:

Just open Keychain Access and remove any GitHub or git: entries.

Then, next time you push or pull, Git will prompt you for the new credentials. This way, Git uses the latest credentials without touching your Git config, and you stay session-based.

When I wanted a one-off Git login and didn’t want to touch any config files or saved credentials, I found this trick to be super effective:

GIT_ASKPASS=git-gui git push origin main

This forces Git to prompt you for your credentials via GUI. Alternatively, if you’re comfortable with the terminal, you can unset the credential helper temporarily:

git config --local credential.helper ""
git push

This will ask for your username and token, but it won’t save them afterward—perfect if you’re switching users just for that one session.