Skip to content

Instantly share code, notes, and snippets.

Created March 26, 2014 18:43
What would you like to do?
Github Two-Factor Authentication Failed For HTTPS

I heard from GitHub Two-Factor Authentication]( nearly a couple of days ago when I was reading my RSS feed. I enabled it and couldn' push to any of my repositories anymore. Learn in this blog post how to fix it.

Two-Factor Authentication

"Is a process involving two stages to verify the identity of an entity trying to access services in a computer or in a network". Github solves this authentication with sending an SMS to a device which wants to push to their platform.

Enabling Two-Factor Authentication

  1. Go to your Account Settings.
  2. Set up two-factor authentication.
  3. You'll be given the option of setting up 2FA either through a text message, or through an app you can download onto your smartphone.

Once you type in the number on your github page, your account is verified.

Setting Up Personal Access Token

Since you have enabled 2FA, you can create a personal access token.

  1. Go into your Account Settings.
  2. Click on Applications - this is where you can find the a section where you can create your "Personal Access Token"
  3. Save the password in some encrypted file.

Testing your Personal Access Token


$ curl -u <token>:x-oauth-basic

If everythings works fine then you should get following json output:

  "login": "matthias-guenther",
  "id": 264708,
  "avatar_url": "",
  "gravatar_id": "9172bb642e29e9959f078f329308faa1",
  "url": "",
  "html_url": "",
  "followers_url": "",
  "following_url": "{/other_user}",
  "gists_url": "{/gist_id}",
  "starred_url": "{/owner}{/repo}",
  "subscriptions_url": "",
  "organizations_url": "",
  "repos_url": "",
  "events_url": "{/privacy}",
  "received_events_url": "",
  "type": "User",
  "site_admin": false,
  "name": "Matthias Günther",
  "company": "",
  "blog": "",
  "location": "Berlin",
  "email": "",
  "hireable": true,
  "bio": "software developer, writer, hiker, jogger, and mobile apps lover",
  "public_repos": 64,
  "public_gists": 11,
  "followers": 54,
  "following": 65,
  "created_at": "2010-05-04T16:46:36Z",
  "updated_at": "2014-03-26T04:43:54Z",
  "private_gists": 0,
  "total_private_repos": 0,
  "owned_private_repos": 0,
  "disk_usage": 57682,
  "collaborators": 0,
  "plan": {
    "name": "free",
    "space": 307200,
    "collaborators": 0,
    "private_repos": 0

If something went wrong, you should get a message like the following:

  "message": "Not Found",
  "documentation_url": ""

Pushing To An HTTPS Github URL On Your Own

I had all my repositories checked out via HTTPS. But after enabling 2FA, you can't push to this repositories anymore.

$ git remote -v
  origin (fetch)
  origin (push)

$ git push origin master
  fatal: '' does not appear to be a git repository
  fatal: Could not read from remote repository.

  Please make sure you have the correct access rights
  and the repository exists.

I tried every combination of passwords, personal access token and even created a new ssh-key, but it won't work. I had to change the remote URL to and it worked.

Pushing To An HTTPS GitHub URL On An Organization

I'm the maintainer of and pushing my changes with the git@* remote URL hack did not work out very well:

$ git remote -v
  origin (fetch)
  origin (push)

$ git push
  fatal: '' does not appear to be a git repository
  fatal: Could not read from remote repository.

  Please make sure you have the correct access rights
  and the repository exists.

Most posts out there advices to use osxkeychain to save your credentials. Since I'm using Xubuntu for developing I had to search after another method.

Use .netrc File To Store Credentials

The .netrc file contains login and initialization information for managing the auto-login process.

All you have to do is to setup your crdentials in ~/.netrc:

login matthias-guenther
password <token>
protocol https

login matthias-guenther
password <token>
protocol https

Where <token> is your personal access token. It would be silly to save your password in plain text.

Encrypt .netrc file with gpg

I assume that you already have your gpg key, you need to run the following command:

$ gpg --encrypt --armor --recipient .netrc

And update the credentials helper:

$ git config --global credential.helper "netrc -f ~/.netrc.asc -v"

Now you can push again.

Copy link

in the docs for Github 2F Auth, there's some info on how to use on the command line.

Basically, for https, use Personal Access Token, generated at The ssh solution described here works, but if you still want to use https, good to go

Thanks for the gist, got me working again quickly!

Copy link

adsteel commented Aug 10, 2015

Looks like there's a simpler way. After turning on two step authentication:

  1. Create a personal access token.
  2. When you go to push your repo, paste that token instead of typing your password

Copy link

akauppi commented Oct 12, 2015

+1 for @adsteel. Worked for me.

Copy link

@adsteel , thanks , works for me :)

Copy link

interestingly it did not work for me, though the longer tips above did. I wonder if I was pasting it wrong or what.

Copy link

For me, the issue was the remote origin URL. I had to change it to git@ in order for my SSH key to work. No password I used ever had an effect.

Copy link

ItGumby commented Jan 3, 2016

It appears I just had to change the repo url. Your gist helped me resolve that. Thanks!

Copy link

Why would entering the access token every time you want to do anything with the git remote be easier than just using the method this Gist covers?

Copy link

+1 for @adsteel. Also worked for me

Copy link

I was just running into a similar problem with a repo from one of my organizations. I also have two-factor authentication enabled.
I used my username and the personal access token that was created for the organization and then the following solution worked for me: git remote set-url

This was based upon the section in this gist called Pushing to an HTTPS Github URL on your own

This was on Windows 10 x64, using Git Shell (PowerShell). Once I'd updated the remote URL, I was then able to use Github for Windows to push/pull.

Copy link

thanks @adsteel!

Copy link

Thank you, this solved my problem.

Copy link

Thanks @adsteel, Personal Access Token as password works smoothly on https.

Copy link

BYVB commented Feb 27, 2017

+1 @adsteel, worked for me

Copy link

quantuminformation commented May 10, 2017

Personal Access Token worked for me thx!

Copy link

Thanks mate! Probably saved me some hours of trying and failing. 😄 👍

Copy link

I Googled for hours trying and failing, and this post was the only one explaining how to do 2FA.
Now works like charm ;)

Shame on GitHub for not providing a decent documentation about this topic :(

Copy link

@adsteel thanks a lot this worked perfect.

Copy link

eckucukoglu commented Oct 18, 2017

yes we can use this personal access token as a password, however it is hard to remember, not safe to write it down.
i used to cache my github pw with a timeout. ( I believe re-authenticate the github session (that last max ~6h for me) for each time is more secure way. after 2-way authentication is enabled, it is almost impossible to use this feature.

Copy link

bonekost commented Feb 2, 2018

Thanks. Saved hours ;)

Copy link

Copy link

sio4 commented Feb 21, 2018

FYI, when I use personal access token, it requires scope 'repo'.

Copy link

It worked for me. Thank you so much @adsteel. It was really helpful.

Copy link

ghost commented Apr 9, 2018

This information is no longer relevant. There is no "Personal Access Token" section.

Copy link

intere commented Apr 20, 2018

@rquinlivan - there is a section for this "Personal Access Tokens", it's just moved:

Copy link

check this

Once you have a token, you can enter it instead of your password when performing Git operations over HTTPS.

Copy link

Both ways work fine for me:

  1. Changing the URL (HTTPS -> SSH) with the command:
    git remote set-url origin
  2. Using the access token as the password.

Thanks @adsteel for sharing! =]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment