Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log in to GitHub from the command line with multiple accounts

Tags:

git

github

I opened a new GitHub account to separate my business vs. personal repositories.

Now, I git init my local repository and git add remote origin <the repository HTTPS URL>

I try to push, and it seems to always take the credentials of my original account, not prompting me for the credentials for the new account.

I tried using the URL format with

https://<username>:<password>@github.com/<username>/<repository.git>

but that doesn't seem to help: I still get an error that the credentials are invalid for the original account username.

How do I log in with multiple sets of credentials or how would I somehow reset the original credentials to force password prompt when pushing?


The only way I managed to push right now is by specifying the username:[email protected]/ in the URL in the git push command.

like image 989
MrE Avatar asked Jan 11 '16 21:01

MrE


People also ask

Can I use SSH key for multiple GitHub accounts?

GitHub does not allow us to use the same SSH key in multiple accounts, so we'll have to create separate keys for each account. We can create SSH keys and add them to our SSH agent by following this guide from the GitHub Documentation.


1 Answers

git config --global credential.helper cache

... which tells git to keep your password cached in memory for (by default) 15 minutes. You can set a longer timeout with:

git config --global credential.helper "cache --timeout=3600"

More useful links:

https://confluence.atlassian.com/bitbucketserver/permanently-authenticating-with-git-repositories-776639846.html

Using the .netrc file The .netrc file is a mechanism that allows you to specify which credentials to use for which server.

This method allows you to avoid entering a username and password every time you push to or pull from Git, but your Git password is stored in plain text.


You can store your credentials using the following command

git config credential.helper store
git push http://example.com/repo.git
# Now type username and password and it should be saved by git.
like image 109
CodeWizard Avatar answered Sep 19 '22 19:09

CodeWizard