Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch between user identities in one Git on one computer [duplicate]

Tags:

git

github

I have ONE repository on GitHub, let's call it Repo-1.

I want to first access that repository as a default Git user.

Let's call that user User-1.

I created SSH keypair, everything fine, works nice.


I made ANOTHER repository on GitHub, let's call it Repo-2.

I didn't make any changes in local Git, on my laptop. No configurational changes, nothing.

Now - I want to clone from Repo-1 as the User-2 (but from the same laptop).

First of all: is this at all possible to do?

Can local Git on one single laptop switch between "user accounts" and present itself as User-2? And then, from THAT identity, clone from Repo-1, make some change, and then push to Repo-1?

If possible, how do I do that?

like image 440
Martin Avatar asked Feb 19 '12 07:02

Martin


People also ask

Can you have two git accounts one computer?

By creating different host aliases to github.com in your ~/. ssh/config, and giving each host alias its own ssh key, you can easily use multiple github accounts without confusion. That's because github.com distinguishes not by user, which is always just git, but by the ssh key you used to connect.

How do I switch between users in git?

gitconfig file into the . git folder (and rename it to "config") and just change the lines you want to change (probably github. user and github. token) or you create a new file with just the two lines in it.


2 Answers

You have your global .gitconfig where you already configured your SSH Keys/User Information. The global .gitconfig is overridden by a local gitconfig - the file "config" in your .git folder (if it does not exist you might have to create it).

For example you can copy the .gitconfig file into the .git folder (and rename it to "config") and just change the lines you want to change (probably github.user and github.token) or you create a new file with just the two lines in it.

If you prefer the command line "git config" you can avoid all the file moving stuff by omitting the "--global" option.

like image 109
Moe Avatar answered Sep 22 '22 05:09

Moe


You need to determine if you actually have two ssh keypairs, or just two emails you want to use. An ssh keypair is linked to accounts as described here.

The ssh keypair (specifically the private key), basically gives your git client permission to connect to github, and thus permission to push. This is separate from the user identity, which is just the email in your commit messages.

If you have two ssh keypairs, each linked to one account, follow these instructions to create a ~/.ssh/config file. The key part is to use a different ssh psuedo-host for each account:

# Default GitHub user (joe) Host github.com   HostName github.com   User git   IdentityFile /Users/joe/.ssh/id_rsa  # Client user (client) Host github-client   HostName github.com   User git   IdentityFile /Users/joe/.ssh/id_rsa_client 

You then use two corresponding remotes:

git clone [email protected]:joe/my_repo.git 

and

git clone git@github-client:client/his_repo.git 

If you just want to use two emails, you can just give each clone a separate .git/config with the desired [user] settings.

like image 39
Matthew Flaschen Avatar answered Sep 25 '22 05:09

Matthew Flaschen