28

I have configured Account A on my system with Global configurations and I can clone all my repos from there.

Now I don't want to change the configuration and I want to clone and do all operations of account B with my username and password. How can I do this?

I have tried:

git clone username:passwordgit@github.com:*****/******.git

But with no success.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Usman Afzal
  • 558
  • 1
  • 6
  • 13

2 Answers2

54

You can try with the complete https url:

git clone https://username:<token>@github.com/*****/******.git

If you omit the https:// part (and use ':' instead of '/'), it would be interpreted like an ssh url.

The GitHub help page "Which remote URL should I use?" confirms an https url can access private repos.

Note: I wouldn't put the token directly in the url, but use a credential manager to get the right password for the right user.

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

Reminder: since Aug. 2021 Token (or SSH key) authentication are required for all authenticated Git operations for GitHub.
Here, the token is a PAT (Personal Access Token).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    when two-factor authentication is enabled, you would need to generate a temporary access token https://help.github.com/articles/which-remote-url-should-i-use/#when-2fa-is-enabled – mission.liao Feb 24 '16 at 14:39
  • 1
    @mission.liao I agree. I describe PAT (Personal Access Token) in http://stackoverflow.com/a/18607931/6309 – VonC Feb 24 '16 at 15:51
  • @alhelal that should follow the same principle, so yes, it should work with Bitbucket as well. – VonC May 25 '18 at 05:03
17

Just to make the syntax a bit clearer, to clone a private repository use:

git clone https://[insert username]:[insert password]@github.com/[insert organisation name]/[insert repo name].git

or

git clone https://[insert username]@github.com/[insert organisation name]/[insert repo name].git

Example:

git clone https://myusername:mypassword@github.com/myorgname/myreponame.git
GraSim
  • 3,830
  • 1
  • 29
  • 35