Set Git configuration conditionally
Written byPhuoc Nguyen
Category
Tip
Tags
Git
Created
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`
:.git/config
[user]
name=...
email=...
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.
~/.personal.gitconfig
[user]
name=...
email="personal email address"
~/.works.gitconfig
[user]
name=...
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`
.~/.gitconfig
[user]
name=...
email=...
[includeIf "gitdir:/personal-projects/"]
path=~/.personal.gitconfig
[includeIf "gitdir:/works-projects/"]
path=~/.works.gitconfig
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
Questions? 🙋
Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.
Recent posts ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
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