Managing authentication for multiple github accounts
Recently Github stopped accepting passwords for authentication for fetch/push operations to remote repo. In regard with change Github suggested developers either use Personal Access Token(s) or PAT(s) or ssh for authenticating to remote repo.
I followed up with instruction given on their page for creating PAT, it worked well. For more details refer below in references section #1
This worked until I had to do some operations on my personal Github account repo which is separate from my work Github account. All my commits were being pushed under my work email not my personal email, part reason for that is I had set my global config pointing towards work credentials. The thing I wanted to achieve was to set username and email for repo individually and do not worry for authentication while doing fetch/push operation.
Firstly I removed my global config of username and email I had set earlier by running the following command
$ git config --global --edit
I removed the user block where my credentials were set. By doing this step you enforce the git to use the local credentials set for individual repo rather than using global credentials. If you don't have this don't worry, you can follow the next step.
Next, set user name & user email on your local repo, initially this will take time if you have too many repos, but once done its a one time thing to do. Run the following command to set values for both in the root folder of repo.
$ git config user.name "Your Name Here" $ git config user.email email@example.com
During my research how to handle this efficiently, I stumbled upon Git credential manager core (refer #2). Taken from official docs.
Git Credential Manager Core (GCM Core) is another way to store your credentials securely and connect to GitHub over HTTPS. With GCM Core, you don't have to manually create and store a PAT, as GCM Core manages authentication on your behalf, including 2FA (two-factor authentication).
So I installed GCM core by running this commands. I am using macOS so installed using homebrew, link in references has instructions for Windows and Linux.
$ brew tap microsoft/git $ brew install --cask git-credential-manager-core
Once the above step is done, in any event of fetch/push operations it will do this, taken from official docs.
The next time you clone an HTTPS URL that requires authentication, Git will prompt you to log in using a browser window. You may first be asked to authorize an OAuth app. If your account or organization requires two-factor auth, you'll also need to complete the 2FA challenge. Once you've authenticated successfully, your credentials are stored in the macOS keychain and will be used every time you clone an HTTPS URL. Git will not require you to type your credentials in the command line again unless you change your credentials.
This way I have different credentials for multiple github accounts.