By Filippo Iovine (20 novembre, 2018) in git, windows
Having some problems to set multiple github accounts on my windows machine and with the target to not use any SSH approach, I chose to play with GIT credential helpers to fit the best one for my needs.
Final solution, in my case, was to set GIT using git-credential-store helper: a credential helper that stores GIT credentials locally on disk.
Be aware that probably a credential helper is already in place in your GIT environment and probably that is GIT Credential Manager (GCM). That’s because GCM is installed alongside GIT for Windows that updates GIT system config file accordingly (credential.helper=manager is added into the GIT system config file, usually located in \Program Files\Git\mingw64\etc\gitconfig).
Clean GIT config file from previous helpers
Open a dos CLI as administrator and unset helper from GIT system config:
git config --system --unset credential.helper
Set new credential.helper=store into global config file:
git config --global credential.helper store
Finalize GIT to handle multiple github accounts.
To complete the job, you need to set remote url to https like below or edit local git config file (.git/config) looking for [remote … ] sections and add missing USERNAME@:
git remote set-url origin https://USERNAME@github.com/USERNAME/REPO.git
Now push to force git asking for credentials:
With the store credential helper, GIT stores credentials in the GIT credential file (usually located in %HOME%\.git-credentials) in a unencrypted format (password included).
So be careful with that and protect credential file at least by filesystem permissions.
Why can we not use GCM to handle multiple github accounts?
GCM saves GIT accounts (encrypted) credentials into Windows Credential (WC) wallet database.
When GCM is set and GIT requests credentials:
- GCM is invoked, a PAT (Personal Access Token) is requested to github and saved remotely in the Settings/Developer settings/Personal access tokens section on your github account.
Locally PAT account data is stored in your WC wallet as PersonalAccessToken/[PASSWORD] user with URI key internet/network address=git:https://github.com.
- In case 1) won’t be performed (or interrupted) then GIT command line asks for credentials (USER NAME and PASSWORD) to locally saves those in your WC wallet as [USER]/[PASSWORD] account with URI key internet/network address=git:https://github.com.
Performing same operation with another github account, GCM stores account info using again same URI internet/network address=git:https://github.com overwriting previous one (WC can save one account per URI).
Finally, GCM does not support multiple users per Uri (as reported in Microsoft/Git-Credential-Manager-for-Windows:issue 363).