Search code examples
gitgithubvisual-studio-codewindows-10

How to manage github credentials with Windows 10?


I often need to switch from a github account to another (professional / personnal) to clone a repo locally and then publish modifications to an other. I'm logged in github via VSCode and cannot switch from an account to an other : when I log out and then log in, the same github account is used.

I need to browse Windows parameters to delete credentials here :

Windows Credentials manager

Is there a way to avoid this ?


Solution

  • There is a special setting that will store the credentials on a per-repo basis instead of per site.

    You may need to clear any previously stored credentials first though:

    Run git config --get credential.helper, then depending on the output:

    • manager or manager-core: run the following command:
      echo "protocol=https`nhost=github.com" | git credential-manager erase
      
    • wincred: run the following command:
      cmdkey /delete:LegacyGeneric:target=git:https://github.com
      

    Or manually delete all the credentials from your Windows Credential Manager.

    Configure the credential manager for per-repo tokens

    Then run the following command to instruct github to use the whole clone URL instead of just the domain to store credentials:

    git config --global credential.https://github.com.useHttpPath true
    

    Now re-authenticate to GitHub, your credentials should be stored on a per-repo basis. It means you'll get a few more credential prompts after the switch, but after a while you'll have a token stored per repo and won't need to switch at all anymore.

    This assumes you are using the Git Credential Manager, which can be downloaded from here.

    Add the User to the clone url

    If I'm remembering correctly, you should also be able to add the desired username to your clone url:

    git clone https://[email protected]/jessehouwing/my-repo
    

    Should store a separate credential than the one it stored for:

    git clone https://[email protected]/git-ecosystem/git-credential-manager
    

    You can update the current remote for your repo and add the user in:

    git remote --verbose
    origin  https://github.com/owner/repo (fetch)
    origin  https://github.com/owner/repo (push)
    
    # Take the full url from the output and add your user:
    git remote --set-url origin https://[email protected]/owner/repo