Reputation: 7053
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
Upvotes: 517
Views: 855987
Reputation: 1
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://username:<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.
1, Go to "Settings":
2, Scroll down then go to "Developer settings":
3, Press "Personal access tokens" then press "Generate new token":
4, Fill "Note" then check "repo":
*If "repo" is not checked, you cannot clone a private repository.
5, Scroll down then press "Generate token":
6, Finally, a PAT is generated:
This is the command with the PAT generated above:
git clone https://username:[email protected]/<your account or organization>/<repo>.git
Upvotes: 469
Reputation: 424
You can also clone your private repo via authenticating with Github CLI. Install Github CLI locally on your machine and authenticate using the below commands.
gh auth login
Once authentication is successful, try to clone your private repo. It should work.
Upvotes: 1
Reputation: 865
settings > developer settings > personal access token
link: https://github.com/settings/tokensgenerate new token
This will generate a token which looks something like this:
Now use the following command:
git clone https://user:[email protected]/username/repo.git
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:
Click on your icon profile and go to (use the link above for more guidance):
settings> developer settings> personal acces token > generate new token
Upvotes: 46
Reputation: 578
I just spent an hour frustrated with this. Found the easiest way is to run apt install gh
then to run gh auth login
then follow the prompts. Then you can use git normally and clone any and all private repos. Hope this helps someone else as i wasted an hour of my life just now.
Or you can also use gh repo clone name/your_repo
Upvotes: 2
Reputation: 8442
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:
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.
Upvotes: 81
Reputation: 321
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
Upvotes: 1
Reputation: 31
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
Upvotes: 1
Reputation: 46
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
Upvotes: 2
Reputation: 2558
If you are using source tree make sure no wrong/different GitHub host names are added in the advanced settings.
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
Upvotes: 0
Reputation: 2776
git clone https://{personal access token}@github.com/Shuai1996/Root.git
Upvotes: 6
Reputation: 619
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.
Upvotes: 1
Reputation: 12461
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
Upvotes: 2
Reputation: 22198
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.
Upvotes: 11
Reputation: 5085
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.
Upvotes: 2
Reputation: 223
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
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
Upvotes: 16
Reputation: 5971
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.
Upvotes: 1
Reputation: 3441
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
Upvotes: 2
Reputation: 3755
This worked for me:
git clone https://[email protected]/username/repo_name
Upvotes: 312
Reputation: 4809
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.
git clone https://github.com/username/repo_name.git
(The default git repo link is good enough!)Update:
git config credential.helper store
(don't do this on machine you don't trust)Upvotes: 169
Reputation: 142
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.
Upvotes: 1
Reputation: 1352
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.
Upvotes: 1
Reputation: 25080
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} .
Upvotes: 1
Reputation: 59
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
Upvotes: 3
Reputation: 2402
When cloning from private repos with 2FA enable, there is a simple steps which you need to follow
Follow same steps when you get Authentication failed error message for Private repo
Upvotes: 9
Reputation: 633
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
Upvotes: 11
Reputation: 15595
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:
git clone https://[email protected]
When prompted for password put that token in here.
Upvotes: 33
Reputation: 791
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.
Upvotes: 10
Reputation: 3688
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.
Upvotes: 1
Reputation: 187
This worked for me on mac
git clone https://[email protected]:username/repo_name
Upvotes: 5
Reputation: 2034
Add your desktop ssh public key in github.
https://github.com/settings/keys
You can clone the repo without any password.
Upvotes: 1