Forem

Bhupesh Varshney 👾
Bhupesh Varshney 👾

Posted on • Originally published at til.bhupesh.me

12 1

Using multiple git user configs with credentials store

So, you have decided to have 2 separate accounts for work and personal.
Here are steps to follow to use both git configurations simultaneously with the same .git-credentials.

Step 1: Separate Work/Personal directories

Create a work and a personsal directory in your Documents folder. Use these directories to separate your git directories.

  Documents
  ├── work
  │   ├── ...
  │   └── ...
  └── personal
      ├── ...
      └── ...
Enter fullscreen mode Exit fullscreen mode

Step 2: Separate git config for each account

  • Create .gitconfig-personal in the personal dir.
  [credential]
    helper = store
  [user]
    name = Personal_Username
    email = personal_email@domain.com
  [credential "https://github.com"]
    username = Personal_Username
    helper = store
Enter fullscreen mode Exit fullscreen mode
  • Create .gitconfig-work in the work dir.
  [credential]
    helper = store
  [user]
    name = Work_Username
    email = work_email@domain.com
  [credential "https://github.com"]
    username = Work_Username
    helper = store
Enter fullscreen mode Exit fullscreen mode

Step 3: Update global .gitconfig to switch the profile based on directory

  • Update your global config using the following command.
  git config --global --edit
Enter fullscreen mode Exit fullscreen mode
  • Add the following config.
  [includeIf "gitdir:~/Documents/work/"]
      path = ~/Documents/work/.gitconfig-work
  [includeIf "gitdir:~/Documents/personal/"]
      path = ~/Documents/personal/.gitconfig-personal
Enter fullscreen mode Exit fullscreen mode

Step 4: Register Access Tokens

  https://Personal_Username:PERSONAL_TOKEN@github.com
  https://Work_Username:WORK_TOKEN@github.com
Enter fullscreen mode Exit fullscreen mode

That's it, the next time you pull/push/clone a private repo, git will automatically choose the correct token for each config.

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay