How to clone a private repository in Github

Git is an open-source, version control tool created in 2005 by developers working on the Linux operating system.

Cloning public repositories from Github or Gitlab using git is pretty straightforward, but when it comes to handle a private one, sometimes the situation is a bit tricky. You can either clone a private Github repository with a password like you normally would with any other online service, or do it with a token if you enabled 2-factor-authentication on your account or your organization uses SAML SSO. Alternatively, you can also clone private Github repo with SSH credentials.

In this article, we will show you how to clone a private repo using git with both a password and a Github token. We will also cover possible solutions to the common "Repository not found" error message when you try to clone a private repo from Github.

Prerequisites

In order to clone a git repository, you obviously need to have Git installed on your computer.

To check if Git is correctly installed on Windows or Linux, execute the following command :

$ git --version git version 2.22.0

Now that you know that Git is correctly installed, you're ready to start cloning private Github repositories.

Clone private Github repo with username/password

In order to clone a private Github repo with an username and password pair, you can simply put the username in front of github.com, following with code>@</code like so :

git clone https://[email protected]_url git clone https://[email protected]/go-gitea/gitea.git #An example
Code language: PHP (php)

In a few moment, you will be prompted for a password. Once you input the correct one, the output should look something like this :

git clone git://[email protected]/username/private-repo.git Cloning into 'private-repo' Password for 'https://[email protected]_url: remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done.
Code language: PHP (php)

Alternatively, if you don't really care about exposing your password in plain text, you can put it directly in the repository URL :

git clone https://your_username:[email protected]/username/private-repo.git
Code language: PHP (php)

Important: Private repositories cannot be cloned using git protocol.

git clone https://github.com/orgname/reponame.git #WILL NOT WORK
Code language: PHP (php)

You can configure git to specifically use HTTPS over other protocols by setting hub.protocol global flag to https. This will affect all git operations, including clone, fork, remote add and other operations that expand references to GitHub repositories as full URLs.

git config --global hub.protocol https
Code language: CSS (css)

Clone private Github repo without typing password

Using an username/password pair, you have to input the password every time you want to push, pull or do operations to the repo. This behaviour can be annoying at times. Luckily, you can permanently store your password in git configuration file so you won't be prompted ever again.

First, you have to enable the credentials helper by setting the credential.helper flag to store.

git config --global credential.helper store
Code language: CSS (css)

From now on, you will be asked to provide the password on the first time you clone a new private repository. Once git successfully connected to the repo, the password will be stored in .git-credentials file inside your home directory.

cat ~/.git-credentials https://<username>:<password>@<server>
Code language: HTML, XML (xml)

Clone private Github repo with SSH keys

To be able to clone from Github using SSH, you must generate an SSH keypair on your computer and add the public key to your GitHub account. Github documentation has extensively covered the process at Connecting to GitHub with SSH.

Next, you have to find the repository SSH URL from Github repo page.

A GitHub project screen, showing the Clone or Download dropdown menu to select SSH

Click Use SSH and the SSH URL should be displayed in a second.

Clicking the Copy to clipboard button on the GitHub project screen

This SSH URL works exactly the same way username/password pair does, except you don't have to enter a password because it is already stored on your system in the form of a private key.

Clone private Github repo with token

In order to clone a Github repo with a 2FA-enabled Github account, you need to use a personal access token instead of entering the password. This Github token also allows you to login to organizations that uses SAML single sign-on - a technology used to control who can access which part of the repository.

Head over to Github dashboard. In the upper-right corner of any page, click your profile photo, then click Settings.

Settings icon in the user bar

In the left sidebar, click Developer settings.

Developer settings

You will be redirected to Github Apps page. In the left sidebar, click Personal access tokens and then Generate new token.

Generate new token button

You have to give your token a descriptive name and grant it the permission to access your repositories. To use your token to access repositories from the command line, select repo. Finally, click Generate Token button at the end of the webpage.

Selecting token scopes

At this stage, you have to copy the token and securely save it elsewhere. For security reasons, after you navigate off the page, you will not be able to see the token again.

Treat your tokens like passwords and keep them secret because they are actually your password to some extent.

Once you have the token, you can use it just like a password. Whenever you are prompted for password, input that token. The token is pretty long and hard to remember, so I recommend you saving it to avoid type it again and again.

git config --global credential.helper store git clone https://your_username:[email protected]/username/private-repo.git
Code language: PHP (php)

"Repository not found" error when clone a private repo

If you see this error message when cloning a private Github repo, it basically means that the repository does not exists (of course) and you don't have the permission to access it.

First, try each of the below checklist taken from Github docs :

  • Check your spelling
  • Check your permissions
  • Check your SSH access
  • Check that the repository really exists

If "Repository not found" does not disappear and you're using Windows, you may have old session state. In order to delete them and cleanly log in to Github again, you have to follow these steps:

  1. Open Control Panel from the Start menu and select Select User Accounts
  2. Select Manage your credentials in the left hand menu
  3. Delete any credentials related to Git or GitHub

On Linux, try clearing saved credentials from your system with the following command.

rm -rf ~/.git-credentials

On a MacOS device, you can go to Keychain Access, find the credentials related to Git and Github, then change the username and password to the correct value.

If you still get the error, despite following all above solutions, you can try reinstalling git's credential-manager using these commands.

git credential-manager uninstall git credential-manager install

We hope that you've learned how to clone a private repo using git with username/password pair and a Github token. If you see "Repository not found" error message when you try to clone a private repo from Github, you may know solved it. We've covered a few more tutorials and articles on how to use git and troubleshoot it, have a look and let we know what you think.

Click to rate this post!
[Total: 8 Average: 5]

Leave a Comment