Home > Software engineering >  Force password entry before each GitHub push in VS Code on macOS
Force password entry before each GitHub push in VS Code on macOS

Time:03-19

How can I force VS Code to ask for my macOS Keychain password before each push to GitHub?

VS Code never asked for my GitHub password (via Keychain) until I pushed to GitHub and it asked before each push again.

This was working until a few hours ago when I had some issues and revoked my access and VS Code requested a new token.

Now VS Code does not ask at all for a password. Even when I remove VS Code from the Keychain whitelist, it asks for my password at program start only once and then not again, even not for pushes.

I want VS Code to request my GitHub token before each push from Keychain again.

CodePudding user response:

After comparing my current macOS login Keychain with one from a Backup where everything was still working as I wanted, I found a solution:

There seem to be at least two ways of how VS Code accesses the Keychain:

  1. Directly via OAuth. This is used when VS Code automatically requests an access token via the GitHub website. It stores the token in the Keychain with the name vscodevscode.github-authentication.

  2. Indirectly via git-credential-osxkeychain and a personal access token that is stored in the Keychain with the name github.com.

VS Code seems to decide automatically which way it uses depending on which keys it finds in the macOS Keychain.

I have not found a way how to force VS Code to ask for the Keychain password on each push when using OAuth, but it is working via git-credential-osxkeychain now.

Solution

  1. Request a personal access token via GitHub / Settings / Developer settings / Personal access tokens

  2. Retreive your GitHub ID via: https://api.github.com/users/<your user name>

  3. Open Keychain Access and create a new password item:
    Keychain Item Name: https://github.com
    Account Name: <your GitHub ID>
    Password: <your personal access token>

Notes:

  1. If VS Code asks for your Keychain password multiple times, try a push via command line

    • click on Always Allow on the request that asks to ...access key "github.com"...
    • click on Allow on the request that asks to ...use your confidential information stored in "github.com".
  2. This is a complete "try and error" solution, I have not found much information about that. It would be great if someone could explain what is actually going on here. I have also not found any official information about setting up VS Code via git-credential-osxkeychain.

  3. I guess git needs to be installed to make that work. I installed it via Homebrew

  4. At least for me this solution works for pushing via VS Code GUI, VS Code integrated terminal and OS terminal.

CodePudding user response:

Check your git config --global credential.helper: it references the program in charge of caching your credentials.

A git config --global --unset credential.helper would remove it, forcing Git to always ask for your credentials.

  • Related