Deedee vs. multiple Git accounts
My goldfish brain's attempt to not forget this once and for all
Free-range data Deedee using organic braincells, ❌ AI zone.
Every time switching back and forth between multiple Git accounts on my local machine, I run into goldfish syndrome: forgetting everything and end up wasting time debugging things that should have been… easy.
Funnily, Git docs do have instructions about these things - but they can be scattered and … confusing (for instance they will guide you on how to do A B C but there is little clue on when to do A B C, and whether A B C is relevant to your immediate need right now is something you must have some wisdom to navigate).
Since I have no wisdom, I usually end up on some good old stackoverflow1 threads to resolve. That’s also a lot of typing and reading for my liking. So here is for my future goldfish brain, a step by step takeaway. If you are a fellow goldfish, join along!
Assumptions
Using MacOS
Following the SSH way, not HTTPS
Already have git profiles configured in
~/.ssh/config
file like so2
The set up
For easy demo, let’s say these are the accounts we are working with
Scenario 1: New project to remote Git
Ok so my scenario is usually like this:
I have created a new project in IDE
I need to initialize it with git and then push to remote (but, since I have multiple git accounts: Github work vs personal, Gitlab Bitbucket whatever etc.) you can imagine that without due diligence I can be using wrong Git profile to plug into the desired Git platform/account. Practically this can not happen thanks to Git yelling at me in lower-cased letters in the terminal, but you get the idea.
Hence, there are few important things to keep in mind:
I must first verify what Git account I’m using
After git init, I must make sure the remote repo is the desired one before pushing
Commands
Now, let’s say I have configured globally my Git setting to be using work account by default, when I’m working on something that uses personal Git account, I would need to make sure it’s switched properly.
# Step 1: initialize git for your project | |
cd your_repo_path | |
git init | |
# Step 2: check your git config and auth | |
ssh -T git@me.github.com | |
# Hi deedee! You've successfully authenticated... | |
# Step 3: make sure your remote Git is the right Git account | |
# create your new_project repo on your intended Git account first | |
git remote set-url origin git@me.github.com:deedee/new_project.git | |
git push -u origin main | |
# Step 4: business as usual, init commit | |
git add . | |
git commit -m "init project" |
At step 3, in case you want to create repo directly from terminal instead of Github UI:
gh repo create deedee/new_project --public --source=. --remote=origin --push
Scenario 2: Cloning a remote repo
Things should be straightforward now that you get the gist above. The key is to always verify and make sure you use the right host identifier.
Suppose you need to clone the project (notice this is the SSH way) git@github.com:deedee/dexter_pranks.git
Commands
# Step 1: Check your git auth | |
ssh -T git@me.github.com | |
# Step 2: Clone your project, use the right host identifier | |
cd your_project_path | |
git clone git@me.github.com:deedee/dexter_pranks.git |
The important thing to note is the host identifier, if you type without the “me” in our example, it will follow the global config of your git profile, which is whatever you have set as default … globally.
Tada 🎉, you are done. Things are where they should be, and you can continue working on the repo with the right credential. Every time you need to switch profile, use the ssh -T command, combined with the host identifier you have set.
For you gen Alpha Beta Omega people, it’s where us millennials and older gens used to hang out to guide each other to enlightenment before ChatGPT came along.
If you have not, follow these in this order: generate a new SSH key, then add it to your ssh-agent, which will get you the ssh config file with multiple profiles.