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:
[core]
if [[ $IS_REMOTE -eq 1 ]]; then
editor = "vim"
else
editor = "subl -n -w"
fi
where I've somehow set $IS_REMOTE based on the hostname?
The global git config is simply a text file, so it can be edited with whatever text editor you choose. Open, edit global git config, save and close, and the changes will take effect the next time you issue a git command. It's that easy.
gitconfig is used to store a per-user configuration as fallback values for the . git/config file. The file /etc/gitconfig can be used to store a system-wide default configuration. The configuration variables are used by both the Git plumbing and the porcelains.
# There are 3 levels of git config; project, global and system.
No, Git config does not support checks or conditional statements. But your underlying shell probably does, so you can use something like:
[core]
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
[core]
editor = "my_edi_script.sh"
with my_edit_script.sh
containing something like:
#!/bin/bash
if [[ $IS_REMOTE -eq 1 ]]; then
ED="vim"
else
ED="subl -n -w"
fi
$ED some argument or other
Edit: The my_edit_script.sh
would have to be in the $PATH, of course :)
You can conditionally include another Git config file based on your Git directory or branch in Git 2.13 and later.
Put your default configuration in file ~/.gitconfig
as usual. At the end, conditionally include another configuration file:
[user]
email = [email protected]
name = John McGehee
# All work Git repositories are in a subdirectory of ~/work.
# All other Git repositories are outside ~/work.
[includeIf "gitdir:~/work/"]
path = .gitconfig.work
Then, in ~/.gitconfig.work
add or override configuration values you want when using a repository located in ~/work
or any subdirectory thereof:
[user]
email = [email protected]
You can observe the difference by changing to a Git directory under ~/work
, and running:
git config user.email
Try the same command in a Git directory that is not under ~/work
.
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
:
[include]
path = ~/.gitconfig.local
Here's a snippet from the git-scm book showing the general format:
[include]
path = /path/to/foo.inc ; include by absolute path
path = foo ; expand "foo" relative to the current file
path = ~/foo ; expand "foo" in your $HOME directory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With