Just use the HTTPS address to clone with the key as the user, so:

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

or

git clone https://username:[email protected]/username/repo.git
Answer from Andris on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 62152834 › whats-the-correct-way-to-pass-token-with-git-clone-using-pypi
python - What's the correct way to pass token with git clone using PyPi? - Stack Overflow
The token is meant to be used with GitHub's REST API, not git itself. ... Thanks for the clarifications. It worked with the https url ... Thanks to the comments, I was able to clone my repository using the https url instead of the ssh url and it worked without the need for token.
🌐
Graphite
graphite.com › guides › git-clone-with-token
Cloning a Git repository using a GitHub token - Graphite
Once you have your personal access token, you can use it to clone a repository from the command line: ... Replace <username> with your GitHub username and <token> with your personal access token.
🌐
GitHub
github.com › mazen160 › GithubCloner
GitHub - mazen160/GithubCloner: A script that clones Github repositories of users and organizations. · GitHub
Clone all repositories of an organization, along with all repositories of the organization's members. ./githubcloner.py --org organization --include-org-members -o /tmp/output · Use Github authentication in the task. ./githubcloner.py --org organization -o /tmp/output --authentication user:token
Starred by 426 users
Forked by 125 users
Languages   Python
🌐
DEV Community
dev.to › nisevi › script-for-cloning-repositories-4h4m
Python script for cloning repositories - DEV Community
July 28, 2020 - This is a script for cloning repositories from a given organization in the GitHub ecosystem. Tagged with python, script, github, token.
🌐
Medium
kettan007.medium.com › how-to-clone-a-git-repository-using-personal-access-token-a-step-by-step-guide-ab7b54d4ef83
How To Clone a Git Repository Using Personal Access Token: A Step-by-Step Guide | by Ketan Gupta | Medium
July 16, 2024 - Clone the Repository Using the ... <repository-url> with the URL of the Git repository you wish to clone: git clone https://<PAT>@github.com/username/repo.git...
Find elsewhere
🌐
CodePal
codepal.ai › code generator › python git clone repository with token authentication and search changed files
Python Git Clone Repository with Token Authentication and Search Changed Files - CodePal
November 24, 2023 - repourl = “https://github.com/example/repo.git” token = “yourtokenhere” repopath = clonerepositorywithtoken(repourl, token)
🌐
Reddit
reddit.com › r/git › how to clone a private organization repo using a personal git token using git clone command
r/git on Reddit: How to clone a private organization repo using a personal git token using git clone command
September 1, 2023 -

Hi all,

I am part of an organization, and I have created a personal git token,

Now I want to clone a repo from my organization using my git token,

currently I and doing some thing like this
git clone https://your-username:your-token@github.com/organization-name/your-private-repo.git

but this does not seem to work

can you pls suggest how to clone using git clone command using token only
I am not doing ssh because of some reasons

can you pls suggest

thankyou

🌐
GitHub
gist.github.com › frozenfoxx › c874a7a219a04532e61d60288e5c6eac
A python script to clone a specified github user's repos, gists. · GitHub
March 9, 2022 - Clone this repository at &lt;script src=&quot;https://gist.github.com/frozenfoxx/c874a7a219a04532e61d60288e5c6eac.js&quot;&gt;&lt;/script&gt; Save frozenfoxx/c874a7a219a04532e61d60288e5c6eac to your computer and use it in GitHub Desktop. ... A python script to clone a specified github user's ...
🌐
Readthedocs
geo-python-site.readthedocs.io › en › latest › lessons › L2 › git-basics.html
Meet Git - Geo-Python - Read the Docs
October 20, 2022 - On the command line this action is equivalent to the git clone command. ... Pay attention to which folder you are in! Git will create a new folder under the folder you are located in when cloning a repo. Git needs to know who you are in order to give you access to remote repositories. Insert your GitHub username and personal access token:
🌐
Medium
pythononpow.medium.com › howto-use-github-tokens-on-the-command-line-995422c06f00
Howto use github tokens on the command line - Klaas (khz) - Medium
August 16, 2021 - git clone https://<username>:<token>@github.com/<accountname>/<reponame> Developer · Github · Cli · Python · Git · 36 followers · ·5 following · 11 to 1 pm spare time software developer.
🌐
Accuweb
accuweb.cloud › home › how to clone a private repo using a personal access token (pat)?
Git Clone Private Repo Using PAT (GitHub Step-by-Step)
May 27, 2024 - Learn how to git clone a private GitHub repository using a Personal Access Token. Step-by-step guide with common errors and fixes.
🌐
GitHub
github.com › Chaitanya-git › CloneLab
GitHub - Chaitanya-git/CloneLab: A python script to recursively clone project groups and subgroups on GitLab
Currently options unique to either git clone or git pull are not supported when updating partially cloned subgroups. If you do not wish to build and install a pip package, simply run the clonelab file inside the scripts folder with a python interpreter of your choice. CloneLab can optionally load api tokens from a secure, encrypted file stored in ~/.config/clonelab/tokens.
Author   Chaitanya-git
🌐
Educative
educative.io › answers › how-to-clone-a-private-repository-from-github
How to clone a private repository from GitHub
Copy the token generated and use it with this command. git clone https://<pat>@github.com/<your account or organization>/<repo>.git
Top answer
1 of 1
10

There's a couple of steps needed for this: firstly you need to get some information from GitHub by hand, and then there is a little dance that your app needs to do to swap its authentication secrets for a temporary code that can be used to authenticate a git clone.

Gather information

Before you can write a function to do this, you need three pieces of information, all of which are available from the App's settings. To get there

  1. Go to the organization you have created the App for
  2. Go to Settings > Developer Settings > GitHub Apps
  3. Click Edit next to the name of the App you're using, and authenticate with 2FA

The three pieces of information you need are:

The App ID

This is in the General page, in the About section at the top.

The Installation ID

If you haven't already, you also need to install the App into the Organization. Once this is done, go back to the Install App page in the App settings, and copy the link for the installation settings. Paste it into your editor and get the number from the end. The link should have the form https://github.com/apps/{app_name}/installations/{installation_id}; the part after the last / is the installation ID.

(If you have multiple installations of your app, there may be a way to get this programmatically; I haven't looked into this as I didn't need it for my use case.)

PEM file

This is how you prove to GitHub that you are in control of the App. Go back to the General page in the App settings, and scroll down to the Private keys section. Click the Generate a private key button; this will immediately generate a .pem file and download it to your machine.

Do not commit this to your repository unless you want everyone who can see the repository to be able to authenticate to GitHub as you.

The code

Once you have these three things, the steps you need in code are:

  1. Load your PEM
  2. Use the PEM to create a JSON Web Token that will authenticate your API call
  3. Call the GitHub API to get an installation token
  4. (Use the installation token to clone the repository of interest.)

Get the installation token

Code to do the first three steps could look like this:

from datetime import datetime
import jwt
import requests

def get_installation_access_token(
    pem_filename: str, app_id: str, installation_id: str
) -> str:
    """
    Obtain and return a GitHub installation access token.

    Arguments:
        pem_filename: Filename of a PEM file generated by GitHub to
                      authenticate as the installed app.
        app_id: The application ID
        installation_id: The ID of the app installation.

    Returns:
        The installation access token obtained from GitHub.
    """

    # With thanks to https://github.com/orgs/community/discussions/48186
    now = int(datetime.now().timestamp())
    with open(pem_filename, "rb") as pem_file:
        signing_key = jwt.jwk_from_pem(pem_file.read())
    payload = {"iat": now, "exp": now + 600, "iss": app_id}
    jwt_instance = jwt.JWT()
    encoded_jwt = jwt_instance.encode(payload, signing_key, alg="RS256")

    response = requests.post(
        "https://api.github.com/app/installations/" f"{installation_id}/access_tokens",
        headers={
            "Authorization": f"Bearer {encoded_jwt}",
            "Accept": "application/vnd.github+json",
            "X-GitHub-Api-Version": "2022-11-28",
        },
    )
    if not 200 <= response.status_code < 300:
        raise RuntimeError(
            "Unable to get token. Status code was "
            f"{response.status_code}, body was {response.text}."
        )

    return response.json()["token"]

Pass in the information collected above as the three parameters to the function. Note that this depends on the jwt and requests packages, both available under those names from pip.

This will give an installation token that is valid for an hour. (This is much less time than the PEM file is valid, because it has a lot less security. That's the reason this dance is needed—you're trading something pretty secure for something that is less secure but easier to use with git clone; because it's less secure, it has to be time limited instead to reduce the chance of it getting stolen.)

Clone the repository

Assuming that you have a repository URL in the form

repo_url = https://github.com/organization/repository_name

then you can clone the repository as:

import git

if not original_url.startswith("https://"):
    raise ValueError("Need an HTTPS URL")

auth_url = f"https://x-access-token:{token}@{original_url[8:]}"
git.Repo.clone_from(
    auth_url,
    deployment["tempdir_path"] / "repo",
    branch="deployment",
)

Here I've used the GitPython library for Python. Equivalently, you could use the shell command

$ git clone https://x-access-token:${TOKEN}@github.com/organization/repository_name

where ${TOKEN} contains the result of calling the above Python function.

Credits

Many thanks to loujr on the GitHub Community for the guide that eventually clued me into how to do this. I've stripped out the need to use command-line arguments and to manually pass the JWT into curl, instead keeping everything in Python.

🌐
GitHub
gist.github.com › farid-mkh › 5e1ebe40f9e0b5e827e2fb8278cbd2e1
How to clone a repo with github classic token · GitHub
In your server, go to a directory you want, then git clone https://{token}@github.com/{username}/{repo}.git
🌐
PyPI
pypi.org › project › github-clone
github-clone · PyPI
July 4, 2021 - To clone private repositories you need to supply an OAuth token for an account with access to the private repository (to get one see https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line).
      » pip install github-clone
    
Published   Jul 04, 2021
Version   1.2.0
🌐
GitHub
github.com › ezbz › gitlabber › issues › 97
Use access token to clone repo · Issue #97 · ezbz/gitlabber
March 9, 2022 - Is your feature request related to a problem? Please describe. Currently using --method http results in getting a question for username / password. Within GitLab it is however possible to use the access token (assuming it has read_repository permissions) to clone the repo.