Clone A Private Repository (Github)
Asked Answered
A

34

476

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

Architrave answered 24/3, 2010 at 3:12 Comment(1)
For some reason, the April 2022 update answer didn't work for me. The answer given here (#69140574) worked for me - git clone https://username:[email protected]/user/repoCoopersmith
M
247

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.

Mamiemamma answered 29/3, 2010 at 13:5 Comment(1)
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.Prolegomenon
T
392

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
Treacherous answered 12/12, 2021 at 2:41 Comment(10)
This also works great in GitHub Actions that are using private repositories. You can set Personal Access Token as secret in Actions and clone private repositories.Housecarl
This worked for me, but I still get the same issue when I try to git pull. How to pull from a private repo?Homeomorphism
Tried this but it still asks for a password and my password is failing. If I make it public it works. The question was about private repositories in github so I think this is missing information.Univalent
How can I generate a token to clone but read-only?Gas
Using the old version of github tokens worked for me, private repo in another organizationOccultation
Note for others - if you are still asked for a password after entering the URL in the format noted (https://<pat>@github.com/<your account or organization>/<repo>.git) - try creating a NEW personal access token; this solved the issue for me. Also: Note that as of Mar 2023 GitHub has added a second "type" of personal access token so that the "Personal access tokens" button shown in the screenshots is now a dropdown. As of this date, selecting the regular token (not the "fine-grained token") works fine, though I suspect both work fine.Bruton
The token is useful when the repo is private, so this is a valid solution.Thalamus
Yes, this works! And for now (Nov 2023) the pattern is github_pat_* instead of just ghp_*.Conceptacle
? theres no github pat featureCrum
@Univalent You can try entering your PAT when it asks for a password. That usually works for me.Coleridge
M
296

This worked for me:

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

Malta answered 4/1, 2015 at 5:37 Comment(10)
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.Leighton
Nice, it made me able to download a private repo on Ubuntu.Kahlil
Why this works but [email protected]:username/repo.git don't ?Eucken
@Eucken 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://Malta
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/…Chutney
The answer is wrong, SSH key has no relevance to HTTP access.Wherewith
Support for password authentication was removed on August 13, 2021Carbaugh
Didn't work for me. "remote: Repository not found."Freyah
No longer a valid answer after password authentication was removed in year 2021Lombard
This still works, when it asks for password you need to put in your Personal Access TokenBrigidabrigit
M
247

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.

Mamiemamma answered 29/3, 2010 at 13:5 Comment(1)
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.Prolegomenon
M
164

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)
Militant answered 24/8, 2018 at 20:41 Comment(3)
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.Krakow
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.Carlita
I can't clone my private repo. It says it wasn't found.Freyah
E
103

This worked for me:

git clone https://username:[email protected]/username/repo_name.git
Expiation answered 1/10, 2017 at 7:13 Comment(4)
this is problematic with special character passwords, leaves your password in the logs and doesn't seem to work anymoreVotaw
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.gitWinther
Authentication using passwords will be depreciated on August 13, 2021 and will no longer work after that. github.blog/…Oxytocin
This will still work if you use a Personal Access Token, and can easily be scripted to prompt for a token (read -s).Goyette
F
57

A better Solution for 2023 and 2024

using Personal Access Token works, however you may prefer to narrow the token access to only the Repository you want to clone

Github introduced Fine-grained personal access tokens which can do exactly that.

Here is the steps to follow:

  1. From the top-right menu choose "Settings"

enter image description here

  1. In the page that follows, from left sidebar choose "Developer Settings"

enter image description here

  1. Then in the next page under "Personal access tokens" choose "Fine-grained tokens"

enter image description here

  1. Then click the button "Generate new token"

enter image description here

  1. In the next page you will be asked to give a name to your token and you can choose the Repository access as well. If you want to specify just a single repository, then choose "Only select repositories" then from the dropdown menu, select your repository

enter image description here

  1. After that, in the same page, you need to specify the Repository permissions. If you want just to be able to clone a private repo, make "contents" as "Access: read-only"

enter image description here

Finally click "Generate token" button

Once you get the token, copy it and run the command

git clone https://oauth2:<YOUR-TOKEN-HERE>@github.com/<your_user>/<your_repo>.git

And that's it.

Falda answered 22/12, 2022 at 15:38 Comment(2)
This one seems to be the best answer in my opinion. It restricts access to only reading the repo, and it doesn't require you to input the access token each time you pull / fetch.Polygenesis
this works if you are the owner of the repository. but i dont see repositories i have been invited to under "select repositories"Longueur
G
34

Solution: FEB 2022

  1. go to the settings > developer settings > personal access token link: https://github.com/settings/tokens
  2. click on generate new token
  3. Select scopes (enable all for full access)

This will generate a token which looks something like this:

enter image description here

Now use the following command:

git clone https://user:[email protected]/username/repo.git


Update 2021 (outdated)

OUTDATED: Trying the following command:

git clone https://user:[email protected]:username/repo.git

will resolove in the following error:

Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.

and links you to the following support page:

https://docs.github.com/ en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token



Solution 2021 (outdated)

Click on your icon profile and go to (use the link above for more guidance):

settings> developer settings> personal acces token > generate new token

Glasscock answered 29/11, 2021 at 8:54 Comment(1)
This apparently has changed slightly. git clone https://user:[email protected]/username/repo.git worked for meObstetrician
C
33

For me the solution was:

git clone https://[email protected]

Here you need to be the owner of the repo but if you aren't then it will go as

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

If you have 2FA enabled then:

  1. Go to the settings from the profile icon in top right or visit https://github.com/settings/profile
  2. Go to the bottom tab or go to https://github.com/settings/tokens
  3. Open last tab here Personal tokens. And generate a token
  4. Copy the token and run git clone https://[email protected]

When prompted for password put that token in here. Token generation image here

Clovis answered 16/5, 2018 at 5:27 Comment(3)
looks like git prompts for username/password if there is no public repository with that URI, so the first command is correctVotaw
In 2FA method at step #4, where is the host mentioned in clone command ?Compote
Yes, where https://[email protected] is the repository you're trying to clone and the settings are available in your own user settings.Clovis
G
23

Using Git for Windows it is easier to use HTTPS url.

Open a git shell then git clone https://github.com/user/repo. Enter username and password when prompted. No need to setup a SSH key.

Gynophore answered 10/3, 2014 at 13:21 Comment(2)
I agree with simplicity of not having to setup sshBlinni
Agree with other comments, just using HTTPS url works perfect on Windows or Ubuntu (tried on 116.04)Brut
A
16

As everyone aware about the process of cloning, I would like to add few more things here. Don't worry about special character or writing "@" as "%40" see character encoding

$ git clone https://username:[email protected]/user/repo   

This line can do the job

  1. Suppose if I have a password containing special character ( I don't know what to replace for '@' in my password)
  2. What if I want to use other temporary password other than my original password

To solve this issue I encourage to use GitHub Developer option to generate Access token. I believe Access token is secure and you wont find any special character.

creating-a-personal-access-token

Now I will write the below code to access my repository.

$ git clone https://username:[email protected]/user/repo

I am just replacing my original password with Access-token, Now I am not worried if some one see my access credential , I can regenerate the token when ever I feel.

Make sure you have checked repo Full control of private repositories here is the snap short

Able answered 4/11, 2020 at 18:5 Comment(1)
good, answer works well in google colab just, change ur password before sharing the .ipynb file :)Motorway
S
11

I needed a non-interactive method for cloning a private repo.

Inspired by this issue: https://github.com/github/hub/issues/1644

Step 1.

Create a personal access token in the github developer settings: https://github.com/settings/tokens

Step 2.

git clone https://$token:[email protected]/$username/$repo.git
Sussi answered 29/5, 2019 at 14:7 Comment(0)
B
11

August 13, 2021 update

From here:

In July 2020, we announced our intent to require the use of token-based authentication (for example, a personal access, OAuth, or GitHub App installation token) for all authenticated Git operations. Beginning August 13, 2021, we will no longer accept account passwords when authenticating Git operations on GitHub.com.

What you need to do today

  • For developers, if you are using a password to authenticate Git operations with GitHub.com today, you must begin using a personal access token over HTTPS (recommended) or SSH key by August 13, 2021, to avoid disruption. If you receive a warning that you are using an outdated third-party integration, you should update your client to the latest version.

  • For integrators, you must authenticate integrations using the web or device authorization flows by August 13, 2021, to avoid disruption. For more information, see Authorizing OAuth Apps and the announcement on the developer blog.


Using a token on the command line

1 ) Create the personal access token - follow this.

2 ) Once you have a token, you can enter it instead of your password when performing Git operations over HTTPS.

For example, on the command line you would enter the following:

$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token # <----- Bingo!

(*) See more in here.

Bahner answered 25/8, 2021 at 20:57 Comment(1)
git clone https://[USER]:[TOKEN]@github.com/username/repo.git also works!Worshipful
R
10

I have a company (private) account and 2-Factor-Authentication enabled, so I had to combine a few posts to make it work as below. (Jotting down so it may be useful to someone with the same situation)

Initially it was the Fatal Error. 
fatal: repository 'https:
...
remote: Repository not found.
fatal: repository 'https:

Installed the credential manager and updated GIT as mentioned here: https://codeshare.co.uk/blog/how-to-solve-the-github-error-fatal-httprequestexception-encountered/

That did not solve the problem as the issue moved to the below when I tried using the clone command as follows:

$ git clone https://<username>:<password>@github.com/<ORG_NAME>/<PROJECT-NAME>.git

remote: Invalid username or password.
fatal: Authentication failed for 'https://

My password had $ symbol in it and for some reason GIT command line / GIT Bash did not like it. I can see this on the error returned text did not have the $ symbol in it.

fatal: Authentication failed for 'https://<username>:<password-missing-$-symbol>@github.com/<ORG_NAME>/<PROJECT-NAME>.git'

I had to reference this site: https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage

Please note: If an incorrect password is stored for your account in Windows Credential Manager it will also add to the problem. I removed the github credential stored this way before moving to the step below.

$ git config --global credential.helper cache
$ git clone https://<username>:<password>@github.com/<ORG_NAME>/<PROJECT-NAME>.git

Now the Windows Credential manager popped-up. I typed my username and password in the text-boxes (This accepted my password that had the $ symbol) and prompted me for a 2-Factor Authentication code. I typed in the authentication code from Google Authenticator, and the clone started perfectly.

Rajewski answered 8/10, 2018 at 21:20 Comment(2)
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.Oriental
For me deleting Windows Credentials fixed the problem.Brecher
T
9

When cloning from private repos with 2FA enable, there is a simple steps which you need to follow

  1. Go to your Git account
  2. Go to Settings-> Developer Settings->Personal Access Token
  3. Click on Generate new token
  4. Create a token with title you want and with the functionalities
  5. When you are cloning the private repo, by using git clone repoName, after entering your user name, give personal access token as the password.

Follow same steps when you get Authentication failed error message for Private repo

Tombolo answered 16/9, 2019 at 15:18 Comment(0)
O
6

In response to mac's answer, you can get your SSH clone URL on your github repo page, by clicking SSH on You can clone with HTTPS, SSH, or Subversion. and copy the URL.

Ossian answered 7/10, 2013 at 1:37 Comment(0)
A
5

This worked for me on mac git clone https://[email protected]:username/repo_name

Alienist answered 18/5, 2018 at 7:13 Comment(0)
C
4
git clone https://{personal access token}@github.com/Shuai1996/Root.git
Competence answered 1/7, 2022 at 13:8 Comment(0)
C
3

In addition to MK Yung's answer: make sure you add the public key for wherever you're deploying to the deploy keys for the repo, if you don't want to receive a 403 Forbidden response.

Carilla answered 28/9, 2014 at 13:33 Comment(0)
W
3

First make sure that you have a SSH key or generate one at: https://help.github.com/articles/generating-ssh-keys/

Once you have your key, you have to add it to your github account at: https://github.com/settings/ssh

For Windows users it's useful to run git bash as an administrator.

Now the cloning should work for private repositories (repo), without having to put your username and password.

Whaleback answered 11/6, 2015 at 20:48 Comment(0)
F
3

If you are sure that you don't have 2FA enabled, you have permission to access the repo, and the repo exists, it's possible that your [email protected] is logged in with another account.

to check that you can do

ssh -T [email protected]

If it shows another account, to resolve this issue:

 ssh-add -D
 ssh-add ~/.ssh/your_rsa
 ssh -T [email protected]
 git clone [email protected]:<owner_name>/<repo_name>.git
Fortunio answered 5/10, 2019 at 22:22 Comment(2)
It displays: unknown option -T. My git version is 2.7.4Crank
@Crank my apologies, it should have been ssh -T [email protected]Fortunio
P
2

In case you have two-factor authentication enabled, make sure that you create a new access token and not regenerate an old one.

That didn't seem to work in my case.

Pannier answered 26/12, 2020 at 5:56 Comment(0)
J
1

Add your desktop ssh public key in github.

https://github.com/settings/keys

You can clone the repo without any password.

Jacquesjacquet answered 9/2, 2018 at 7:24 Comment(0)
A
1

If the newly used computer has different credentials running this command

git clone https://github.com/username/reponame.git

directly will not work. Git will attempt to use the stored credentials and will not prompt you for the username and the password. Since the credentials mismatch, git will output Repository not found and the clone operation fails. The way I solved it was by deleting the old credentials, since I don't use them anymore, and ran the the above mentioned command again and entered the required username and password and cloned the private repo.

Anion answered 24/9, 2018 at 9:55 Comment(0)
G
1

If you want to achieve it in Dockerfile, below lines helps.

ARG git_personal_token
RUN git config --global url."https://${git_personal_token}:@github.com/".insteadOf "https://github.com/"
RUN git clone https://github.com/your/project.git /project

Then we can build with below argument.

docker build --build-arg git_personal_token={your_token} .
Gertie answered 24/12, 2019 at 10:55 Comment(0)
P
1

1) try running command with username and password in below format

git clone https://your_username:[email protected]/username/reponame.git

now problem as others have mentioned here is when we have special character in our password. In Javascript use below code to convert password with special characters to UTF-8 encoding.

console.log(encodeURIComponent('password@$123'));

now use this generated password instead of one with special characters and run command.

Hope this solve issue.

Parkin answered 8/1, 2020 at 12:38 Comment(0)
W
1
git clone https://myusername:[email protected]/myusername/project.git

When you are trying to use private repo from the repo at the time you need to pass username and password for that.

Wongawonga answered 30/1, 2020 at 6:13 Comment(2)
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!!Obligatory
Well if it worked for me in GOOGLE COLABORATORY!Habitual
B
1

Cloning Private Repository using HTTPS in Year 2022

If maintainer of repository has given Developer access to you on his private library ,you need to first login to https://gitlab.com/users/sign_in with user for which you have received invitation,you will be prompted to change your password,once you change your password then you can successfully clone repository ,pull and push changes to it.

Bergeron answered 23/8, 2020 at 19:19 Comment(0)
T
1

I was using Android Studio to clone the project from GitHub private repository and two-factor authentication (2FA). I created a personal token as made in lzl124631x's answer.

Then I cloned the repo using an url like this: https://YourGitHubUsername:[email protected]/YourRepoPath.git

Thermoelectrometer answered 10/9, 2020 at 17:53 Comment(0)
G
1

I think it also worth to mention that in case the SSH protocol can not be used for some reason and modifying a private repository http(s) URL to provide basic authentication credentials is not an option either, there's an alternative as well.

The basic authentication header can be configured using http.extraHeader git-config option:

git config --global --unset-all "http.https://github.com/.extraheader"
git config --global --add "http.https://github.com/.extraheader" \
          "AUTHORIZATION: Basic $(base64 <<< [access-token-string]:x-oauth-basic)"

Where [access-token-string] placeholder should be replaced (including square braces) with a generated real token value. You can read more about access tokens here and here.

If the configuration has been applied properly then the configured AUTHORIZATION header will be included in each HTTPS request to the github.com IP address accessed by git command.

Gustative answered 29/10, 2020 at 9:6 Comment(0)
A
1

I can create an SSH key and use that to clone the repository. The command will be still the same:

git clone [email protected]:DriveWealth/my-repo-name.git
Arber answered 20/12, 2021 at 4:46 Comment(0)
P
1

Generate a Classic Token, with repo scope enabled

Account -> Settings -> Developer Settings -> Personal access tokens (classic)

And clone the repository using the command below:

git clone https://[email protected]/account/repo.git
Pallium answered 26/10, 2023 at 14:24 Comment(0)
A
1

I tried all your solutions, but unfortunately, I couldn't download the repo. Your answers are correct, but apparently, they are not working I some computers. If anyone is in the same situation I was in, I want to share what I did.

Working with multiple GitHub accounts (personal and enterprise)

$ brew install gh
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
$ > Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

type: GitHub-your-username

You will have this ~/.ssh/GitHub-your-username.pub

$ gh auth login
$ use ssh

Select rsa created, in our case cd ~/.ssh, ls to see content, and you will see 'GitHub-your-username.pub'

Use "login by web" not token (solution for me)

And yala, you can clone private repos in which you are a collaborator

Adallard answered 12/1 at 7:0 Comment(0)
S
0

If you are using source tree make sure no wrong/different GitHub host names are added in the advanced settings.

enter image description here

If any wrong host names are present, remove them and on cloning next time you will see a prompt for username and password. use PAT as the password in that window

Sloshy answered 25/2, 2023 at 12:52 Comment(0)
L
0

If you have a private repository with empty folder follow these steps

Github username : user1, Repository name : demo-repo, Type of repository is : private

then

Step 1 : copy your private repository URL from github

https://github.com/user1/demo-repo.git

Step 2 : Open terminal in particular clone location

Step 3 : Using this commend

git clone https://[email protected]/user1/demo-repo.git

add username with @ sign before the github.com

Step 4 : Select authentication method to web browser

Finally empty private repository successfully cloned you personal computer

Lim answered 10/2 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.