how to use a secondary github account from the terminal

introduction

This has been a big headache for me for a long time. I want to use more than one github account. It is ridiculously difficult. For some reason none of of the various instructions I followed worked, until this one did. Links at bottom to various other resources on the same subject.

This is set up per-repo. More clever automated solutions exist, see links at bottom. But they never worked for me.

The instructions that finally cracked it for me: Quick Tip: How to Work with GitHub and Multiple Accounts

This is not comprehensive at explaining how to use github, git or the terminal from scratch. It is just to fill in existing knowledge. Also I assume you do everything the same way I do. If you don’t like it feel free to return to the original tutorials.

Do it with an empty repo and make sure everything has gone correctly because if you screw it up it’s really not so easy to backtrack.

Note replace the emojis

πŸ… = your secondary username

πŸ₯• = repo name

create a new ssh key for use with new account in ~/ssh/id_ed25519-πŸ…Β and add it to the keychain

add to ~/ssh/.config:

Host github-πŸ…
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_ed25519-πŸ…

Create new repo locally:

❯ mkdir πŸ₯•
❯ cd πŸ₯•
❯ git init
Initialized empty Git repository in πŸ₯•/.git/

Make a tiny nothing commit:

❯ git commit -m "init git"
On branch main
Initial commit
nothing to commit (create/copy files and use "git add" to track)

Login to github and create πŸ₯• repo there

  • Be sure to create the README.md

Sync local and remote:

❯ git remote add origin git@github-πŸ…:πŸ…/πŸ₯•
❯ git pull origin main
From github-πŸ…:πŸ…/πŸ₯•
 * branch            main       -> FETCH_HEAD
❯ ls
README.md

open the README.mdΒ make some tiny change and save it.

❯ nano README.md

edit your local πŸ₯•/git/configΒ to add (use your correct commit email)

[user]
	name = πŸ…
	email = 000000+πŸ…@users.noreply.github.com 
[pull]
	rebase = false
[branch "main"]
	remote = origin
	merge = refs/heads/main

send local files to remote

❯ git add README.md
❯ git commit -m "change readme local"

❯ git push origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 260 bytes | 260.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github-πŸ…:πŸ…/πŸ₯•
   b2a484f..9ef3ae5  main -> main

troubleshooting

if you check your ID via ssh, it will still say your main account:

❯ ssh -T git@github.com
Hi CouldBeThis! You've successfully authenticated, but GitHub does not provide shell access.

If you are having problems, here is how to determine which file is being applied

❯ git config --show-origin user.name
❯ git config --show-origin user.email

Using git with multiple profiles and GPG+SSH keys | markentier.tech

official documentation