Cloning a private Github repo

好久不见. 提交于 2019-12-17 10:14:07

问题


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/myusername/reponame.git


回答1:


Private clone URLs take the form git@github.com: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.




回答2:


This worked for me:

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

This, of course, was after adding my SSH key to Github. I used this on a CentOs server, if that's necessary.




回答3:


This worked for me:

git clone https://username:password@github.com/username/repo_name.git



回答4:


For God's sake, 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)



回答5:


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.




回答6:


For me the solution was:

git clone https://myusername@restoftherepolink.git

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

git clone https://myusername@github.com/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://myusername@restoftherepolink.git

When prompted for password put that token in here.




回答7:


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.




回答8:


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.




回答9:


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.




回答10:


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.




回答11:


This worked for me on mac git clone https://username@github.com:username/repo_name




回答12:


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:x-oauth-basic@github.com/$username/$repo.git



回答13:


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




回答14:


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.




回答15:


Add your desktop ssh public key in github.

https://github.com/settings/keys

You can clone the repo without any password.




回答16:


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 git@github.com is logged in with another account.

to check that you can do

ssh -T git@github.com

If it shows another account, to resolve this issue:

 ssh-add -D
 ssh-add ~/.ssh/your_rsa
 ssh -T git@github.com
 git clone git@github.com:<owner_name>/<repo_name>.git


来源:https://stackoverflow.com/questions/2505096/cloning-a-private-github-repo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!