Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mfaani/b554dba69428ddda4ce9bfa747e4e238 to your computer and use it in GitHub Desktop.
Save mfaani/b554dba69428ddda4ce9bfa747e4e238 to your computer and use it in GitHub Desktop.
How to modify your git config based on directory - gist

There are some nuances. Took me a bit to piece it all together.

  1. Just add an if condition at the end of your existing global .gitconfig at your root directory.
[includeIf "gitdir:~/something/personal/"]
  path = .gitconfig-personal
[includeIf "gitdir:~/elsewhere/work/"]
  path = .gitconfig-work

The reason for adding at the end of your config, is because you want to inherit everything that's in your existing config, but then just override with some other directory based configurations.

ATTENTION: You must also add the / at the end of your directories. Otherwise things won't work.

  1. Create and add whatever overrides you want into .gitconfig-personal, .gitconfig-work. For example mine is just:
# .gitconfig-personal
[user]
	name = mfaani
	email = personalEmail@gmail.com
# .gitconfig-work
[user]
	name = Mohammad Faani
	email = workEmail@company.com

How can I validate my settings?

Either:

  • Inspect existing repos: git config --list | grep user.name in different directories and inspect if things are different.
  • Inspect new repos:
    • cd personal
    • git init Foo
    • cd Foo
    • git config user.name
    • compare its value if you did the same steps but from within /work directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment