How to switch accounts for multiple Gits from the same PC

Asked 2 months ago, Updated 2 months ago, 2 views

Thank you for your help.

I used to create a Git account and used it on my private PC for a while, but recently I started using it for work.

This time, I would like to create a separate Git account, create a separate private account, a separate repository, but when I push on my private PC, it is recognized by the user name I created before (currently using for work), and I cannot push it.

Please let me know if anyone knows how to separate Git accounts on the same PC.

Thank you for your cooperation.

git github

2022-09-30 14:23

4 Answers

It depends on whether you use SSH or HTTPS...

If this problem occurs with HTTPS, I think the credential helper remembers the credentials (username/password).In this case, you can have another credential stored by putting the username in the URL.

 git clone https://github.com/user_a/aprojectX
 ↓
git clone https://[email protected] /user_a/aprojectX

See also

In this case, the user is identified by the private key and must use the private key differently.

Depending on your environment, if you use git push/pull in the command, you are probably using openssh for SSH connections.Then you can specify a private key for each destination in .ssh/config.

For example, .ssh/config should include:

Host github-private
    HostName github.com
    IdentityFile~/.ssh/id_rsa_private

If you use the github-private determined above instead of github.com as the hostname,

 git clone [email protected]:user_a/projectX
 ↓
git clone git @github-private:user_a/projectX

~/.ssh/id_rsa is used instead of ~/.ssh/id_rsa_private.

For Github only,

  • Register a work account as a Collaborator in a private repository
  • Register your work public key to your private account (if SSH)

and so on.

See also

You can rewrite the URL you want to clone by hand, or, for example, if you want to apply it to a Github user, you can automatically replace it by generating the entry url.<base>.insteadOf in gitconfig.You can replace it from the HTTPS URL.

 [url"[email protected]:user_a/"]
    installOf=https://github.com/user_a/

Note Rewrite Git Repository URL Settings "insteadOf" and "pushInsteadOf"-kakakakukublog

In either way, the name and email at the time of commitment must be changed separately.This is a function called conditional inclusions, and you might want to set it to ~/private/ and below to apply this gitconfig...

Reference Use includeIf to replace git config for each project - kakakaku blog


2022-09-30 14:23

There is no description of the operating system, but how about dividing the users on the operating system?


2022-09-30 14:23

For your information, using the Pro version of GitKraken, it seems that you can switch between multiple accounts.
(GUI Client for Windows, Mac, and Linux)

Frequently Asked Questions

How can I use multiple GitHub/Bitbucket accounts with GitKraken?
However, with GitKraken Pro's multiple profile support, you can easily switch between profiles that each have their own associated GitHub and BitBucket accounts.


2022-09-30 14:23

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.