Clone A Private Repository (Github)

440,423

Solution 1

Private clone URLs take the form [email protected]:username/repo.git - perhaps you needed to use git@ rather than git://?

git:// URLs are read only, and it looks like private repos do not allow this form of access.

Solution 2

This worked for me:

git clone https://[email protected]/username/repo_name

Solution 3

I have met this issue several times and every time I landed on this page, tried every thing and failed!

It's because I have 2FA enabled!!!

According to https://help.github.com/articles/which-remote-url-should-i-use/#when-2fa-is-enabled

If you have enabled two-factor authentication, or if you are accessing an organization that uses SAML single sign-on, you must provide a personal access token instead of entering your password for HTTPS Git.

  1. Follow this link and create an access token
  2. git clone https://github.com/username/repo_name.git (The default git repo link is good enough!)
  3. Enter your username and use the access token as password!!

Update:

  • If you don't mind exposing your access token in the command line, you can also paste the access token as username then hit enter enter (no need for password).
  • Hate copy&pasting the access token over and over again?
    Use git config credential.helper store (don't do this on machine you don't trust)

Solution 4

This worked for me:

git clone https://username:[email protected]/username/repo_name.git

Solution 5

Apr, 2022 Update:

You can clone a private repository from your account and you can also clone a private repository from organization if you're its owner or member.

*pat is PAT(Personal Access Token).

git clone https://<pat>@github.com/<your account or organization>/<repo>.git

To clone a private repository from your account or organization, you need to generate a PAT(Personal Access Token) on your Github account, and add it to the command above. *Organization doesn't have PAT generator.

This is how you generate a PAT on your Github account:

1, Go to "Settings":

enter image description here

2, Scroll down then go to "Developer settings":

enter image description here

3, Press "Personal access tokens" then press "Generate new token":

enter image description here

4, Fill "Note" then check "repo":

*If "repo" is not checked, you cannot clone a private repository.

enter image description here

5, Scroll down then press "Generate token":

enter image description here

6, Finally, a PAT is generated:

enter image description here

This is the command with the PAT generated above:

git clone https://[email protected]/<your account or organization>/<repo>.git
Share:
440,423
Solomon
Author by

Solomon

Updated on July 15, 2022

Comments

  • Solomon
    Solomon almost 2 years

    I have a private repository on Github for a project I'm working on. Until now I had only worked on my home desktop, but I just bought a laptop, and am trying to set it up so that I can work on the project from either computer, and push / pull changes.

    I added a new SSH key to my Github account for the laptop, and was successful in cloning and making changes to a public test repo that I set up. However, I couldn't clone the private repo. Is there anything special I need to do in the command line in order to clone a private repo? Do I need to set up a new GitHub account for my laptop and set myself up as a collaborator?

    The command I used was git clone git://github.com/username/reponame.git

  • Félix Adriyel Gagnon-Grenier
    Félix Adriyel Gagnon-Grenier over 8 years
    I agree with simplicity of not having to setup ssh
  • reinhardt
    reinhardt about 8 years
    thanks for this. But I'm oblivious as to why this works. Was never prompted for a password. Should git clone via https in any way touch my ssh keys? I thought not.
  • The Onin
    The Onin almost 8 years
    Nice, it made me able to download a private repo on Ubuntu.
  • greg_data
    greg_data over 7 years
    Agree with other comments, just using HTTPS url works perfect on Windows or Ubuntu (tried on 116.04)
  • diimdeep
    diimdeep about 7 years
    Why this works but [email protected]:username/repo.git don't ?
  • charlchad
    charlchad over 6 years
    @diimdeep Git and other tools, often use the git: protocol for accessing files in remote repositories. Some firewall configurations are blocking git:// URLs, which leads to errors when trying to clone repositories or download dependencies. (For example corporate firewalls are "notorious" for blocking git:.) If you run into this issue, you can force the use of https: instead, by running the following command: git config --global url."https://".insteadOf git://
  • David Winiecki
    David Winiecki about 6 years
    In some cases, if you are prompted for a password and then denied access, it may be because you need to provide a "personal access token" instead of your GitHub login password. [1] help.github.com/articles/which-remote-url-should-i-use [2] help.github.com/articles/…
  • pcnate
    pcnate almost 6 years
    this is problematic with special character passwords, leaves your password in the logs and doesn't seem to work anymore
  • pcnate
    pcnate almost 6 years
    looks like git prompts for username/password if there is no public repository with that URI, so the first command is correct
  • Pavel Šimerda
    Pavel Šimerda almost 6 years
    The answer is wrong, SSH key has no relevance to HTTP access.
  • Let Me Tink About It
    Let Me Tink About It over 5 years
    The dollar symbol $ is a reserved character in Unix. It marks the beginning of the retrieval of a variable value with that name equal to the string that follows the $ symbol.
  • leo
    leo over 5 years
    Yes - using "[email protected]:" rather than "https://" solved my problem! Actually I enabled 2FA at the same time but that had nothing to do with it.
  • Vinay
    Vinay about 5 years
    Try this - You will be prompted to enter your account password. No worries of password learking to history or logs. git clone https://[email protected]/username/repo_name.git
  • bvdb
    bvdb over 4 years
    It displays: unknown option -T. My git version is 2.7.4
  • Kexin Lu
    Kexin Lu over 4 years
    @bvdb my apologies, it should have been ssh -T [email protected]
  • Jirik
    Jirik over 4 years
    If you are on Windows you can install github.com/microsoft/Git-Credential-Manager-for-Windows Then when cloning a private repository, UI window with Github login appear. It will also let you fill 2FA code. Your Github credentials will be stored securely.
  • Michael Larionov
    Michael Larionov over 4 years
    For me deleting Windows Credentials fixed the problem.
  • Tian
    Tian almost 4 years
    This is not safe. The cloned repo stores the provided http link in the .git/config file. All commits made to the local repo can be pushed to remote without requiring username and password. Which is really dangerous if you are not the only user!!
  • Raghu
    Raghu over 3 years
    In 2FA method at step #4, where is the host mentioned in clone command ?
  • Black Mamba
    Black Mamba over 3 years
    Yes, where https://[email protected] is the repository you're trying to clone and the settings are available in your own user settings.
  • Farhan Hai Khan
    Farhan Hai Khan over 3 years
    Well if it worked for me in GOOGLE COLABORATORY!
  • Alvin Novian
    Alvin Novian over 3 years
    Thank you for this, another way is to set the url format like this: https://username:[email protected]/username/repo_name.‌​git it's helpful for me if I have public repo that changed to private. All I need to do is to change the remote origin with that format.
  • Muneeb Ahmad Khurram
    Muneeb Ahmad Khurram about 3 years
    good, answer works well in google colab just, change ur password before sharing the .ipynb file :)
  • dylanvanw
    dylanvanw almost 3 years
    Authentication using passwords will be depreciated on August 13, 2021 and will no longer work after that. github.blog/…
  • Milliron X
    Milliron X almost 3 years
    This will still work if you use a Personal Access Token, and can easily be scripted to prompt for a token (read -s).
  • teoman
    teoman over 2 years
    git clone https://[USER]:[TOKEN]@github.com/username/repo.git also works!
  • fred
    fred over 2 years
    This apparently has changed slightly. git clone https://user:[email protected]/username/repo.git worked for me
  • bytefish
    bytefish almost 2 years
    Works for me in 2022.