
If you are like me and use more than one github account on your computer, you must have experienced problems. Constantly changing accounts, adjusting configs... After a while, it gets annoying. That's why I wanted to share a solution I implemented today with you.
Note: What I explained is valid for the MacOS operating system.
# First we go to the ssh directory
cd ~/.ssh
# Here you enter your personal email address and file name.
ssh-keygen -t rsa -C "personal@mail.com" -f "personal-github"
# here you enter your work email address and file name
ssh-keygen -t rsa -C "work@mail.com" -f "work-github" # Here we add the ssh key of your personal github account
ssh-add -K ~/.ssh/personal-github
# Here we add the ssh key of your work github account
ssh-add -K ~/.ssh/work-github ### Let's copy our ssh key and save it to our personal github account.
pbcopy < ~/.ssh/personal-github.pubLet's add the copied code here from Github settings and save this code by saying "Add new SSH". Follow the same process for the next step.
### Let's copy our ssh key and save it to our work github account.
pbcopy < ~/.ssh/work-github.pub# Let's create and edit the config file.
touch ~/.ssh/config
open -e ~/.ssh/configIn the window that opens, let's add the following configurations.
#personal
Host personal-github.com
HostName github.com
User git
IdentityFile ~/.ssh/personal-github
#work
Host work-github.com
HostName github.com
User git
IdentityFile ~/.ssh/work-githubWe need to create a special config file for our work-related projects. To do this, we will keep all the work-related ones in one folder and pull the config file from there. In this way, when you switch to the work folder, you will work with different git information, and when you switch to your personal projects, you will work with a different git account. In this way, you can use your work and personal accounts in a healthy way without being interrupted.
# Go to your work folder and create .gitconfig
touch ~/work/.gitconfig
open -e ~/work/.gitconfigNow let's add the following configurations.
[user]
name = Berat Bozkurt
email = work@mail.comNow, as the last step, let's include the configurations we created in the work folder in our main .gitconfig file. To do this, simply add the following configuration.
# Let's open the main .gitconfig file.
open -e ~/.gitconfig
# Let's add these configurations in the window that opens.
[user]
name = Berat Bozkurt
email = personal@mail.com
[includeIf "gitdir:~/works/"]
path = ~/works/.gitconfigSit back and look at your code. Don't bother constantly switching accounts and logging in. Enjoy coding…
Regards!
Resources: