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?

3
  • When did you create User-2? Do you have two SSH keypairs? Commented Feb 19, 2012 at 7: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 = [email protected] [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
    Commented Feb 19, 2012 at 7:54
  • I ran into the same problem, that I had to switch between different users/authors for the commits on my machine. Thus I coded small nodejs tool to enable me to quickly switch the user via the command line like this. Here github is a predefined config pair: gituser github If anyone is interested, check this out: github.com/juocal/git-config-switcher
    – Webchen
    Commented Jan 15 at 1:11

2 Answers 2

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.

3
  • 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
    Commented Feb 19, 2012 at 9: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: help.github.com/articles/…
    – AndyO
    Commented Sep 7, 2017 at 10:03
  • Yes, the last sentence, but more specifically, here is link to the docs which actually says where each file lives. git-scm.com/book/en/v2/Customizing-Git-Git-Configuration Commented Dec 10, 2022 at 20:38
57

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 [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.

10
  • 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
    Commented Feb 19, 2012 at 9: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. Commented Feb 19, 2012 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
    Commented Feb 19, 2012 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. Commented Feb 19, 2012 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
    Commented Feb 20, 2012 at 9:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.