5

I have a GitHub handle that I've used for years, however, after joining a new company, they required that I register a new GitHub account under the company email address. Now I can't clone the private repo because the user.name is set as my normal GitHub handle.

I read on this page that I can add my username before the URL like git clone https://usernamenew@github.com/companyname/repo-name.git.

However, I get the error

Cloning into 'repo-name'...
remote: Repository not found.
fatal: repository 'https://usernamenew@github.com/companyname/repo-name.git/' not found

How do I clone the company's repo using a different user name?

xiaodai
  • 13,509
  • 17
  • 70
  • 120

2 Answers2

4

Based on your example output, git is not asking you for a password. It looks like:

  • Your credentials are cached
  • The credentials are for your personal account, not your new company account

You have correctly added your new username to the URL. But by default, git only cares about the host when applying cached credentials.

You can use the useHttpPath option of gitcredentials to tell git to consider the URL path, as well. This will allow you to use different accounts with different repositories.

https://git-scm.com/docs/gitcredentials.html#Documentation/gitcredentials.txt-useHttpPath

useHttpPath

By default, Git does not consider the "path" component of an http URL to be worth matching via external helpers. This means that a credential stored for https://example.com/foo.git will also be used for https://example.com/bar.git. If you do want to distinguish these cases, set this option to true.

Configure this by running the following command:

git config --global credential.https://github.com.useHttpPath true
Rob Bajorek
  • 5,962
  • 7
  • 44
  • 49
1

The config user.name has nothing to do with authentication.

Do check you git config credential.helper setting.
If set, that means your credentials might be cached: you would need to remove them or, better, use an SSH URL (one where you can set a different private key for different URL)

If this is not a cached issue, simply check that usernamenew does have access to github.com/companyname/repo-name.git.

VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755