170

Inside a script I am trying to clone a GitHub repository with an OAuth token.

According to this tutorial:

https://github.com/blog/1270-easier-builds-and-deployments-using-git-over-https-and-oauth

I should be able to build a command for it like this:

git clone https://<token>@github.com/owner/repo.git

If I try this manually with a proper access token, it still asks for my password.

If I try it on the commandline I am simply getting a repository not found error.

The article is from 2012 and I cannot find any API documentation for this. So I am wondering if this still works.

NoobTW
  • 2,243
  • 2
  • 23
  • 39
Stephan-v
  • 17,343
  • 25
  • 103
  • 194

8 Answers8

155

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

git clone https://oauth-key-goes-here@github.com/username/repo.git
Community
  • 1
  • 1
Andris
  • 2,968
  • 1
  • 18
  • 15
  • 3
    Hate to be that guy, but, This was really helpful, thank you very much . I am working on a Matlab container that needs a script to run but that script changes a lot and I don't want to keep rebuilding the containers each time, with this I can just clone what I need to run there instead of having a fixed script. – Gilberto Treviño Mar 09 '21 at 17:00
  • 1
    You can also use github Personal Access Token with this command in place of OAuth Key. – Aung Htet Jan 26 '22 at 02:26
102

I turned out to be a scope issue. I of course needed full repo scope since I was trying to clone a private repository.

It's a shame Github does not have some clearer error messages for these kind of things, but security wise I understand why.

For anyone trying to figure out what is wrong when trying out something like this, I would suggest to create a personal access token with full access to everything:

settings > developer settings > personal access tokens > generate new token

This way you can easily test if it is a scope issue by comparing your token with a personal access token that has access rights for everything.

Thanks for anyone who still took the time to read this.

Stephan-v
  • 17,343
  • 25
  • 103
  • 194
  • 2
    Mind that if you simply try to access the repo page through a browser you'll still face a 404-not found page. But cloning worked fine regardless in my case. – Herick May 15 '18 at 20:35
  • I provided a scope of `user repo` to the authorization request, and when requesting an access token it always returns with `scope=` (empty scope). Why is that? – Era May 06 '20 at 21:46
  • It may seem that there is no way to avoid write access using a Github security token, which is kind of a security limitation for scripts and other integrations where just cloning a private repo is required :( – matanster Jan 29 '22 at 17:53
51

Just clone the repository with HTTP like so:

git clone https://github.com/myuser/myrepo.git

When prompted for Username, fill your username.

When prompted for Password, fill the token instead.

Amit Levy
  • 835
  • 7
  • 9
24

Do whatever works for you from these two choices

In your terminal

$ git clone your_repo_url Username:your_token Password:

... there is no password

In your git client app

i.e. Sourcetree, GitKraken, and the GitHub client.

Enter your repo_url (obvsiously without the '$ git clone part')

Username:your_token Password:

... there is no password

OR i.e. in Sourcetree, open preferences and then go to advanced, enter the hostname (i.e. www.x.com) and userName (i.e. your_token)

enter image description here

t.ios
  • 836
  • 9
  • 16
21

Please try this.

git clone `https://oauth2:TOKEN@github.com/username/repo.git`

For example, git clone https://oauth2:ghp_...Gnm61dm4rh@github.com/gituser/testrepo.git

hobbydev
  • 1,059
  • 13
  • 29
  • 2
    it's working. please check the url carefully. @github.com/gituser/testrepo.git – hobbydev Sep 14 '21 at 18:09
  • 4
    This is way better than manually writing the username and password! I can save a github access token variable and user a script with `git clone https://oauth:$GITHUB_TOKEN@github.com/user/repo.git` or just add the token itself to a script. Underrated answer here. – JakeAve Oct 09 '21 at 02:27
  • I get ` remote: Invalid username or password` when using an organization repo :( `git clone https://oauth2:$GITHUB_TOKEN@github.com/MyOrg/repo.git` – A Kingscote Jan 12 '22 at 08:56
  • @AKingscote, please check if your key is correct and not expired. – hobbydev Jan 17 '22 at 09:37
18

go to https://github.com/settings/tokens and generate a new token, remember to enable access to the repo, after that, you can do the following to clone the repo.

git clone https://<token>@github.com/owner/repo.git

Note: owner is your username if it's your repository else keep username of repository owner(one with all the rights of repo).

sfsf9797
  • 380
  • 3
  • 12
2

In .net core you can do in this way when dealing with Azure DevOps Repo:

 public void CloneRepository()
        {
            var _gitURL = "URLofGitRemoteRepository";
            var _userName = "PersonalAccessToken";
            var _pswd = ""; //Keep it blank

            var co = new CloneOptions();
            co.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = _userName, Password = _pswd };

            Repository.Clone(_gitURL, filePath, co);
        }
Insa
  • 21
  • 3
1

For me none of the answers above worked. It turns out I had set an expiration of one month on my token so I had to recreate the token using the instructions here: https://www.shanebart.com/clone-repo-using-token/

Ayudh
  • 1,291
  • 14
  • 39