Skip to content

harshith-21/2GitAccounts1CLI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

2 Git Accounts, 1 CLI

So, if you're like me and use the same machine for both work and personal projects, you've probably run into the challenge of managing multiple GitHub accounts. Switching between two Git accounts - one for work and one for personal use - can be tricky, especially when you need to ensure the right credentials are used for each repository. In this guide, I'll walk you through how to set up your machine to easily switch between Git accounts without the hassle.

1. Generate RSA Key Pairs for Each Account

You might have a SSH key and added it to remote git account (GitHub, Bitbucket etc) already for work. If so, you can skip this step for work account and only do for personal.

You can create more, if you want to configure more accounts

ssh-keygen -t rsa -C "personal-email@example.com" -f ~/.ssh/id_rsa_personal

2. Add Public Keys to Your Remote Git

Go to your respective GitHub or Bitbucket account settings and add the corresponding SSH public key ~/.ssh/id_personal.pub in the account and so on if more.

3. Configure SSH for Each Account

Add these keys to your SSH configuration to tell Git which key to use for each account. Edit the ~/.ssh/config file:

Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa
    IdentitiesOnly yes
    AddKeysToAgent yes

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

4. Create Git Aliases for Easier Switching

Now, create Git aliases for each account. This way, you can easily switch between profiles with a single command. Add the following to your .zshrc / .bashrc based on your shell:

git_use_personal() {
  git config user.name "user name personal"
  git config user.email "personal@gmail.com"
  old_url=$(git remote get-url origin)
  clean_url=$(echo "$old_url" | sed -E 's/github\.com(-work|-personal)?/github.com/')
  new_url=$(echo "$clean_url" | sed 's/github.com/github.com-personal/')
  git remote set-url origin "$new_url"
  export GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_personal'
  echo "✅ Switched to PERSONAL Git profile"
}

git_use_work() {
  git config user.name "user name work"
  git config user.email "work@email.io"
  old_url=$(git remote get-url origin)
  clean_url=$(echo "$old_url" | sed -E 's/github\.com(-work|-personal)?/github.com/')
  new_url=$(echo "$clean_url" | sed 's/github.com/github.com-work/')
  git remote set-url origin "$new_url"
  export GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa'
  echo "✅ Switched to WORK Git profile"
}

Usage

Voilà! Now you can easily switch between accounts:

git_use_personal  # For your personal account
git_use_work      # For your work account

SSH Agent Commands for more help

To start SSH agent

eval $(ssh-agent -s)

To add a key

ssh-add ~/.ssh/id_rsa_personal

To authenticate

ssh -T git@github.com

To list keys

ssh-add -l

Happy coding with multiple Git accounts! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published