3

Github recommends the foolowing URL for setting a remote repo https://github.com/{USERNAME}/{PROJECTNAME}.git [When creating a new repo on Github].

But only when a different format - https://{USERNAME}@github.com/{USERNAME}/{PROJECTNAME}.git - I was able to work with a second account on the same git client [https://stackoverflow.com/a/27407168/7224430].

  1. What is the difference between both formats?
  2. What affect does adding {USERNAME}@ in front of github.com have?
Ben Carp
  • 24,214
  • 9
  • 60
  • 72

3 Answers3

1

It affects which cached password will be used.

An HTTPS URL can trigger credentials caching if your git cconfig credential.helper is set to manager-core

Adding username@ in your URL forces the helper to use the right username, helping it to retrieve the right password.

Wouldn't it make sense to use this URL format by default

Only if you have several accounts.
If not, it does not add much.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The Git config of the repository doesn't set `credential.helper`. Could it be that it is set by default to `manager-core`? – Ben Carp Jul 12 '21 at 07:41
  • I just checked `.gitconfig` in the user folder. `[credential]helper = store` – Ben Carp Jul 12 '21 at 07:45
  • Wouldn't it make sense to use this URL format by default? – Ben Carp Jul 12 '21 at 07:50
  • @BenCarp Store is the basic credential caching mechanism: https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage. A manager-core is safer, but might not be installed depending on your environment – VonC Jul 12 '21 at 07:51
  • 1
    @BenCarp If you have multiple users on the same workstation, or if you are working with multiple accounts, yes. If not, it is not necessary. – VonC Jul 12 '21 at 07:52
1

If you're trying to clone a repo without logging in, the below will prompt you for a password

git clone https://username@github.com/username/repository.git

If you check your gitconfig file, programfiles->git->etc->gitconfig

You could notice the credential helper set to manager-core

You can be accessing/cloning multiple GitHub accounts from your local. Using a username lets it use/cache the respective account credentials.

enter image description here

In CI/CD pipelines when we configure the first step where the code gets checked out from the remote repository, we have username and password picked from the credential manager/helper

git pull https://${GIT_USERNAME}:${GIT_PASSWORD}@github.myorg.com/myrepo.git

More or less, this is what happens in our system locally, where the credentials are picked from the cache based on the username.

In the case of ssh, when you specify git@github.com:username/repo.git, it simply tells git that the user is git itself and that it should go get the credentials (public key) from the ssh-agent for the host github.com.

You could also set the username for a specific host

[credential "https://example.com"]
 username = foo

Ref:- http://git-scm.com/docs/gitcredentials

Ref:- https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage

dee
  • 2,244
  • 3
  • 13
  • 33
-1

Perhaps you've noticed that sometimes git asks you for the password, that can be triggered either due to SSH key being encrypted or because of the HTTP protocol getting an unsuccessful response.

By issuing https://{USERNAME}@something you are actually complying with the protocol's shape for providing credentials(URL anatomy). In full it'd be:

protocol://username:password@domain:port/path?query=value&query2=value2#anchor

Therefore if you'd want, you could have provided a password even with a plaintext (and have it logged in a console history, which is not recommended).

The HTTP credentials are most likely triggered by one of these statuses:

  • 401 Unauthorized
  • 403 Forbidden
  • 407 Proxy Authenticate

which git checks and based on the *-Authenticate header then sends back to the git server when using HTTP protocol.

When not providing the credentials via URL git has to find another way to provide them (if required) e.g. by already mentioned credential helper.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90