2

I have oauth2 access_token for user with scope repo which means I can basically access all public/private repos of the user. I have a local copy of a repo on my machine and I want to pull (fetch and merge) using the authenticated user's token in github api.
How do I manage this?

2 Answers2

2

An OAuth access token, (like PAT (Personal Access Token)) can be used from command-line:

That OAuth token can be used as a password when GitHub asks for your credentials (username/password) for an HTTPS URL.

Therefore, make sure the remote URL is an HTTPS one, with <theuser> username in it

    cd /path/to/repo
    git remote -v
    # if origin starts with git@github.com (SSH)
    git remote set-url origin https://<theuser>@github.com/<theuser>/<repo>

Whenever you clone/pull/push a private repo from <theuser>, enter the OAuth access token as a password.

Or check your git config credential.helper: you can cache those credentials, using for instance the GCM (Git Credential Manager) like one for Mac/Linux, or for Windows.

Note that git merge is a local-only operation, so it won't require any credentials.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • As mentioned above, I only have the user's oauth access token, unless I can use it to get the PAT (which I don't think is the case) then it won't help me. – Anwesh Mohapatra Mar 31 '20 at 16:45
  • 1
    @GokuAfrica Still, try and use that access token as password for an HTTPS URL, and see if it is accepted as password. – VonC Mar 31 '20 at 16:47
  • Yes it really worked! Thank you. Now I feel so stupid. I must remember now that the oauth 2 token can be substituted for the password for the given user – Anwesh Mohapatra Mar 31 '20 at 17:17
  • please edit the answer to mention oauth2 token rather than PAT token. State that it can be used as a replacement for password. See my own answer to the question. Make those changes and I'll gladly accept your answer – Anwesh Mohapatra Mar 31 '20 at 17:45
  • I want all branches to pull from master when pushed directly to master branch. Is there any solution? – Dmytro Myronenko Nov 26 '20 at 19:27
  • @DmytroMyronenko pull from when push to? I am not sure to fully understand. Can you make this a separate question with more details? – VonC Nov 26 '20 at 22:33
0

you can not do it locally, but I think it can be done as from docs

git pull is shorthand for git fetch followed by git merge FETCH_HEAD

you can achieve this by following the steps in order

  1. create branch
  2. create a new commmit
  3. update branch to point to new commit
  4. merge the new created branch with you want pull in
Moazzam Arif
  • 298
  • 1
  • 5
  • The merge will just merge code from the new branch to existing branches, it has nothing to do with pulling latest code from repo to local machine using the access_token – Anwesh Mohapatra Mar 31 '20 at 15:25
  • https://developer.github.com/v3/repos/contents/, this will get you the content, but don't think so you can merge it locally using github api – Moazzam Arif Mar 31 '20 at 16:09
  • Yeah, I saw the contents api. The best solution I got was using the api to download the zip of the entire repository whenever a code merge happens to master but I don't think that's really viable for me. – Anwesh Mohapatra Mar 31 '20 at 16:46