Multi-Github accounts through terminal
Intro
Cause my company requires all the employees to use the company’s Github account.
But sometimes I still need to use my personal account for different projects.
And I’m too lazy to set the git local setting for all the repo.
It’s kinda annoying that you need to set the config whenever you create a new repo or project.
So, Let’s fix it,
And Keep you lazy.
Prepare
- Github CLI
- Any Text Editor
- Nice terminal
Configuration
After Installing all the required tools.
Pop up your terminal, change the directory to your root (macOS/Linux)
If you’re windows user, please google where is your root git-config sit at.
There’s a hidden file name .gitconfig
( If not you need to create an empty file named .gitconfig
)
And replace its content by following below.
If you got more than 2 accounts,you can add the
includeIf
as more as you want.
1 | [includeIf "gitdir:<your-personal-directory-path>"] |
Example:
1 | [includeIf "gitdir:~/Documents/Aust1n/"] |
All my repo are sitting inside the Aust1n
folder.
So that all the repo will use the setting inside the .gitconfig.personal
.includeIf
is like an if-function for the config.
Then you need to create a new file named .gitconfig.personal
.
You can naming the config file with any suffix
The content looks like below
1 | [user] |
After finishing all the config.
Your .gitconfig
will follow by your working directory.
Authentication
If you want to push your code, you still need the authentication for Github.
Here comes the Github-CLI.
Pop up your terminal Again, and change the directory to your root/.config/gh
If you’re windows user, please google where is your github-cli config sit at.
(If not you create a folder file named gh
)
Then you need to touch (create) four files in gh directory.
(If files exist, you should modify the content)
1 | touch config.yml |
Put the below content into the config.yml
1 | # What protocol to use when performing git operations. Supported values: ssh, https |
Put the below content into the host.yml.<suffix>
with suffix.
Each suffix stands for a different account.
Docs for GitHub personal token
Remeber check all the checkbox when you creating the Github personal token
1 | github.com: |
Please remind there’s a space after colon
Otherwise it won’t work
Then you can switch the account by using the command below:
1 | gh personal |
If the terminal shows the below info:
github.com
✓ Logged in to github.com as 0xaust1n (oauth_token)
✓ Git operations for github.com configured to use ssh protocol.
✓ Token: *******************
Then set up the GitHub-CLI for authentication by using the command below:
1 | gh auth setup-git |
Now it’s work. Enjoy
TLDR version
- Edit
.gitconfig
1 | code ~/.gitconfig |
Add below content
1 | # Each account must using different path |
- Create config for all suffix
e.g.: I got work and personal suffix
thus I need to create.gitconfig.work
and.gitconfig.personal
- Add below content into each config file
1 | [user] |
- Install gh cli
1 | # bash |
- Add config for gh cli
1 | # path of location (If path does not exist, please make the dir) |
- Put the below content into the
host.yml.<suffix>
with suffix.
1 | github.com: |
- Put the below content into the
config.yml
1 | git_protocol: ssh |
- Switch Account
1 | gh <suffix> |
- Set up cli
1 | gh auth setup-git |