92

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?

Konrad Borowski
  • 11,584
  • 3
  • 57
  • 71
Martin
  • 1,276
  • 1
  • 10
  • 13
  • When did you create User-2? Do you have two SSH keypairs? – Matthew Flaschen Feb 19 '12 at 07:27
  • Hi Matthew! Guess I do. I have just managed to do this, still not sure if this is done right. I went directly to ~/.gitconfig (in Windows it's C:\Users\Martin\.gitconfig) and changed this parameters: [user] name = USER 2 email = user2@blabla.com [github] user = user2. I didn't change the token - I left it as it was. Then I added the user1 repo to Git (having user2 identity). Cloned. Changed the file. Staged, commited and pushed. Went to GitHub Repo1 page and there it was: a commit from another user. HOWEVER... does this approach at all makes sense? – Martin Feb 19 '12 at 07:54

2 Answers2

82

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.

Erik Pragt
  • 13,513
  • 11
  • 58
  • 64
Moe
  • 1,356
  • 10
  • 13
  • 5
    Nice, thank you. I have just tested it. Two folders (same level, one by another, folder1 and folder2). In folder1, default settings in config file. In folder2 changed settings, including GitHub username. Started GitBash in folder1, clone-...-push, GitHub sees a commit from defalut user. Started GitBash in folder2, clone-...-push, GitHub sees a commit from OTHER user. Works like a charm, no need to alter global settings. Different folders, different users. Just what I needed. Cool, thanx again! – Martin Feb 19 '12 at 09:18
  • IMO, the last sentence in this answer is key. Because it seems cleaner to me to let git handle the workflow instead of messing around with the files yourself. Here's by the way a nice guide on git config related to this very topic: https://help.github.com/articles/setting-your-commit-email-address-in-git/#setting-your-email-address-for-a-single-repository – AndyO Sep 07 '17 at 10:03
  • Yes, the last sentence, but more specifically, here is link to the docs which actually says where each file lives. https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration – SacWebDeveloper Dec 10 '22 at 20:38
56

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 pseudo-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 git@github.com: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.

Aage
  • 5,932
  • 2
  • 32
  • 57
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • Okay, thanx. This is a nice little tutorial, just what I needed! Anyways, what about GitHub usernames? I added to .git/config files two different GitHub usernames, and now GitHub shows commits from two different users. Does it mean that I've already created two SSH keypairs in the previous days (I frankly don't remember cause I messed around a lot)...? If I hadn't, GitHub wouldn't have accepted commits from the second user, am I thinking right? – Martin Feb 19 '12 at 09:59
  • @Martin, you could have two different emails that you're pushing to github through one account. That's probably what's happening if you're not using two identity files. – Matthew Flaschen Feb 19 '12 at 10:12
  • Oh, now this last answer is about approaching two different GitHub repositories... did I get it right? That was not the question, though. The question was how to access one single GitHub repository and present yourself as 2 different users. Like me and my friend both use the same laptop, and are working on the same project (same GitHub repo), only in the morning I clone and push as Martin, and in the evening he clones and pushes as John. Same local computer, with one local Git installation, but two identities approaching one remote repo. – Martin Feb 19 '12 at 10:19
  • 2
    @Martin, no, it's about authenticating to GitHub with two different accounts, each with their own private key. I think you are committing with different emails, but connecting to GitHub as one user. – Matthew Flaschen Feb 19 '12 at 21:52
  • This is what it says on the "history" page... most of the commits say "user1 authored a day ago", but 2 of them say "user2 authored a day ago", and the user profile picture is different. And when I click on "user2", it takes me to the second account page. Can this be just due to different emails? – Martin Feb 20 '12 at 09:15
  • @Martin, yes. According to [these docs](http://help.github.com/set-your-user-name-email-and-github-token/), "Git tracks who makes each commit by checking the user’s name and email. In addition, [GitHub] uses this info to associate your commits with your GitHub account." If you go to Account Settings > Click "SSH Public Keys" on each account, do they each have SSH keys? If so, the instructions in my answer provide a way to use them. That would allow the two users to have totally separate push permissions (imagine if they worked at different companies). – Matthew Flaschen Feb 20 '12 at 15:02
  • @Martin, remember that the pusher is not necessarily the committer. Say I make some commits, then push them to you (over a private network, or email). Then, you push them to GitHub. I am recorded as the committer, but you're the one whose push permissions are relevant to GitHub. Once the commits are on GitHub, they'll be associated based on my email. – Matthew Flaschen Feb 20 '12 at 15:07
  • Hmmm, alright. Obviously I didn't get how GitHub worked quite well. I'll check this out and follow up with how it went. Thanks again for this support... – Martin Feb 21 '12 at 18:56
  • Hey guys, I just automated the process of switching both remote hosts (`github.com` to `github-client`) and user.email in one single comment. Here are [the details](http://arnaudrinquin.github.io/worflow/2014/03/11/fast-github-account-switch/) – Arnaud Rinquin Mar 12 '14 at 12:46
  • @ArnaudRinquin Broken link – Dave Liu Sep 02 '16 at 22:19