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?
There are three aspects to acting as a second user.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With