Managing Multiple Git Authors by Directory

Managing Multiple Git Authors by Directory

Infrastructure, Ansible, AWS, Home Lab

If you contribute to both personal and work repositories, you’ve probably run into this:

You commit to a personal project — and realise your work email is all over it.

Or worse, you commit to a company repo using your personal identity.

Git only tracks one author configuration per system by default, but with a little setup, you can make Git automatically choose the right name and email based on the directory you’re working in.

Let’s walk through how to do it cleanly.


🧩 The Problem

By default, Git uses the global configuration you set up when you first installed it:

git config --global user.name "John Smith"
git config --global user.email "john@work.com"

That means every commit — no matter which project — will use that identity.

If you use Git for both work and personal projects, this gets messy fast.


🛠️ The Solution: Per-Directory Config

Git allows conditional includes, meaning you can load different configuration files depending on the path of your repository.

You can define separate config files for each identity and tell Git which one to use automatically based on directory.


⚙️ Step 1 – Set Your Global Config

Keep your global Git config simple — it acts as the default.

git config --global user.name "Personal Name"
git config --global user.email "personal@example.com"

Then open your global config file for editing:

nano ~/.gitconfig

You’ll see something like:

[user]
    name = Personal Name
    email = personal@example.com

⚙️ Step 2 – Add Conditional Includes

Now, let’s add rules so Git automatically uses your work identity in a specific folder, and your personal identity elsewhere.

Append this to your ~/.gitconfig:

[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work

[includeIf "gitdir:~/projects/personal/"]
    path = ~/.gitconfig-personal

Each includeIf rule checks the path of the repository.
If the repo lives inside ~/work/, Git will include your ~/.gitconfig-work settings.

💡 The path must end with a slash (/) to match everything inside that directory.

Step 3 – Create the Additional Config Files

Now create the two files:

~/.gitconfig-work

[user]
    name = Work Name
    email = work@example.com

~/.gitconfig-personal

[user]
    name = Personal Name
    email = personal@example.com

You can add as many of these as you need — for freelance clients, open-source accounts, or separate organisations.


🧪 Step 4 – Test It

Navigate into one of your repos and check which author Git is using:

cd ~/work/project1

git config user.name
-> Work Name

git config user.email
-> work@example.com

cd ~/projects/personal/myproject2

git config user.name
-> Personal Name

git config user.email
-> personal@example.com

You should see the correct identity based on the folder you’re in.


🧰 Bonus Tip – Verify Before You Commit

If you want a quick reminder before committing, you can add an alias to show the current identity:

git config --global alias.whoami '!git config user.name && git config user.email'

Now run:

cd ~/work/anotherproject

git whoami
Work Name

to double-check which author Git will use in the current repo.


🛠️ Fixing any previous commits

If you have accidentally committed with the wrong git author, you can always go back and alter the commit history, however note this can be a destructive process so use with caution! Where GIT_HASH is the commit just before the bad commit author you can substitute that and run the following:

git rebase -r GIT_HASH \
    --exec 'git commit --amend --no-edit --reset-author'

This will rebase ALL commits from that point resetting them against the currently configured author.

⚠️ Warning: This rewrites history — only do this on branches you control (not shared ones).


✅ Summary

With conditional includes, Git becomes smart enough to switch identities automatically based on directory.

Benefits:

  • No need to constantly reconfigure or remember which profile you’re using.
  • Prevents accidentally committing to personal projects with your work email.
  • Keeps your global configuration clean and simple.

It’s a small setup that saves a lot of cleanup later — especially if you juggle multiple Git identities daily.

In Part 2 we go into the next step of using separate SSH keys for multiple Git Accounts