Skip to content

Commit 616b69b

Browse files
committed
Add TIL: git conditional configs.
1 parent 3720537 commit 616b69b

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
+++
2+
title = "TIL: Git Conditional Configs"
3+
date = 2024-04-07T12:38:00-07:00
4+
lastmod = 2024-04-07T14:23:13-07:00
5+
tags = ["til", "git"]
6+
categories = ["til", "git"]
7+
draft = false
8+
toc = true
9+
+++
10+
11+
Every Git user will have probably been asked to set up their Git at the first time: <br/>
12+
13+
```sh
14+
git config --global user.name "Ramsay Leung"
15+
git config --global user.email [email protected]
16+
```
17+
18+
The above command will simply add the `user.name` and `user.email` value into your `~/.gitconfig` file <br/>
19+
20+
```sh
21+
> cat ~/.gitconfig
22+
[user]
23+
name = Ramsay Leung
24+
25+
[core]
26+
quotepath = false
27+
[init]
28+
defaultBranch = master
29+
```
30+
31+
You could also specify `--local` argument to writes the config values to `.git/config` in whatever project you're currently in. <br/>
32+
33+
If you need to simultaneously contribute to your work and open source project on the same laptop, with different Git config values, e.g.(company email address for work-specific projects, personal email address for open source project), what should you do? <br/>
34+
35+
You could definitely set up work-specific config as global config, then set up personal config with `--local` for every personal project separately. It works, but tedious and easy to mess-up. <br/>
36+
37+
Fortunately, starting from Git version 2.13, Git supports conditional configuration includes, you are capable of setting up different configs for different repositories. <br/>
38+
39+
If you add the following config to your global config file: <br/>
40+
41+
```toml
42+
[includeIf "gitdir:~/projects/oss/"]
43+
path = ~/.gitconfig-oss
44+
45+
[includeIf "gitdir:~/projects/work/"]
46+
path = ~/.gitconfig-work
47+
```
48+
49+
Then Git will look in the `~/.gitconfig-oss` files for values only if the project you are currently working on matches `~/projects/oss/`. <br/>
50+
51+
****Caution****: If you forget to specify the "/" at the end of the git dir, e.g. "~/projects/oss", Conditional Config won't work! <br/>
52+
53+
Therefore, you could have a "work" directory and work-specific config here and an "oss" directory with values for your open source projects, etc. <br/>
54+
55+
{{< figure src="/ox-hugo/conditional_config.png" >}} <br/>
56+
57+
Git also supports other filters more than `gitdir`, you could specify a branch name as an include filter with `onbranch` <br/>
58+
59+
```toml
60+
; include only if we are in a worktree where foo-branch is
61+
; currently checked out
62+
[includeIf "onbranch:foo-branch"]
63+
path = foo.inc
64+
```
65+
66+
Check out [the Git docs](https://git-scm.com/docs/git-config?ref=blog.gitbutler.com#_includes) for more details <br/>
67+
96.9 KB
Loading

0 commit comments

Comments
 (0)