Authenticate with GitHub using a token

GithubGithub Pages

Github Problem Overview


I am trying to authenticate with GitHub using a personal access token. In the help files at GitHub, it states to use the cURL method to authenticate (Creating a personal access token). I have tried this, but I still cannot push to GitHub. Please note, I am trying to push from an unauthenticated server (Travis CI).

cd $HOME
git config --global user.email "[email protected]"
git config --global user.name "username"

curl -u "username:<MYTOKEN>" https://github.com/username/ol3-1.git
git clone --branch=gh-pages https://github.com/username/ol3-1.git gh-pages

cd gh-pages
mkdir buildtest
cd buildtest
touch asdf.asdf

git add -f .
git commit -m "Travis build $TRAVIS_BUILD_NUMBER pushed to gh-pages"
git push -fq origin gh-pages

This code causes the errors:

> remote: Anonymous access to scuzzlebuzzle/ol3-1.git denied.

> fatal: Authentication failed for 'https://github.com/scuzzlebuzzle/ol3-1.git/'"

Github Solutions


Solution 1 - Github

Your curl command is entirely wrong. You should be using the following

curl -H 'Authorization: token <MYTOKEN>' ...

That aside, that doesn't authorize your computer to clone the repository if in fact it is private. (Taking a look, however, indicates that it is not.) What you would normally do is the following:

git clone https://scuzzlebuzzle:<MYTOKEN>@github.com/scuzzlebuzzle/ol3-1.git --branch=gh-pages gh-pages

That will add your credentials to the remote created when cloning the repository. Unfortunately, however, you have no control over how Travis clones your repository, so you have to edit the remote like so.

# After cloning
cd gh-pages
git remote set-url origin https://scuzzlebuzzle:<MYTOKEN>@github.com/scuzzlebuzzle/ol3-1.git

That will fix your project to use a remote with credentials built in.

>Warning: Tokens have read/write access and should be treated like passwords. If you enter your token into the clone URL when cloning or adding a remote, Git writes it to your .git/config file in plain text, which is a security risk.

Solution 2 - Github

First, you need to create a personal access token (PAT). This is described here: https://help.github.com/articles/creating-an-access-token-for-command-line-use/

Laughably, the article tells you how to create it, but gives absolutely no clue what to do with it. After about an hour of trawling documentation and Stack Overflow, I finally found the answer:

$ git clone https://github.com/user-or-organisation/myrepo.git
Username: <my-username>
Password: <my-personal-access-token>

I was actually forced to enable two-factor authentication by company policy while I was working remotely and still had local changes, so in fact it was not clone I needed, but push. I read in lots of places that I needed to delete and recreate the remote, but in fact my normal push command worked exactly the same as the clone above, and the remote did not change:

$ git push https://github.com/user-or-organisation/myrepo.git
Username: <my-username>
Password: <my-personal-access-token>

(@YMHuang put me on the right track with the documentation link.)

Solution 3 - Github

This worked for me using ssh:

SettingsDeveloper settingsGenerate new token.

git remote set-url origin https://[APPLICATION]:[NEW TOKEN]@github.com/[ORGANISATION]/[REPO].git

Solution 4 - Github

Automation / Git automation with OAuth tokens

$ git clone https://github.com/username/repo.git
  Username: your_token
  Password:

It also works in the git push command.

Reference: https://help.github.com/articles/git-automation-with-oauth-tokens/

Solution 5 - Github

I generated a token using the instructions from Creating a personal access token.

To actually use it, the following sequence worked for me:

git remote remove origin
git remote add origin https://[TOKEN]@github.com/[USER]/[REPO]
git push

Solution 6 - Github

Step 1: Get the access token

Go to this link: https://github.com/settings/tokens. And generate the token there.

Or from you Github account, Go to:
Settings -> Developer Settings -> Personal Access Tokens

Step 2: Use the token

git push

Username: <your username>
Password: <the access token>

Solution 7 - Github

To avoid handing over "the keys to the castle"...

Note that sigmavirus24's response requires you to give Travis a token with fairly wide permissions -- since GitHub only offers tokens with wide scopes like "write all my public repos" or "write all my private repos".

If you want to tighten down access (with a bit more work!) you can use GitHub deployment keys combined with Travis encrypted yaml fields.

Here's a sketch of how the technique works...

First generate an RSA deploy key (via ssh-keygen) called my_key and add it as a deploy key in your github repo settings.

Then...

$ password=`openssl rand -hex 32`
$ cat my_key | openssl aes-256-cbc -k "$password" -a  > my_key.enc
$ travis encrypt --add password=$password -r my-github-user/my-repo

Then use the $password file to decrypt your deploy key at integration-time, by adding to your yaml file:

before_script: 
  - openssl aes-256-cbc -k "$password" -d -a -in my_key.enc -out my_deploy_key
  - echo -e "Host github.com\n  IdentityFile /path/to/my_deploy_key" > ~/.ssh/config
  - echo "github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==" > ~/.ssh/known_hosts

Note: the last line pre-populates github's RSA key, which avoids the need for manually accepting at the time of a connection.

Solution 8 - Github

Normally I do like this:

 git push https://$(git_token)@github.com/user_name/repo_name.git

The git_token is reading from variable config in Azure DevOps.

You can read my full blog post here.

Solution 9 - Github

For macOS, if you are not prompted with a username and password request, it means your password is stored in Keychain Access. Every time you try to clone or push it will try to use your old password.

Follow these three steps to solve this:

  1. Generate a PAT (personal access token) - LINK
  2. Open KeyChain Access (Via spotlight search) → search GitHub → click GitHub → change and save with your new PAT link
  3. Try to push or clone again. Now you have stored the PAT instead of your password.

Solution 10 - Github

I'm on Ubuntu 20.04 (Focal Fossa) and I kept getting the message that soon I wouldn't be able to log in from console. I was terribly confused.

Finally, I got to the URL below which will work. But you need to know how to create a PAT (personal access token) which you are going to have to keep in a file on your computer.

Here's what the final URL will look like:

git push https://1234567890123456789012345678901234567890@github.com/user-name/repo.git

long PAT (personal access token) value -- The entire long value between the // and the @ sign in the URL is your PAT.

user-name will be your exact username

repo.git will be your exact repository name

Also you will be able to use it this way too:

When you do a

git push

1. You'll be prompted for a username and password

2. Just submit your username as normal

3. Now submit your PAT as your password and it will work.

You need to generate a PAT following the steps at: Creating a personal access token

That will give you the PAT value that you will place in your URL.

When you create the PAT make sure you choose the following options so it has the ability to allow you to manage your repositories.

PAT settings

Save Your PAT Or Lose It

Once you have your PAT, you're going to need to save it in a file locally so you can use it again. If you don't save it somewhere there is no way to ever see it again and you'll be forced to create a new PAT.

Now you're going to need at the very least:

  1. a way to display it in your console so you can see it again.
  2. or, a way to copy it to your clipboard automatically.

For 1, just use:

cat ~/files/myPatFile.txt

Where the path is a real path to the location and file where you stored your PAT value.

For 2

xclip -selection clipboard < ~/files/myPatFile.txt

That'll copy the contents of the file to the clipboard so you can use your PAT more easily.

FYI - if you don't have xclip do the following:

sudo apt-get install xclip

It downloads and installs xclip. If you don't have apt-get, you might need to use another installer (like YUM).

Solution 11 - Github

I previously used passwords to access my private repositories using Git CLI and had saved my credentials with git config --global credential.helper store.

As support for passwords has been dropped today, I couldn't manage to update my credentials with the token using the git config commands suggested.

If anyone else has this problem on Linux, you can manually update the ~/.git-credentials file, e.g.

nano ~/.git-credentials

Enter your token between the : and @ symbols. (To save and close the file, press Ctrl + O, Enter, Ctrl + X).

You might have to also run the following command after updating your token in the credentials file (see @guhur's comment):

git config --global credential.helper store

Note that by using Git's credential helper, anyone who has access to your home directory can see your token.

Solution 12 - Github

You can easily change your remote Authentication, first:

Remove your current origin:

git remote remove origin

And then:

git remote add origin https://<TOKEN>@github.com/<USERNAME>/<REPO>.git

You can find how to generate your Authentication Token here.

Solution 13 - Github

The following steps works for me:

  1. git remote remove origin

  2. git remote add origin https://[TOKEN]@[REPO LINK]

For example, my repo name is: https://github.com/username/codf.git.

The command will be:

git remote add origin https://[TOKEN]@github.com/username/codf.git

  1. git push origin branchName

Solution 14 - Github

Since I am using macOS, I can answer this for macOS specifically. We can use this for Windows also. It works!! In your GitHub account, click on the top right avatar and go to settings profile.

Click on Settings:

settings

Click on Developer settings:

Developer settings

Click on Personal Access Tokens:

Personal Access Tokens

Personal Access Tokens (Details)

And then click on Generate new token:

generate new token

Give a name to your access token and check on the first option.

Scroll down and click on generate token

Now, when you push the repo, use the following syntax:

git remote add origin https:<access__token>://@github.com/<username>/<repo__name>.git

git push https://<access__token>@github.com/<username>/<repo__name>.git

In my opinion, you can use the second option, while pushing the repo provide access token and you're good to go.

Solution 15 - Github

The only answer that helped me in a VS Code environment and a private GitHub.

git clone https://[MY_USER_NAME]:[GITHUB_PERSONAL_ACCESS_TOKEN]@github.com/davidsonlima/davidson-myrepo.git without square brackets

Solution 16 - Github

If you're using GitHub Enterprise and cloning the repository or pushing gives you a 403 error instead of prompting for a username/token, you can use this:

  1. Delete the repository

  2. Open a command prompt and navigate to the folder you want the repository in

  3. Type:

    git clone https://[USERNAME]:[TOKEN]@[GIT_ENTERPRISE_DOMAIN]/[ORGANIZATION]/[REPO].git
    

Solution 17 - Github

  1. git remote remove origin
    
  2. git remote add origin https://{accesstoken}:{accesstoken}@gitlab.com/{username}/{repo}.git
    
  3. git push https://{youraccesstoken}@github.com/{username}/{repo}.git
    

This works for me.

Solution 18 - Github

I had to add oauth or oauth2 as username to successfully authenticate:

https://oauth:<TOKEN>@github.com/user/repo.git

Solution 19 - Github

Lately github is not allowing commits directly from cmd using our username and password. For that we need to generate the accesss token as elaborated here.

And then use the same access token as username and password in the command prompt for git commands git push, git pull etc. For example

git push origin master
Username for 'https://github.com': lhq_4npmklMYXXXXXXXXXXXXXXXXXXXL8SxHxU
Password for 'https://[email protected]':<give same access token here as password too>

And you start to see the code logs as:

Enumerating objects: 24, done.
Counting objects: 100% (24/24), done.
Delta compression using up to 8 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (18/18), 6.33 KiB | 539.00 KiB/s, done.
Total 18 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (5/5), completed with 2 local objects.
To https://github.com/xxxxxxxxxxx/xxx-xxxxx-repo.git
123456..1233456  master -> master

Hope this helps someone. Happy Coding !!! :)

Solution 20 - Github

Having struggled with this issue for pretty much a full day, hard coding in the ORG/REPO section into our build script getting the dreaded 'remote not found' error, eventually I found a working solution by using TRAVIS_REPO_SLUG.

Switching this in for the hardcoded attributes worked immediately.

git remote set-url origin https://[ORG]:${TOKEN}@github.com/${TRAVIS_REPO_SLUG}

Solution 21 - Github

For those coming from GitLab, what's worked for me:

Prerequisite:

Create a token:

    1. Select the necessary permissions
    1. Select expiration date
    1. Generate by pressing create personal access token
  • Save the token!
Step 1.

Add a remote:

git remote add origin https://<access-token-name>:<access-token>@gitlab.com/path/to/project.git

Step 2.

Pull once:

https://<access-token-name>:<access-token>@gitlab.com/path/to/project.git

Now you are able to read/write to/from the repository

Solution 22 - Github

For Mac users:

  1. Open Keychain Access and find GitHub

  2. Right-click in GitHub

  3. Click delete

  4. Open the terminal and try to clone a private project

  5. Add the required values
    Username: $your GitHub username
    Password: $paste token here
    And hit Enter. Voilà - the token has been added.

Solution 23 - Github

For Windows:

  1. Open Credential Manager - Windows Credentials
  2. Find the entry of git:https://github.com, edit it
  3. replace your former password with the PAT access token
  4. Solved

Solution 24 - Github

I suffered with this problems but found a solution:

git push https://github.com/<username>/<project_name>
and
username: paste your personnal access token
password: paste your personnal access token

Solution 25 - Github

It can be done using github deploy keys which narrow access to a single github repo as well as making write permission optional.

Github deploy keys use a user generated ssh key using ssh-keygen which creates a private key file and a public key files.

Suppose the key name given ssh-keygen is key-test, and the private and public files live in ~/.ssh/key-test and ~/.ssh/key-test.pub respectively.

Suppose the github project name is keytest.

To add a deploy key to project via the github project web page, got settings/deploy keys and click add. Paste the contents of the public key file ~/.ssh/key-test.pub into the target box and confirm.

Modify the contents of your ~/.ssh/config file to include the following:

Host gh-keytest
        Hostname github.com
        IdentityFile=/home/user/.ssh/key-test

Note: gh-keytest is an arbitrary alias.

Now you can push using

git push git@gh-keytest:<githubaccountname>/keytest.git

To do it using only push

git remote remove origin  # in case origin is already set
git remote add origin git@gh-keytest:<githubaccountname>/testscope.git
git push --set-upstream origin main

Note: Replace main with the correct intended branch name.

Thereafter

git push 

is sufficient.

Solution 26 - Github

By having struggling so many hours on applying GitHub token finally it works as below:

> $ cf_export GITHUB_TOKEN=$(codefresh get context github --decrypt -o > yaml | yq -y .spec.data.auth.password)

  • code follows Codefresh guidance on cloning a repo using token (freestyle}
  • test carried: sed %d%H%M on match word '-123456-whatever'
  • push back to the repo (which is private repo)
  • triggered by DockerHub webhooks

Following is the complete code:

version: '1.0'
steps:
  get_git_token:
    title: Reading Github token
    image: codefresh/cli
    commands:
      - cf_export GITHUB_TOKEN=$(codefresh get context github --decrypt -o yaml | yq -y .spec.data.auth.password)
  main_clone:
    title: Updating the repo
    image: alpine/git:latest
    commands:
      - git clone https://chetabahana:$GITHUB_TOKEN@github.com/chetabahana/compose.git
      - cd compose && git remote rm origin
      - git config --global user.name "chetabahana"
      - git config --global user.email "[email protected]"
      - git remote add origin https://chetabahana:$GITHUB_TOKEN@github.com/chetabahana/compose.git
      - sed -i "s/-[0-9]\{1,\}-\([a-zA-Z0-9_]*\)'/-`date +%d%H%M`-whatever'/g" cloudbuild.yaml
      - git status && git add . && git commit -m "fresh commit" && git push -u origin master

Output...

On branch master 
Changes not staged for commit: 
  (use "git add ..." to update what will be committed) 
  (use "git checkout -- ..." to discard changes in working directory) 

modified:   cloudbuild.yaml 

no changes added to commit (use "git add" and/or "git commit -a") 
[master dbab20f] fresh commit 
 1 file changed, 1 insertion(+), 1 deletion(-) 
Enumerating objects: 5, done. 
Counting objects:  20% (1/5) ...  Counting objects: 100% (5/5), done. 
Delta compression using up to 4 threads 
Compressing objects:  33% (1/3) ... Writing objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done. 
Total 3 (delta 2), reused 0 (delta 0) 
remote: Resolving deltas:   0% (0/2)  ...   (2/2), completed with 2 local objects. 
To https://github.com/chetabahana/compose.git 
   bbb6d2f..dbab20f  master -> master 
Branch 'master' set up to track remote branch 'master' from 'origin'. 
Reading environment variable exporting file contents. 
Successfully ran freestyle step: Cloning the repo 

Solution 27 - Github

The password that you use to log in to github.com portal does not work in the Visual Studio Code CLI/shell. You should copy the PAT token from URL https://github.com/settings/tokens by generating a new token and paste that string in CLI as the password.

Solution 28 - Github

To update your remote repo with a new access token

git remote set-url origin https://{{your_username}}:{{your_new_token}}@github.com/{{repo_path}}.git

Solution 29 - Github

Select vcs → push tab from Android Studio. A popup would be displayed with username and password. Enter your username and instead of a password, enter the token number. It will get pushed to the repository.

Solution 30 - Github

  1. Clone your project -> git clone https://<[email protected]>//project.git
  2. In Project folder -> git config --global credential.helper cache

and work

Solution 31 - Github

After generating access token from Developer setting, run these commands, git push origin [branch] Username for 'https://github.com': [accessToken] Password for 'https://[accessToken]@github.com':[accessToken]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwayofthefutureView Question on Stackoverflow
Solution 1 - GithubIan Stapleton CordascoView Answer on Stackoverflow
Solution 2 - GithubEchelonView Answer on Stackoverflow
Solution 3 - Githubccreedon1View Answer on Stackoverflow
Solution 4 - GithubJohn HuangView Answer on Stackoverflow
Solution 5 - GithubScriddieView Answer on Stackoverflow
Solution 6 - GithubOmar MagdyView Answer on Stackoverflow
Solution 7 - GithubBoshView Answer on Stackoverflow
Solution 8 - GithubTony NgoView Answer on Stackoverflow
Solution 9 - GithubCharles OuverleauxView Answer on Stackoverflow
Solution 10 - GithubraddevusView Answer on Stackoverflow
Solution 11 - GithubundefinedView Answer on Stackoverflow
Solution 12 - GithubBrunnoView Answer on Stackoverflow
Solution 13 - GithubPing WooView Answer on Stackoverflow
Solution 14 - GithubIshan KesharwaniView Answer on Stackoverflow
Solution 15 - GithubDavidson LimaView Answer on Stackoverflow
Solution 16 - GithubGigazelleView Answer on Stackoverflow
Solution 17 - GithubFlame alchemistView Answer on Stackoverflow
Solution 18 - GithubMhmdrz_AView Answer on Stackoverflow
Solution 19 - GithubKailasView Answer on Stackoverflow
Solution 20 - GithubmdmjshView Answer on Stackoverflow
Solution 21 - GithubArtem KozlenkovView Answer on Stackoverflow
Solution 22 - GithubAshique BavaView Answer on Stackoverflow
Solution 23 - GithubPu1seView Answer on Stackoverflow
Solution 24 - GithubIbrahima BaView Answer on Stackoverflow
Solution 25 - GithubCraig HicksView Answer on Stackoverflow
Solution 26 - GithubeQ19View Answer on Stackoverflow
Solution 27 - GithubKanna ReddyView Answer on Stackoverflow
Solution 28 - GithubjawadView Answer on Stackoverflow
Solution 29 - GithubVinay BakleView Answer on Stackoverflow
Solution 30 - GithubCedric FlamainView Answer on Stackoverflow
Solution 31 - GithubMechaCodeView Answer on Stackoverflow