Use Multiple GitHub accounts on one Machine

Use Multiple GitHub accounts on one Machine

ยท

3 min read

This Blog will contain Three main tools:

  • Git Bash

  • VS code

  • Github

Create ssh Folder

Follow the below commands

cd ~ # To ensure you are on home
mkdir ~/.ssh # Create ssh folder

Change Directory to ssh by

cd ~/.ssh

Check if is there any ssh keys - if yes then it will be named as githubUserName.pub

Create ssh key

ssh-keygen -t rsa -C "example@gmail.com" -f "GitHubUserName"

You will be asked to input passphrase just like below:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Ignore & Press Enter ( leave it empty )

And then ssh key will be generated.

To check enter the below command line:

ls -al

๐Ÿ“Œ Add key to ssh agent

eval `ssh-agent -s` # output : Agent pid Number

Ensure to add username to ssh

ssh-add ~/.ssh/githubUserName

# Make sure to you are at `~/.ssh` path ( look into the below image )
code githubUserName.pub 
# Text editor will be opened and copy the entire thing to github.

To add follow the link : www.github.com/settings/keys

Give your own tittle and past the entire thing in the Key section.

SSH key successfully added to Github - make sure to follow below steps

Similary follow the above steps for your company account.

Go to bash - create files as mentioned below

#  ensure you are on root directory or you can go to root/home by < cd ~ >
touch .gitconfig .gitconfig.personal .gitconfig.company

#  ensure you are on ssh directory
touch config

Open config file -

code config

Text editor will be opened. And copy & paste below code in config file

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/GithubUsername
  AddKeysToAgent yes

Host github-company # or else keep your name as required < Host github-goggle >
  HostName github.com
  User git
  IdentityFile ~/.ssh/GithubUsername
  AddKeysToAgent yes

# save it

Go back to root/home directory by - cd ~ and copy & paste below code format & replace with your details.

.gitconfig

#inside ~/.gitconfig
[includeIf "gitdir:D:/Projects"] # include for all .git projects under Personal/ 
path = ~/.gitconfig.personal

[includeIf "gitdir:D:/company"] # include for all .git projects under Company/
path = ~/.gitconfig.company  

[core]
excludesfile = ~/.gitignore      # Ignore .gitconfig files valid everywhere

.gitconfig.personal

#inside ~/.gitconfig.personal
[user]
email = example@gmail.com
name = GithubUserName

[github]
user = "GithubUserName" #should be inisde double quote.

[core]
sshCommand = "ssh -i ~/.ssh/GithubUserName" #should be inisde double quote.

.gitconfig.company

#inside ~/.gitconfig.company
[user]
email = example@company.tech
name = GithubUserName

[github]
user = "GithubUserName" #should be inisde double quote.

[core]
sshCommand = "ssh -i ~/.ssh/GithubUserName" #should be inisde double quote.

Successfully Completed ๐Ÿฅณ


๐Ÿ“Œ Process to be done when you want to push your local repo to github correctly โœ…

  • Create your project

  • And come to the path on your gitbash

And follow below code so that your present project / local repo knows which github account should i make / from changes

git config user.name "githubUsername" # username must be in quotes
git config user.email "example@gmail.com" # username must be in quotes

On the first commit when connecting to remote repository. Make sure to run the below command

# correct โœ… 
git remote set-url origin git@github-company:githubUserName/repoName.git

# Default โŒ
git remote set-url origin git@github.com:githubUserName/repoName.git

Your are done โœจ๐ŸŽ†

Check wheather you are on connected to correct github account or not.

ssh -T git@github.userName
# in reponse you will get welcome message with userName. 
# That means your are connected to correct github account

ย