There are three different ways you can do this, depending on your particular goals and circumstances:
- Put a username in the URL.
- Enable
useHttpPath
.
- Switch to SSH on port
443
.
Add your username in the URL
On GitHub, you can add a username to the HTTPS URL of your remote:
State |
Remote URL |
Before |
https://github.com/path/repo.git |
After |
https://JerryGoyal@github.com/path/repo.git |
Since the the part of the URL used for comparison is different, you can add both your work and personal forks as remotes for the same repository if you want to. The technique works on many repository hosts, but some may not support it.
1 SourceHut only allows Git access over SSH.
Set useHttpPath
for the host
Git Credential Manager can select a credential based on the full URL, rather than sharing them by hostname.
Tells Git to pass the entire repository URL, rather than just the hostname, when calling out to a credential provider. (This setting comes from Git itself, not GCM.)
Defaults to false
.
This second method does not work if you want to use multiple remote credentials for the exact same remote. But if you have different URLs for separate remotes, it should work fine, even from a single local repository.
All repositories on the remote host
This is often the most useful mode for useHttpPath
. To set the option for all GitHub remote URLs, run this:
git config --global credential.https://github.com.useHttpPath true
Git for Windows enables useHttpPath
for Azure Repos right out of the box. You can see it for yourself in etc/gitconfig
in your install location.
[credential "https://dev.azure.com"]
useHttpPath = true
Just one repository on the remote host
As rjmunro notes, you can drop --global
to use the path for only the current repository. If so, you may as well drop the hostname, too:
git config credential.useHttpPath true
Then, repositories for the host will default to a shared host-credential, but this repository will use the full URL path for saving and loading its credential.
All repositories on your computer
Technically, you can force yourself to separately add credentials for every single remote repository. That's needlessly verbose, but this answer shows how:
git config --global credential.useHttpPath true
What I really want is ssh
, but Port 22 is blocked
If you're only asking about HTTPS because you have a firewall problem outside of your control, you might be interested in my answer to a similar question for using Port 443 for certain Git providers that support it.