DEV Community

Cover image for Setting up project-level gitconfig
Nikkhiel Seath
Nikkhiel Seath

Posted on

3 2

Setting up project-level gitconfig

Last week, I completely shifted my system to Arch Linux and during this process, I lost some config(s), settings, etc.

git config was one of them. I used to have a separate config like username, email, ssh key to use based on which project I was working on.
For example, my work projects require a different set of credentials (username, email, ssh-key) than the credentials I use on my personal projects.

I have done this before too but, after a while, I just tend to forget how to do it. This time, I am writing it in a post so that I can refer to it when required.

Steps

  1. Go to the directory where you store the projects and create a .project.gitconfig with the desired credentials:
[user]
    name = work-username
    email = username@work-domain.com
Enter fullscreen mode Exit fullscreen mode
  1. Edit the user level .gitconfig present under $HOME/.gitconfig to include the following condition:
[includeIf "gitdir:ABSOLUTE_PROJECT_DIRECTORY_PATH"]
    path = "ABSOLUTE_PROJECT_DIRECTORY_PATH/.project.gitconfig"
Enter fullscreen mode Exit fullscreen mode

Basically, we are telling git:

"If you find a .git directory nested under this path then, use these set of credentials."

Verify

Now, go to a project inside the work/project-root directory and verify that the correct credentials is being used:

git config --list | grep -P "user|email"
Enter fullscreen mode Exit fullscreen mode

Note (or Gotcha)

Keep in mind that this config will only be used by git if it finds .git directory at that path or a parent .git directory.
For example:

work/
|- .work.gitconfig
|- project-a/
   |- .git/
|- project-b/
   |- .git/
Enter fullscreen mode Exit fullscreen mode

Now, my .work.gitconfig will be used when I am inside project-a or project-b but, not inside work.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay