Sitemap

Git Credentials — Using Different Credentials Conditionally

7 min readApr 16, 2026

--

Press enter or click to view image in full size
Photo by Yancy Min on Unsplash

Hello there, and welcome to my guide on how I configure Git so that my machine automatically uses either my work or my personal Git credentials based on the location from which I am pushing a repository. Let’s get started!

This guide uses C:\Users\<YourUsername>\personal\ as an example on Windows.
On macOS/Linux, use something like /Users/you/personal/ which in shorthand will be represented as ~/personal instead.

This guide focuses exclusively on SSH‑based authentication. HTTPS with PATs works differently and is not covered here.

The Problem ⚠️

As a developer, I wanted the ability NOT to have to switch Git credentials manually before I pushed any config up to a remote repo on GitHub. I do not want to accidentally push code up under the name/email of my workplace credentials when I intended to use personal credentials, and vice versa.

The Solution ✅

Fortunately, Git supports this fairly well using conditional configuration, no need for any under-the-hood magic solutions. It already exists! You can have a look through the Git config in their docs here 👉 Git — git-config Documentation.

Before we carry on, this guide is operating under the premise that as a user:

- You want your work Git credentials to be used everywhere by default (referred to as Globally)

- You want your personal GitHub account used only inside a directory of your choosing, such as C:\Users\<YourUsername>\personal on windows or ~/personal on macOS/Linux, Great!

How does Git choose the credentials to use?

Git will read the settings it needs from a global config file, usually called .gitconfig and is usually located at:

# Windows
C:\Users\<YourUsername>\.gitconfig

# macOS/Linux
~/.gitconfig

You can have a look yourself if you open your terminal and perform the following:

ℹ️ These commands do the same thing; the syntax differs slightly between shells.

# Windows (PowerShell)
$ cd $HOME
$ ls -Force

# macOS/Linux terminal (Terminal, zsh/bash)
$ cd ~/
$ ls -a

You will see a screen that looks similar to this:

Press enter or click to view image in full size
Git bash terminal showing all files in my home directory

Inside .gitconfig, which you can open by using the command nano .gitconfig Or open it with your preferred text editor, such as Code, etc. You will see the content looking like this:

Press enter or click to view image in full size
.gitconfig contents

These are my settings that Git looks at every time I push to a GitHub repository. This is my global config. What we now need to do is tell Git there are other settings to use whenever I am inside a particular Directory on my machine.️

Conditonal Git Configurations 🛠️

Git has a wonderful feature called IncludeIf which will allow it to load extra configuration files based on the .git directory you’re working in.

We are going to use it to tell Git "If I am in a repo anywhere under C:\Users\<YourUsername>\personal\, use my personal credentials instead"

⚠️ Note: includeIf rules are only evaluated when you are inside a Git repository. Running git config user.email outside a repo will always show the global configuration.

Step 1: Confirm Global (work) identity 🔑

Let’s just make sure that the work identity is the current global

$ git config --global user.email
# This should output your work email, and running this from any directory
# should return your work email

# If it is not set, then you can set it by doing the following:

$ git config --global user.name "<YOUR_WORK_NAME>"
$ git config --global user.email "<YOUR_WORK_EMAIL>"

# Running the top command again should output your work email

Step 2: Creating a personal Git config file 📄

Great! Now we are going to create a personal Git config file. Assuming you are still currently in the home directory of your machine:

# Windows (PowerShell)
$ New-Item .gitconfig-personal -ItemType File

# macOS/Linux
$ touch .gitconfig-personal

Now add the following contents:

[user]
name = YOUR_PERSONAL_NAME>
email = <YOUR_PERSONAL_EMAIL>

This file is only going to be used when Git is told to load it.

Step 3: Tell Git when to load your personal credentials 🗝️

Before adding the includeIf rule, it’s important to understand how the two config files work together:

  • .gitconfig remains your global (work) configuration
  • .gitconfig-personal is only loaded conditionally when the repository lives under your chosen directory

Open your main Git config file:

# Windows
C:\Users\<YourUsername>\.gitconfig

# macOS/Linux
~/.gitconfig

and add the following at the bottom:


# Windows
[includeIf "gitdir/i:C:/Users/<YourUsername>/personal/**"]
path = ~/.gitconfig-personal
# macOS/Linux
[includeIf "gitdir:/Users/you/personal/**"]
path = ~/.gitconfig-personal

What this is doing:

  • gitdir ➡️checks the current repository's location
  • (Windows only) i ➡️makes the match case-insensitive (important on Windows)
  • C:/Users/<YourUsername>/personal/** on windows or ~/personal on macOS ➡️ matches this folder and all subfolders

This means that any repository found inside of:

# Windows
C:\Users\<YourUsername>\personal

# macOS/Linux
~/personal

will use your personal Git config automatically!

Step 4: Verify it works 😎

I want to navigate in my terminal to a work repository, and run:

$ git config user.email

And I should see my work email address scott.maitland@myfakecompany.com

If I navigate to a personal repository:

# Windows
C:\Users\<YourUsername>\personal\

# macOS/Linux
~/personal

and run the same command, then I should see my personal email address.

Get Scott Maitland’s stories in your inbox

Join Medium for free to get updates from this writer.

So, this is great for telling Git what user credentials to use, but what happens when you want to also tell it to use the correct SSH key 👀 let’s find out together!

Incorporating SSH keys to ensure I use the correct SSH key 🔐

At this stage, you will most likely already have set up to use SSH keys with your work GitHub account. If not, this process will be the same, but with your work account.

Step 1: Create a personal key

Let’s create a new key using our terminal

$ ssh-keygen -t ed25519 -C "<YOUR_PERSONAL_EMAIL>" -f ~/.ssh/id_ed25519_personal

this creates:

  • id_ed25519_personal
  • id_ed25519_personal.pub

You can go ahead and add the .pub to your personal GitHub account SSH key section in settings.

Step 2: Configure SSH to know about both keys

We will want to edit the file ~/.ssh/config. If it does not already exist, then go ahead and create it.

We will want to add the following:

Host github.com
User git

I am purposefully not adding any Identity information here because I will let Git dictate which SSH to load in my .gitconfig.

Step 3: Matching the SSH Identity to the right directory

Now we need to link everything together using conditional Git config again, just like we have already done before with our git user credentials.

In our main ~/.gitconfig We will want to tell it which SSH key to use:

[user]
name = YOUR_WORK_NAME>
email = <YOUR_WORK_EMAIL>

[core]
sshCommand = ssh -i ~/.ssh/id_ed25519_work
  • The work SSH key will be used
  • The work Identity will be used
  • This will work with clipboard URLs that GitHub.com gives when cloning the repo using SSH, such as git clone git@github.com:work-org/repo.git

✅ This makes your work SSH the default everywhere (Global)

Personal Git Config (Your Directory-based override)

Inside our personal git config at:

# Windows
C:\Users\<YourUsername>\.gitconfig

# macOS/Linux
~/.gitconfig-personal

~/.gitconfig-personal We will also update it to use the right SSH key:

[user]
name = YOUR_PERSONAL_NAME>
email = <YOUR_PERSONAL_EMAIL>

[core]
sshCommand = ssh -i ~/.ssh/id_ed25519_personal

⚠️ It is important to note that if your workplace is using GitHub Enterprise, then you will need to set up a separate Host for it inside your ssh config file

# Windows
C:\Users\.ssh\config

# macOS/Linux
~/.ssh/config

as the URL is different, like this:

Host <COMPANY_NAME>.ghe.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes

That way, whenever you clone from GitHub Enterprise using the URL it provides, such as <COMPANY_NAME>@<COMPANY_NAME.ghe.com:Org/repo.git> It will use the correct key also.

You will also need to configure SSO after adding the SSH key, to authorize the SSH key for an organization otherwise even though the SSH key is valid, the company policy will block being able to use it.

Outcome 🎉

Now that we have set this all up, when we run:

$ git clone git@github.com:your-username/personal-repo.git

Git directories inside of:

# Windows
C:\Users\<YourUsername>\personal\

# macOS/Linux
~/personal/
  • Git loads .gitconfig-personal
  • Git tells SSH to use ssh -i id_ed25519_personal
  • ✅Personal key is used to clone the repo

When you run the same command elsewhere for a work GitHub URL:

$ git clone git@github.com:your-username/work-repo.git

Git directories outside of:

# Windows
C:\Users\<YourUsername>\personal\

# macOS/Linux
~/personal
  • Git loads .gitconfig
  • Git tells SSH to use ssh -i id_ed25519_work
  • ✅Work key is used to clone the repo

When cloning using a GitHub Enterprise URL

$ git clone <COMPANY_NAME>@<COMPANY_NAME>.ghe.com:Org/repo.git
  • SSH matches <YOUR_COMPANY_NAME>.ghe.com
  • Uses work key
  • Git identity = work
  • ✅Always correct

Thank you for taking the time to read this article. If there is any feedback or comments, please feel free to reach out, or if you have found a more efficient way to approach this same problem, then I would love to hear from you. 🍵 👋

--

--

Scott Maitland
Scott Maitland

I am a Glasgow-based Software Engineer. I love all things tech, and I am a big gaming geek.