Can gitconfig options be set conditionally?

Via Github I use the same set of “dot files” on several different computers and servers. On the Macs and Linux boxes under my direct control I have Sublime Text 2 installed and set up as my git merge and commit editor of choice. However, on remote (i.e., not under my direct control) servers I would select to use vim.

I would rather not create and maintain a second .gitconfig for those remote servers. Is there a way to do something like this:

  • How to handle nested git repositories?
  • How to push commits from changes made to a submodule into main git repo?
  • How do I edit after squashing while doing an interactive rebase?
  • Hudson + Poll SCM - build upstream projects first?
  • How to get icon overlays with git-extensions
  • password issue in cloning git repository from bitbucket
  • [core]
        if [[ $IS_REMOTE -eq 1 ]]; then
            editor = "vim"
            editor = "subl -n -w"

    where I’ve somehow set $IS_REMOTE based on the hostname?

  • Messing with GIT history destroyed recent commits…how to recover?
  • How to reduce git repo size on Bitbucket?
  • What's a good (free) visual merge tool for Git? (on windows)
  • How can I trigger garbage collection on a Git remote repository?
  • Using git with BitBucket
  • How to apply `git diff` patch without Git installed?
  • 5 Solutions collect form web for “Can gitconfig options be set conditionally?”

    No, Git config does not support checks or conditional statements. But your underlying shell probably does, so you can use something like:

        editor = "if [[ $IS_REMOTE -eq 1 ]]; then ED='vim'; else ED='subl -n -w'; fi; $ED"

    If you need to do something more complicated than that, you could just throw the shell code into a script, of course, like

        editor = ""

    with containing something like:

    if [[ $IS_REMOTE -eq 1 ]]; then
        ED="subl -n -w"
    $ED some argument or other

    Edit: The would have to be in the $PATH, of course 🙂

    The [include] section learned by git-config in v1.7.9 gets you most of the way there.

    While it doesn’t let you write runtime conditionals, it does give you a framework for refactoring your ~/.gitconfig into several parts: the shared section, and the env-specific sections. After that, you can symlink something like ~/.gitconfig.local to the relevant env-specific config file, and include ~/.gitconfig.local from ~/.gitconfig.

    The symlinking part can be scripted and done automatically as part of your dotfiles’ init script.

    From the command line, that include path can be added via:

    git config --global include.path '~/.gitconfig.local'

    I use the quotes above specifically to prevent the shell from expanding ~ to an absolute path.

    That adds the following section to your ~/.gitconfig:

        path = ~/.gitconfig.local

    Here’s a snippet from the git-scm book showing the general format:

        path = /path/to/ ; include by absolute path
        path = foo ; expand "foo" relative to the current file
        path = ~/foo ; expand "foo" in your $HOME directory

    I don’t think you can do this, but instead of maintaining your .gitconfig file, how about maintaining a script that generates your .gitconfig file? That way you can do whatever you want, based not only on variables but also on the output of commands and whatever…


    if [ "$#" -eq 0 ]
        case "$1" in
                echo "value $1 not supported" >&2
    # config for both remote and local
    git config --global color.ui true
    git config --global '!pwd -L'
    # config for remote
    if [ "$IS_REMOTE" ]
        git config --global core.editor vim
        git config --global core.editor 'subl -n -w'

    So, if you call the script without parameters, or with the ‘local’ parameter, it will generate some configuration to your .gitconfig file, while if you pass the ‘remote’ parameter to it, it will generate some others.

    not exactly an answer to your question but interesting for related usecases:
    since git 1.8.5 you are allowed to use urlmatch syntax

    for details

    config entries regarding remotes are the only one that can be defined conditionally like

    [http "https://localhost" ]
    sslVerify = false

    ^ will switch of ssl verification for localhost “remotes” only

    Since it isn’t possible to test an environment variable and programmatically alter the .gitconfig, and since scripting the creation of two .gitconfig files feels like more work than I really want to put forth, I’m just going to create two .gitconfig files. On those machines where I can setup Sublime Text 2 as my editor, and have control over the merge tool and diff tool, I’ll use the “primary” gitconfig as the target of my symbolic link. On those machines where I don’t have ST2 as an option, I’ll use the “secondary” gitconfig file.

    Git Baby is a git and github fan, let's start git clone.