A lot of times, you need to clone a GitHub Private Repository. On August 13, 2021, GitHub removes support for password authentication. That means you cannot clone a private repo just by using your GitHub username and password anymore. Now, you have two ways to clone GitHub: HTTPS + Personal Tokens or SSH with SSH keys.

1. Using HTTPS to Clone GitHub Private Repository

1.1. Create a Personal Token

a. Go to your GitHub account settings.

github settings
GitHub Settings

b. Click on “Developer settings” in the left sidebar.

developer settings
Developer Settings

c. Click on “Personal access tokens” and then on “Fine-grained token.”

fine-grained tokens
Fine-grained Tokens

As its name indicate, fine-grained token gives more granularity control. Please note that you cannot set permanent fine-grained tokens anymore as classic tokens. The fine-grained tokens must expire within a year. I personally like this reinforcement, as team members can change quite a bit within a year period in case you forgot to recycle the tokens you give out.

You may also click Generate a personal access token to directly come to the token creation page.

d. Give necessary permission for this token

For repository access, I recommend you choose only the repos you would like this token to access.

repository access
Repository Access

For Repository permissions, for a regular developer, check at least the following permissions.

Mark Commit statuses, Contents, and Pull requests as Read and Write. Metadata will be marked read-only automatically.

1.2. Clone the Private Repo

Supply the personal access token you just created as if it is the password.

git clone https://<your_username>:<your_personal_access_token>@github.com/owner/repo_name.git

Alternatively, you may just clone the repo and wait for the prompt to ask you for the username and password. The prompt might say the password, but you should use your personal access token instead.

If you would like your repo to remember the personal access token, use the following git command to save it locally.

git config credential.helper store

The official document has more detailed information about personal tokens.

2. Using SSH to Clone GitHub Private Repository

Follow the GitHub guide for generating a new SSH key and adding it to your account;

Add the SSH key to your GitHub account by following this guide.

Then clone the repo using the following command line. Replace owner with the repo owner’s username, and repo_name with the name of the repository.

git clone git@github.com:owner/repo_name.git

After cloning the repository, you can navigate to the local folder, make changes, commit, and push to the remote repository as you would with any other Git repository.

3. Conclusion

Between HTTPS and SSH methods, I recommend using the latest fine-grained token. It gives you much more flexibility over permission controls. Please let us know your preference by commenting below.

Recently we have posted quite a few good AI tutorials, please feel free to take a look.

Leave a Reply

Your email address will not be published. Required fields are marked *