Skip to content

Commit 13a3c36

Browse files
committed
feat: add post about git conditional config
1 parent b68ca84 commit 13a3c36

File tree

1 file changed

+67
-0
lines changed
  • content/blog/git-conditional-config

1 file 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 config with and without home-manager"
3+
date = 2025-02-19
4+
[taxonomies]
5+
tags = ["til", "git", "nix"]
6+
+++
7+
8+
Today I learnt how to use git conditional config to use multiple identities easily.
9+
I use git for my private project and the projects I need to work at my day job.
10+
To keep these two projects apart, I have a `~/projects` folder in my home directory with the two subfolder `~/projects/work` and `~/projects/personal`.
11+
Now we can tell git to always use the one identity for all projects under `~/projects/personal` and another for `~/projects/work`.
12+
We just add two includeIfs in our git config. With includeIf we can specify other files to source if the condition is met. The prepended "gitdir:" just means every git repository under this directory. [^1]
13+
14+
```
15+
[includeIf "gitdir:~/projects/personal/"]
16+
path = "./personal"
17+
18+
[includeIf "gitdir:~/projects/work/"]
19+
path = "./work"
20+
```
21+
22+
In the personal config, we specify our personal identity
23+
24+
```
25+
[user]
26+
27+
name = "confusedalex"
28+
signingKey = "05AF71643F6E2ED3"
29+
```
30+
31+
and do the same for the work config.
32+
33+
## Integrating with home-manager
34+
35+
Because I use [home-manager](https://nix-community.github.io/home-manager/index.xhtml) I want to specify these conditionals with nix. We just need to add a list we attributes sets for every include.
36+
37+
> Everything under contents must follow the structure as in git-config.
38+
39+
```nix
40+
{
41+
programs.git = {
42+
enable = true;
43+
# ...
44+
includes = [
45+
{
46+
condition = "gitdir:~/projects/personal/";
47+
contents = {
48+
user = {
49+
name = "confusedalex";
50+
email = "[email protected]";
51+
signingKey = "05AF71643F6E2ED3";
52+
};
53+
gpg.format = "openpgp";
54+
};
55+
}
56+
{
57+
condition = "gitdir:~/projects/work/";
58+
contents = {
59+
#...
60+
};
61+
}
62+
];
63+
};
64+
}
65+
```
66+
67+
[^1]: [ https://git-scm.com/docs/git-config#\_includes ](https://git-scm.com/docs/git-config#_includes)

0 commit comments

Comments
 (0)