1

So I have 2 github accounts, 1 for work and 1 for me, currently I've changed my main account to work by deleting keychain access, after this using any git command asks you to enter credentials.

But how to switch between accounts depending on project? I want to have 1 github account in 1 project and 2nd in another. Using commands like

git config user.email "you@example.com"
git config user.name "Your Name"
git config user.password "your password"

Does not do anything

AlmaG3st
  • 192
  • 9

2 Answers2

1

As you authenticate with your ssh-keys, you should use a different ssh-key for each account.
You then need to configure the ssh-key used for pushing changes, which then again determines the account to authenticate against github.
You can do this, by setting a different core.sshCommand for each local repository.

The following example assumes repo1 is using ~/.ssh/key1 and repo2 is using ~/.ssh/key2.
In repo1, you set core.sshCommand to use ~/.ssh/key1 like this.

git config --local core.sshCommand "ssh -i ~/.ssh/key1"

In repo2, you set core.sshCommand to use ~/.ssh/key2 like this.

git config --local core.sshCommand "ssh -i ~/.ssh/key2"

To configure core.sshCommand while cloning a repository, use the following command

git clone -c core.sshCommand="ssh -i ~/.ssh/key1" git@github.com:repo1/repo1.git

Note that this must be done for every local copy of the github-Repository, and also may not working if not using a Unix operating system or the git bash for windows.

Also see this answer

Mime
  • 1,142
  • 1
  • 9
  • 20
0

Below could be the possible solution if I got your question correctly :

  • You should use HTTPS in this case.

  • change remote url to https with

     git remote set-url origin https://USERNAME@github.com/USERNAME/PROJECTNAME.git
    
  • and you are good to go :)

  • To ensure that the commits appear as performed by USERNAME, one can setup the user.name and user.email for this project, too:

    git config user.name USERNAME
    
    git config user. Email USERNAME@example.com
    
  • A note : if you already set your user.name and user.email with the --global flag, just do what I said above to set it locally for just that one repo.

Hope it will work :)

source