1. 76

  2. 16

    Well now, I had to do a double take after blindly opening Lobsters and seeing my own blog post on the front page!

    Hopefully others can get some use out of this feature since I find it pretty nifty :)

    1. 3

      Is there any way to use environment variables in the condition? I keep my global git config in a git repo and I’d love to have a mechanism for conditionally including machine-specific overrides to some of the settings.

      1. 5

        It doesn’t look like Git’s conditional includes feature supports reading environment variables – it only supports these keywords:

        • gitdir
        • gitdir/i
        • onbranch

        However, the Environment section of the configuration docs lists some environment variables that you could set on specific machines to change Git’s configuration.

        Per-machine configuration using GIT_CONFIG_GLOBAL

        Of those environment variables, GIT_CONFIG_GLOBAL seems the easiest to use. You could use it by putting these files in your config repo:

        • shared_config
        • config_for_machine_1
        • config_for_machine_2

        Within each machine-specific config, use a regular (non-conditional) include to include shared_config:

        	path = shared_config
        ; Then write machine-specific configuration:
        	email = custom_email_for_this_machine@example.com

        Finally, on each of your machines, set the environment variable GIT_CONFIG_GLOBAL to that machine’s config file within your config repo.

        Setting a default config for other machines

        If you want some machines to just use shared_config without further configuration, name that file config instead and make sure your config repo is located at ~/.config/git/. On those machines, you don’t need to set GIT_CONFIG_GLOBAL. This will work because $XDG_CONFIG_HOME/git/config is one of Git’s default config paths.

        1. 1

          Hmm, not that I know of off the top of my head but I’ve never actually sat down and read the Git documentation so I’d be surprised. You could perhaps look into templating your gitconfig using something like chezmoi? There’s always nix which comes up too but that’s quite a bit overkill just for fiddling with some dotfiles of course.

          1. 1

            I can work around it now by generating the file locally, it’s just been a mild source of annoyance for me that I need external support for this.

        2. 1

          Ah, a note for anyone trying this. The original version was missing a trailing slash on the end of the includeIf directive. I’ve just pushed a fix for the typo but if you copied it earlier and were having trouble, just a heads up.

        3. 5

          I’ve become a big fan of direnv for this kind of stuff, being able to just write shel scripts that activate smoothly in all tools I use is a godsend

          1. 1

            use flakes is the chef’s kiss

          2. 4

            That’s definitely sounds better than using local per-repo config for the email. I have definitely leaked a personal address into a work repo a couple times due to forgetting to configure the repo with the work email.

            1. 6

              I once even forced myself to do so via global Git hook that checked whether I set the proper user info:

              if ! git config --get user.email >/dev/null
                  printf "No identity set, set one using 'git identity <name>'" \
                      | cowsay -f dragon-and-cow -W 60
                  echo ""
                  exit 1

              git-identity is script that allowed me to quickly set the identity for given repo.

              1. 2

                I’ve gone the opposite of just not setting a global username/email. I don’t have much to hide but it’s better to just try and keep stuff isolated for me