Auto-switch between multiple git accounts based on the platform
Background
The motivation is pretty simple: I want to be able to use different Git accounts automatically within the same local environment, depending on which Git platform I am pushing to.
I think a lot of people run into this scenario. In my case, my personal Git account usually goes through Github, while the code hosting platform at work is Bitbucket and uses a separate company-issued Git account. This creates a problem: if you are not using an open-source tool like SourceTree to manage the company Git, what other ways can you keep your local Git accounts from getting tangled up? This article walks through the steps to solve it from 0 to 1.
Process
Install git
On a fresh Mac, Git might not be installed yet, so you should test it first. Just type git --version in the terminal. If it shows command not found, then you need to install it yourself.
I am used to managing tools installed on my Mac with Homebrew, so let’s install that first (you can also check the official site). Type the following in terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then you can install git through Homebrew:
brew install git
Create separate git accounts and ssh keys
Suppose your personal email is user@gmail.com and the work-issued email is employee@company.io. You can create separate git accounts and generate different SSH keys for each:
Generate the personal SSH key:
ssh-keygen -t rsa -b 4096 -C user@gmail.com
You can rename the file to make them easier to tell apart, for example renaming it to mine. The naming step is at Enter file in which to save the key:, where you can type mine to name this SSH key.
Same thing for the company SSH key:
ssh-keygen -t rsa -b 4096 -C employee@company.io
Then rename the output SSH key to company.
Write a config file for each platform
I want my personal git account to be used on github and the company account on bitbucket. First, register each public key on the corresponding platform. You can refer to each platform’s official docs for the detailed steps. To copy a public key, you can use the following command:
cat (id)_rsa.pub
In the example above, to copy the personal public key, use cat mine_rsa.pub, and for the company public key, use cat company_rsa.pub.
After registering each ssh key on the respective platform, you can create a local config file to use different git accounts depending on the platform:
- Go to the directory where the ssh keys are stored
cd ~/.ssh
- Create a config file inside
touch config
Then edit the config file. You can use vim or vscode, whichever you prefer. The content should look like this:
# Github // personal github Host github.com HostName github.com User git IdentityFile ~/.ssh/mine_com_rsa # Private GitLab instance // private setup Host bitbucket.org HostName bitbucket.org User git IdentityFile ~/.ssh/company_com_rsa
That is basically it. Now you can clone a project for each, and inside the project terminal type git config --list to see whether the current git account is the personal or the company one.
Ref
- GPT
ChangeLog
- 20230521-init
- 20260501–translate by claude code