7

I want to practice using GitHub doing pull requests and learning about how to see git difference between different users. How do I set up another user account on my terminal in macOS to do this? How do I switch between users?

3
  • Just make two clones of the repo. Commented Oct 30, 2016 at 2:16
  • What do you mean by "different users"? Different accounts on a remote hosting platform such as GitHub? Different committer/commit author identities? Or simply two clones of the same repository so you can push and pull back and forth as if they were on two machines? Commented Oct 30, 2016 at 9:38
  • I want different author identities so I can make pull requests and practice authorizing the changes or giving feedback on pull requests Commented Oct 31, 2016 at 2:00

3 Answers 3

2

There are three aspects to acting as a second user.

1. GitHub Account

To use the GitHub web interface as another user (e.g. fork a repository, submit a pull request, post comments) you need to sign in to another account.*

Tip: Switching between accounts is a pain because you have to sign out and sign in each time. You can sign in to two accounts at the same time using a private browsing window, a different browser, or a different browser profile.

2. SSH Authentication

A GitHub repository can be accessed over HTTPS or SSH. Both require authentication, which GitHub uses to implement permission levels. I'll describe how to clone a repository with SSH configured to authenticate as a second user.

Generate a new SSH key using ssh-keygen -f KEYFILE where KEYFILE is the path to the new key (e.g. ~/.ssh/bob_rsa).

Add the SSH key to the GitHub account of the second user.

SSH needs to be configured to use ~/.ssh/bob_rsa, but only when you are trying to clone a repository as the second user. That way you can still clone repositories as your normal user with an SSH key you added to your normal GitHub account. Different configurations can be specified based on the host name, but for GitHub repositories the host name is always github.com. To specify configurations for only some of the cloned repositories, add a host alias by appending the following to ~/.ssh/config (credit):

# alias for github.com with a custom SSH key
Host bob.github.com
    HostName github.com
    IdentityFile ~/.ssh/bob_rsa

I've used the host name bob.github.com, but it can be any string.

Now you can clone a GitHub repository as the second user using the host name bob.github.com (or whichever host name you used in the SSH configuration):

git clone [email protected]:USER/PROJECT.git

If you clone a repository owned by your first user in this way, you should not be able to push commits to it until you add your second user as a collaborator.

Testing the SSH Configuration

If you encounter problems, check that SSH works by running

ssh [email protected]

(replace bob.github.com with the host name in the line Host XXX).

The first time you connect to GitHub over SSH, you should get a message like

The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)?

Type yes, then hit ENTER. (For the security-conscious, first check that the fingerprint is listed on GitHub's SSH Keys page).

If your configuration is correct and you added the SSH key to the second GitHub account by pasting the contents of ~/.ssh/bob_rsa.pub in the "SSH and GPG Keys" page, you should see

PTY allocation request failed on channel 0
Hi USER! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

USER should be the name of the second account.

If you've also set up SSH access for your normal account, you should be able to run

ssh [email protected]

and get the same message but where USER is the name of your normal account.

3. Git Author Metadata

Git stores the author's name and email address with each commit. GitHub uses this information to display user avatars in the commit history for example. It is trivial to spoof this information (1, 2).

The author's name and email address are usually stored in the global configuration file (~/.gitconfig). You can override them on a per-repository basis by running the following in the repository's directory:

git config --local user.name "NAME"
git config --local user.email "EMAIL"

Replace NAME and EMAIL with the full name and email address of the second user. The --local flag modifies the per-repository configuration file (.git/config in the repository's root directory), as opposed to the --global flag which modifies ~/.gitconfig. The default is --local, so actually it could be omitted.

Now you have a clone where you are effectively the second user. Use another (normal) clone to work as the first user.


*Fine print: GitHub's Terms of Service only allow one free account per person.

Sign up to request clarification or add additional context in comments.

7 Comments

can you explain what the host name does? is that the name that is registered as the author who clones the repo? what do you mean when you said "SSH needs to be configured to use ~/.ssh/bob_rsa, but only for repositories cloned as the second user."?
how does git clone [email protected]/USER/PROJECT.git work? usually the clone address is "github.com/Jacky-Lei/banana-app.git" so if i tried to do git clone [email protected]/Jacky-Lei/banana-app.git it didn't work
git clone [email protected]/Jacky-Lei/banana-app.git doesn't work either - it says repository doesn't exist
@stackjlei: I made a typo, it should be git clone [email protected]:USER/PROJECT.git (colon instead of slash). The host name bob.github.com is used to apply special SSH configurations. It can be any string, as long as you use the same thing in the Host XXX line of the SSH config file and in the command git clone git@XXX:USER/PROJECT.git .
The idea is to have two SSH keys, one (e.g. ~/.ssh/id_rsa) associated with your normal account and another (e.g. ~/.ssh/bob_rsa) associated with your second account. To clone a repository as the normal user, SSH needs to authenticate with the normal key (~/.ssh/id_rsa). To clone a repository as the second user, SSH needs to use the second key (~/.ssh/bob_rsa). The easiest way to tell SSH when to use the second key is to configure a special host name (e.g. bob.github.com, but it can be any string).
|
1

You probably set up an existing user with commands like

git config --global user.name <a username>

and

git config --global user.email <an email>.

You can make a separate clone to push and pull from a different folder. To have the commits show up under separate usernames depending on the folder, use

git config --local user.name <another username>

and

git config --local user.email <another email>

from within the second clone.

The --local option ensures that this only affects the second copy.

Comments

1

I used a slight improvement on the tom's proposed solution
(credit)

to allow multiple users withing the same git workspace :

  1. I create hostname aliases for each users, by creating an entry for each user in the ~/.ssh/config file :
# aliases for github.com with different SSH private keys (identities)
Host *.github.com
    HostName github.com
    FingerprintHash md5
    User: git
    IdentityFile ~/.ssh/id_rsa # (default git ssh-key)

Host alice.github.com
    IdentityFile ~/.ssh/alice_rsa
Host bob.github.com
    IdentityFile ~/.ssh/bob_rsa

note: I am using wildcard in the ssh hostname within the ~/.ssh/config file for default git user identity, and because gitHub display its key w/ MD5 fingerprints, I use FingerprintHash md5

  1. if I haven't done so, I start an ssh-agent : eval "$(ssh-agent -s)"

then I load the key to the ssh-agent w/ ssh-add ~/.ssh/alice_rsa;, verify it with ssh-add -E md5 -l;, then for testing it I use the ssh's -T flag:

ssh -v -T alice.github.com (note you have to have your public keys loaded on github side: https://github.com/settings/keys , and each key can be ONLY used "once" (i.e. for only one gitaccount), so if you already have ssh access to other git account you need to cut a new key)

  1. I create repository aliases for each hostnames :

    for this, I edit my .git/config w/ the following to change the origin's url:

[remote "origin"]
#  url = git@{{sshkeyowner}}.github.com:{{gitaccountname}}/{{gitreponame}}.git
   url = [email protected]:michel47/bin.git
  fetch = +refs/heads/*:refs/remotes/origin/*
[remote "alice"]
   url = [email protected]:michel47/bin.git
   fetch = +refs/heads/*:refs/remotes/alice/*
[remote "bob"]
   url = [email protected]:michel47/bin.git
   fetch = +refs/heads/*:refs/remotes/bob/*

note: if you don't want to edit your .git/config file, you can achieve the same with the command (make sure you substitute the proper repository's names):

git remote remove origin
git remote add origin [email protected]:michel47/bin.git
git remote add bob [email protected]:michel47/bin.git
git remote add alice [email protected]:michel47/bin.git
  1. all in all, it allows me to switch users for all the git command by specifying the remote-repository name :
git pull alice master
git push orign
# etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.