Hi All, In this article, we will discuss how to setup the multiple git accounts.
it can be a scenario that you have more than one GitHub profiles. One can manage for your personal project and you may work with the organization and have other profile for managing the work related repositories
So let's get started. For managing multiple Github accounts you need to create the ssh key.
So the main idea behind ssh key is to generate the private and public keys. the public key will be set to your github account. So when you connect your machine to the GitHub so the combination of the private and public keys is used for authentication with GitHub. After authentication, we can get work with Github for cloning and performing the other operations. So for the demonstration, I have two accounts Account one is my personnel account github.com/priyanhsu10
and another one is github.com/priyanshu1990
Step -1: Create SSH Keys for personnel account
Open terminal
» ssh-keygen -t ed25519 -C "your_personnel@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/priyanshuparate/.ssh/id_ed25519):personnel_id
once you generate the SSH key you can see the private and public key is generated. .shh folder with the name personal_id personal_id.pub personal_id contains the private key and personal_id.pub will contain the public key which needs to be set to your personal account.
Step -2. Set the ssh key to your git hub account Got to setting->
Then go to SSH and. GPG keys . Create a New ssh key and name at your convenience. I gave the title as "personnel" Copy your public key from personnel_id.pub file and paste it in key textbox and create the key.
step -4 Create SSH Keys for Work account
» ssh-keygen -t ed25519 -C "your_Work@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/priyanshuparate/.ssh/id_ed25519):work_id
step- 5 Set the SSH to your other account
Step -6 Configuration for the account on your system. Got .ssh folder to create a file with the name config with no extension (you can use touch command on Mac or Linux on (windows you can create using the command line . like open cmd and type command --> type nul > filename ) now my .ssh folder contains files
~ » ls .ssh priyanshuparate@Priyanshus-MacBook-Air
config personal_id work_id
known_hosts personal_id.pub work_id.pub
Lets first configure our personnel account in the config file
#personel account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/personal_id
IdentitiesOnly yes
in above configuration, the host is the github.com where aur repositories will reside. HostName is a very important tag. It identifies from which account your repository linked for example I have maintion github.com and i have repository git basics with ssh URL. git@github.com:priyanhsu10/gitbasics.git
So while cloning I will run the git clone command
git clone git@github.com:priyanhsu10/gitbasics.git
so in this case it will connect this repository to my personnel account. Suppose i make HostName tag value github.com-test then while cloning i need to run the command with appending -test to git@github to connect personnel account
git clone git@github.com-test:priyanhsu10/gitbasics.git
IdentityFile ~/.ssh/personal_id line specify the private key file which is used for authentication purpose as we discuss before.
Now lets to the configuration for the work account in the config file Add Hostname value as github.com-work
#work account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/work_id
IdentitiesOnly yes
so the config file contains
#personel account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/personal_id
IdentitiesOnly yes
#work account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/work_id
IdentitiesOnly yes
Step -7. Username setup for proper reference of username while committing.
Create folder work in any were and add .gitconfig file for the work profile. I am creating a work folder in the document directory
~/Documents » mkdir work priyanshuparate@Priyanshus-MacBook-Air
--------------------------------------------------------------------------------
~/Documents » cd work priyanshuparate@Priyanshus-MacBook-Air
--------------------------------------------------------------------------------
~/Documents/work » touch .gitconfig
Configuration in ~/Documents/work/.gitconfig
[user]
email =work-account@office_mail.com
name = work_account_name
example
[user]
email =priyanshuparate10@gmail.com
name = Priyanshu1990
Step -8 Update the Global .gitconfig file which is present in your home folder paste be below lines of code under the your work account (if you not see your account then just add in the file ) [includeIf "gitdir:~/Documents/work/"] path = ~/Documents/work/.gitconfig
[user]
name = priyanshu10
email = priyanshuparate@gmail.com
[user]
name = Priyanshu1990
email = priyanshuparate10@gmail.com
[includeIf "gitdir:~/Documents/work/"]
path = ~/Documents/work/.gitconfig
[core]
autocrlf = input
step- 10
Add private keys to ssh in ssh folder using theo ssh-add command
cd ~/.ssh
//ssh-add home_account_key_private_key_file_name
add-ssh personel_id
//ssh-add work_account_key_private_key_file_name
add-shh work_id
Now you can work with different accounts. for more accounts, you need to follow the above steps which we did for the work account.
I hope you learn something new from the article
Thaks for reading
Happy Learning :) !!!!