Skip to content

Commit

Permalink
Add TIL: git conditional configs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsayleung committed Apr 7, 2024
1 parent 3720537 commit 616b69b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions content/en/post/2024/git_conditional_configs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
+++
title = "TIL: Git Conditional Configs"
date = 2024-04-07T12:38:00-07:00
lastmod = 2024-04-07T14:23:13-07:00
tags = ["til", "git"]
categories = ["til", "git"]
draft = false
toc = true
+++

Every Git user will have probably been asked to set up their Git at the first time: <br/>

```sh
git config --global user.name "Ramsay Leung"
git config --global user.email [email protected]
```

The above command will simply add the `user.name` and `user.email` value into your `~/.gitconfig` file <br/>

```sh
> cat ~/.gitconfig
[user]
name = Ramsay Leung
email = [email protected]
[core]
quotepath = false
[init]
defaultBranch = master
```

You could also specify `--local` argument to writes the config values to `.git/config` in whatever project you're currently in. <br/>

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/>

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/>

Fortunately, starting from Git version 2.13, Git supports conditional configuration includes, you are capable of setting up different configs for different repositories. <br/>

If you add the following config to your global config file: <br/>

```toml
[includeIf "gitdir:~/projects/oss/"]
path = ~/.gitconfig-oss

[includeIf "gitdir:~/projects/work/"]
path = ~/.gitconfig-work
```

Then Git will look in the `~/.gitconfig-oss` files for values only if the project you are currently working on matches `~/projects/oss/`. <br/>

****Caution****: If you forget to specify the "/" at the end of the git dir, e.g. "~/projects/oss", Conditional Config won't work! <br/>

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/>

{{< figure src="/ox-hugo/conditional_config.png" >}} <br/>

Git also supports other filters more than `gitdir`, you could specify a branch name as an include filter with `onbranch` <br/>

```toml
; include only if we are in a worktree where foo-branch is
; currently checked out
[includeIf "onbranch:foo-branch"]
path = foo.inc
```

Check out [the Git docs](https://git-scm.com/docs/git-config?ref=blog.gitbutler.com#_includes) for more details <br/>

Binary file added static/ox-hugo/conditional_config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 616b69b

Please sign in to comment.