feat: conditional inclusions

This commit is contained in:
Michele Cereda
2024-02-04 16:36:25 +01:00
parent 378879e688
commit 3e45cb7d76
2 changed files with 48 additions and 5 deletions

View File

@@ -124,3 +124,11 @@
gpgSign = true gpgSign = true
[user] [user]
signingKey = 89C324BA signingKey = 89C324BA
# Conditional inclusions.
[includeIf "hasconfig:remote.*.url:*github.com*/**"]
path = relative.gitconfig.github
[includeIf "hasconfig:remote.*.url:git@gitlab.com:*/**"]
path = ~/.gitconfig.gitlab.ssh
[includeIf "gitdir/i:work/"]
path = /absolute/gitconfig.work

View File

@@ -5,6 +5,7 @@
1. [TL;DR](#tldr) 1. [TL;DR](#tldr)
1. [Authentication](#authentication) 1. [Authentication](#authentication)
1. [Configuration](#configuration) 1. [Configuration](#configuration)
1. [Conditional inclusions](#conditional-inclusions)
1. [Remotes](#remotes) 1. [Remotes](#remotes)
1. [Push to multiple git remotes with the one command](#push-to-multiple-git-remotes-with-the-one-command) 1. [Push to multiple git remotes with the one command](#push-to-multiple-git-remotes-with-the-one-command)
1. [Aliases](#aliases) 1. [Aliases](#aliases)
@@ -34,6 +35,8 @@
## TL;DR ## TL;DR
[Gitconfig example]
```sh ```sh
# Set your identity. # Set your identity.
git config 'user.name' 'User Name' git config 'user.name' 'User Name'
@@ -365,6 +368,15 @@ git \
## Configuration ## Configuration
Config files:
| Path | Scope | Description |
| -------------------------- | ------ | ------------------------------------ |
| `/etc/gitconfig` | system | System-wide default configuration |
| `$HOME/.gitconfig` | global | Per-user configuration |
| `$HOME/.config/git/config` | global | Per-user configuration (alternative) |
| `.git/config` | local | Repository configuration |
```sh ```sh
# Required to be able to commit changes. # Required to be able to commit changes.
git config --local 'user.email' 'me@me.info' git config --local 'user.email' 'me@me.info'
@@ -408,6 +420,24 @@ git config --list \
| xargs -I {} sh -c 'printf "{}=" && git config --get {}' | xargs -I {} sh -c 'printf "{}=" && git config --get {}'
``` ```
### Conditional inclusions
Paths can be relative or absolute, and one can use `~` as shortcut for the user's `$HOME` directory.
```ini
# All repositories in the given directory.
# '/i' makes the match insensitive.
[includeIf "gitdir/i:work/"]
path = /path/to/gitconfig.work
# Only if any remote's URL matches the format.
# Remotes need to be specified *after* this (i.e. in a local scope).
[includeIf "hasconfig:remote.*.url:*github.com*/**"]
path = gitconfig.github
[includeIf "hasconfig:remote.*.url:git@gitlab.com:*/**"]
path = ~/.gitconfig.gitlab.ssh
```
### Remotes ### Remotes
```sh ```sh
@@ -432,7 +462,7 @@ git remote set-url --push --add origin https://exampleuser@example.com/path/to/r
git remote set-url --push --add origin https://exampleuser@example.com/path/to/repo3 git remote set-url --push --add origin https://exampleuser@example.com/path/to/repo3
``` ```
```txt ```ini
[remote "origin"] [remote "origin"]
url = https://exampleuser@example.com/path/to/repo1 url = https://exampleuser@example.com/path/to/repo1
pushUrl = https://exampleuser@example.com/path/to/repo1 pushUrl = https://exampleuser@example.com/path/to/repo1
@@ -443,7 +473,7 @@ git remote set-url --push --add origin https://exampleuser@example.com/path/to/r
To only pull from `repo1` but push to `repo1` and `repo2` for a specific branch `specialBranch`: To only pull from `repo1` but push to `repo1` and `repo2` for a specific branch `specialBranch`:
```txt ```ini
[remote "origin"] [remote "origin"]
url = ssh://git@aaa.xxx.com:7999/yyy/repo1.git url = ssh://git@aaa.xxx.com:7999/yyy/repo1.git
fetch = +refs/heads/*:refs/remotes/origin/* fetch = +refs/heads/*:refs/remotes/origin/*
@@ -968,11 +998,18 @@ All the references in the [further readings] section, plus the following:
- [Git Config | Setup Git Environment] - [Git Config | Setup Git Environment]
- [1 minute coding tip: git diff-words to see diffs on a per-word basis instead of per line] - [1 minute coding tip: git diff-words to see diffs on a per-word basis instead of per line]
- [Dress up your git diffs with word-level highlights] - [Dress up your git diffs with word-level highlights]
- [Git global config for specific repositories?]
<!-- <!--
References References
--> -->
<!-- In-article sections -->
[further readings]: #further-readings
<!-- Files -->
[gitconfig example]: ../examples/dotfiles/.config/git/config
<!-- Upstream --> <!-- Upstream -->
[docs]: https://git-scm.com/docs/git [docs]: https://git-scm.com/docs/git
[getting git on a server]: https://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server [getting git on a server]: https://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server
@@ -982,9 +1019,6 @@ All the references in the [further readings] section, plus the following:
[setting up the server]: https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server [setting up the server]: https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server
[tagging]: https://git-scm.com/book/en/v2/Git-Basics-Tagging [tagging]: https://git-scm.com/book/en/v2/Git-Basics-Tagging
<!-- In-article sections -->
[further readings]: #further-readings
<!-- Others --> <!-- Others -->
[1 minute coding tip: git diff-words to see diffs on a per-word basis instead of per line]: https://www.youtube.com/watch?v=gDkvLxbA5ZE [1 minute coding tip: git diff-words to see diffs on a per-word basis instead of per line]: https://www.youtube.com/watch?v=gDkvLxbA5ZE
[10 git tips we can't live without]: https://opensource.com/article/22/4/git-tips [10 git tips we can't live without]: https://opensource.com/article/22/4/git-tips
@@ -996,6 +1030,7 @@ All the references in the [further readings] section, plus the following:
[dress up your git diffs with word-level highlights]: https://www.viget.com/articles/dress-up-your-git-diffs-with-word-level-highlights/ [dress up your git diffs with word-level highlights]: https://www.viget.com/articles/dress-up-your-git-diffs-with-word-level-highlights/
[get the repository's root directory]: https://stackoverflow.com/questions/957928/is-there-a-way-to-get-the-git-root-directory-in-one-command/#957978 [get the repository's root directory]: https://stackoverflow.com/questions/957928/is-there-a-way-to-get-the-git-root-directory-in-one-command/#957978
[git config | setup git environment]: https://initialcommit.com/blog/git-config [git config | setup git environment]: https://initialcommit.com/blog/git-config
[git global config for specific repositories?]: https://stackoverflow.com/questions/61983894/git-global-config-for-specific-repositories#71096731
[git submodules: adding, using, removing, updating]: https://chrisjean.com/git-submodules-adding-using-removing-and-updating/ [git submodules: adding, using, removing, updating]: https://chrisjean.com/git-submodules-adding-using-removing-and-updating/
[gpg failed to sign the data fatal: failed to write commit object]: https://stackoverflow.com/questions/39494631/gpg-failed-to-sign-the-data-fatal-failed-to-write-commit-object-git-2-10-0 [gpg failed to sign the data fatal: failed to write commit object]: https://stackoverflow.com/questions/39494631/gpg-failed-to-sign-the-data-fatal-failed-to-write-commit-object-git-2-10-0
[how do i check out a remote git branch]: https://stackoverflow.com/questions/1783405/how-do-i-check-out-a-remote-git-branch/#1787014 [how do i check out a remote git branch]: https://stackoverflow.com/questions/1783405/how-do-i-check-out-a-remote-git-branch/#1787014