Reputation: 20768
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.
Upvotes: 45
Views: 462862
Reputation: 710
In Github I went to Settings | Developer Settings | Tokens (classic) and generated a new token. (Link is https://github.com/settings/tokens)
When I clone a new repository, I use a token, e.g.
git clone https://USERNAME:[email protected]/workspaceID/reposlug.git
Once I've done this, it lets me push to Github without using the token.
Upvotes: 4
Reputation: 11
One way is to go to Control Panel > User Accounts > Manage Your Credentials > Windows Credentials and then look for git:https://github.com. Then click the drop down arrow besides it and click Remove.
Then, run the command git push -u origin master and it will ask you to login via the Web Browser. Enter your credentials on the GitHub website that pops-up and grant permissions and you are good to go!
Upvotes: 1
Reputation: 374
You are looking for configuring multiple account git using on your local machine. I will provide a solution used for Mac, Linux and Window using bash.
Be awared before start, [email protected] would be your email to be added in github developer setting later, [email protected] is used for testing in cased abort pushing to remotely.
In your project, remember to set config up
// You will need setting for this email in ssh later
git config user.email "[email protected]"
git config user.name "some_name"
Firstly, generate your own ssh within your email
ssh-keygen -t -C "[email protected]"
And then it will ask you where to locate this generated key:
Enter file in which to save the key (/home/some-user/.ssh/id_rsa):
You can custom the generate path file as you wish to, for example:
Enter file in which to save the key (/home/some-user/.ssh/id_rsa): /home/some-user/.ssh/first-key
In this example, I will generate two pair, first for [email protected] & second for [email protected]
First would be: id_rsa // Account: your_email, email: [email protected]
Second would be: first-key // Account: your_email_2, email: [email protected]
And then just simply accept other options, then you will have a pair private, public key. You can check in .ssh folder by:
cd ~/.ssh && ls -a
Now you would see in your terminal your pair, look something like:
id_rsa.pub id_rsa
// And first-key
first-key.pub first-key
Then you would need to add these pub keys into the developer setting in your github: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account
Now last but not least, you need to setting up your ssh config so that when you push your code, ssh can detect which account you are using and then will point to the correct file
cd ~/.ssh
touch config // If not have
nano config // Or using Vim
And now you need to paste the following config
\\ When you clone via ssh, your remote url would be in this format:
\\ git clone [email protected]:githubAccountName/repoName.git
\\ Edit in config file of .ssh
\\ Host github.com
\\ HostName github.com
\\ IdentityFile ~/.ssh/id_rsa_personal
\\ User githubAccountName
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa // Your path point to private key
User your_email // Put your account here
Host github.com
HostName github.com
IdentityFile ~/.ssh/first-key // Your second account
User your_email_2 // Put your account or email here
Now you can try with your your_email as you added your key in developer settings, if you change your config to your_email_2, you cannot push to remote because you haven't add your ssh-key yet.
// You will need setting for this email in ssh later
git config user.email "[email protected]"
git config user.name "some_name_2"
For more reference, you can take a look at this if my answer doesn't help: https://gist.github.com/jexchan/2351996
Upvotes: 2
Reputation: 12821
The accepted answer is deprecated.
Alternatively you can try
git push https://[email protected]/user/repo.git
then provide in the prompt the token generated in github
or add to github/localSsh your ssh key as indicated in here
Upvotes: 3
Reputation: 60
Since authentication using username & password has been discontinued by Github since August 2021, we can authenticate only through SSH key or Personal Access Token.
I tried the Personal Access Token option. But while using git push
command, there was no prompt to enter the PAT. Here's what worked for me(Windows 10).
In your cmd/Git Bash/power shell, enter
git config user.name "your_username"
git config user.email "your_email"
After doing this go to Credential Manager, and update the credential for Github with the new username & password. Demo Image
Now, if you try the git push
command, it will prompt you to enter your PAT for authentication.
You can generate your PAT by going to your [Github profile]->Settings->Developer settings.
Upvotes: 0
Reputation: 291
In your shell, add your user name: git config --global user.name "your_username"
Add your email address: git config --global user.email "[email protected]"
To check the configuration, run: git config --global --list
Open bash or Terminal.
cd ~/.ssh
ls
There should be these two files: id_rsa and id_rsa.pub. These are login credentials.
If the email in id_rsa.pub does not match the email you want to use, then make new credentials. To keep things simple, use 'id_rsa' for the file in which to save the key and make a passphrase to save for later
ssh-keygen -t rsa -C "[email protected]"
Save your passphrase somewhere safe to use when you push, or you will need to do this again.
vim ~/.ssh/id_rsa.pub
Copy the contents of id_rsa.pub and paste it into GitHub website under the Account Settings > SSH and GPG Keys. Copy all of it, exactly as it appears, with no extra spaces or lines. Give these credentials a name to remind you which machine it is from.
Try git push again
If you get a warning about permissions being too open Permissions 0644
then try these:
chmod 400 ~/.ssh/id_rsa
~/.ssh/config
points to the correct private keyUpvotes: 13
Reputation: 1215
But I have an idea, log into Github:
one via terminal, the other via Github Desktop APP.
Upvotes: -1
Reputation: 10867
From the docs:
If you omit --global or use --local, the configuration is applied only to the current repository.
In your shell, add your user name:
git config user.name "your_username"
And your email address:
git config user.email "[email protected]"
To check the configuration, run:
git config --list
And to compare to your global configuration, run:
git config --global --list
https://docs.gitlab.com/ee/gitlab-basics/start-using-git.html
Upvotes: 1
Reputation: 321
Use this command to relogin with a different account on GitHub:
git remote set-url origin "remote repository URL"
Upvotes: 4
Reputation: 20768
The only way I managed to push was by specifying the username:[email protected]/
in the URL in the Git push command.
Upvotes: 22
Reputation: 1331
You can use the Git configuration inside the repository you are using. In ${local_repo_folder}/.git/config
, add your user details there. That way you can configure which user to use on a per-repository level.
Upvotes: 5
Reputation: 141946
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:
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.
Upvotes: 26