1 min to read
Configure Multi Git Accounts On Linux
Managing multiple user accounts can be a challenging task. A simple use-case scenario is to have an account for a job, and a separate account for personal purposes. Jumping back and forth between these accounts can be a pain, and you are prone to forget which account you are using and causing a lot of confusion. In general, it is recommended to use a single account for all your work, according to GitHub. But when it is not possible, you can have separate directories to host your git repositories for each account, so that you can easily switch between them by loading specific configuration files depending on the directory you are in.
Let’s assume you have a job account and a personal account, and that ~/.git_configs
is used to host configuration files. Also, your job’s repositories are in /projects/job
and your personal repositories are in /projects/personal
.
First, let’s fill the job account file.
cat > ~/.git_configs/job.config << EOF
[user]
name = Thomas A. Anderson
email = thomas.anderson@metacortex.com
[core]
sshCommand = ssh -i ~/.ssh/job.pem
EOF
Second, the personal account.
cat > ~/.git_configs/personal.config << EOF
[user]
name = Neo
email = the_choosen_one@matrix.com
[core]
sshCommand = ssh -i ~/.ssh/the_nebuchadnezzar.pem
editor = vim
EOF
The last thing to do is to load the configuration files from ~/.gitconfig
.
cat > ~/.gitconfig << EOF
[core]
editor = nano
[includeIf "gitdir:/projects/job/"]
path = ~/.git_configs/job.config
[includeIf "gitdir:/projects/personal"]
path = ~/.git_configs/personal.config
EOF
As a result, you will be able to use one account for your job, and a separate one for personal projects. Depending on the directory you are in, you will be able to load the correct configuration file automatically.
Comments