← Back toFront-end tips

Set Git configuration conditionally

Written byPhuoc Nguyen
30 Aug, 2023
When working on different environments, like company projects and personal projects, we usually use different Git accounts. To keep things organized, we clone all the repositories of each account to a dedicated folder. For example, we can clone all the personal project repositories in the `personal-projects` folder, and the company projects in the `work-projects` folder.
Once we clone a Git repository to one of those folders, the first thing we should do is configure the name and email address associated with the cloned repository. This ensures that our commits and contributions are properly identified.
$ git config user.name "..."
$ git config user.email "..."
Behind the scenes, running these commands will add a section called `[user]` to the Git configuration file of the current folder, which is located at `.git/config`:
If you don't set up your Git configuration properly, you might accidentally push commits with the wrong information attached. It's not hard to run two simple commands to fix this, but it's inconvenient to have to do it for every repository. Plus, it's easy to forget.
Thankfully, Git has a solution: you can set up your configuration differently for each folder or repository you work on. The first step is to create a dedicated Git configuration for each folder.
email="personal email address"
email="company email address"
Now, let's talk about using Git configurations based on the repository path.
To set conditional configurations, we'll be using the `includeIf` directive. This directive allows you to include a configuration file based on a condition. In our case, we want to include a configuration file based on the repository path.
We'll be editing the global Git configuration file, which is typically stored in `~/.gitconfig`.
[includeIf "gitdir:/personal-projects/"]
[includeIf "gitdir:/works-projects/"]
We use two instances of the `includeIf` directive to tell Git which configuration to use based on the project path.
For example, if you clone a project into the `personal-projects` folder, Git will use the configuration defined in `~/.personal.gitconfig` instead of the global Git configuration.

See also

If you found this post helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

Questions? 🙋

Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Ask me questions

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn