274

I'd like to include a file in my .gitconfig that has my github settings - is this possible?

Can I do something like this:

[core]
    include = /path/to/file
David Reynolds
  • 3,141
  • 3
  • 18
  • 19
  • 3
    "I don't want to include the github details in it, hence why I would like to include them from an external file somehow": that is precisely what the global config file is for. Is there any reason to not use it in your case? – VonC Oct 13 '09 at 10:35
  • 14
    Yes, because I want to publish the .gitconfig in a git repository and I don't want someone to steal my github credentials. – David Reynolds Oct 13 '09 at 11:14
  • I do not follow you: your regular gitconfig file will be published to github, but *without* any github settings. Why? Because those would be in your *global* config file (`~/.gitconfig`), i.e. *not pushed* to your github repo. When you type '`git config`', what you see is the concatenation of the 3 config file (repo, global and system). Only the repo config file get pushed. The 2 other ones stay local. – VonC Oct 13 '09 at 11:23
  • Now if you still want to push those settings somehow, you could still store them only on your global config file (not pushed), while duplicated those settings in a regular text file which you could then encrypt, and then push to your github repo. – VonC Oct 13 '09 at 11:25
  • 100
    it looks like *everyone* missed the point of this question. David obviously wants to push up a repo of all his dot files (bashrc, gemrc, etc.) INCLUDING his .gitconfig so he can have all his settings on all his machines. A way to push parts of a .gitconfig file by including and ignoring private entries is what he (and I, for that matter) is after – Bo Jeanes Nov 25 '09 at 09:14
  • 6
    @bjeanes: precisely! I've still not found a way to do it though. – David Reynolds Nov 25 '09 at 20:15
  • @bjeanes: I just stumble upon that question again 5 months later, and completed my original answer with one possible way to publish dot files in a Git repo. – VonC Apr 23 '10 at 06:07

4 Answers4

352

Git (1.7.10+) now supports this syntax in .gitconfig:

[include]
    path = /path/to/file

See here for a detailed description of the git change and its edge cases.

By the way, a couple of subtleties worth pointing out:

  1. Environment-variable expansion, e.g. $HOME, is not supported. (Expansion of ~ appeared in Git 1.7.10.2.)

  2. If a relative path is specified, then it is relative to the .gitconfig file that has the [include] statement. This works correctly even across chained includes -- e.g. ~/.gitconfig can have:

    [include]
        path = subdir/gitconfig
    

    and subdir/gitconfig can have:

    [include]
        path = nested_subdir/gitconfig
    

    ... which will cause subdir/nested_subdir/gitconfig to be loaded.

  3. If git can't find the target file, it silently ignores the error. This appears to be by design.

Mike Morearty
  • 9,953
  • 5
  • 31
  • 35
  • 7
    actually you don't need `~`. this is because your .gitconfig-file still has to reside in `~/.gitconfig` a relative path in the config would imply `~`... – robustus Jul 18 '12 at 15:58
  • 3
    The `~` / `$HOME` expansion is actually as of `git describe --contains 4c0a89fc` -> `v1.7.10.2~12^2` (i.e. `v1.7.10.2` or later), notable as it seems Debian 7 and Ubuntu Quantal will release with `v1.7.10.4`. – FauxFaux Aug 22 '12 at 11:06
  • I've implemented this solution with my own dotfiles on Github so that I can provide per-project credentials to git, i.e. a different email address for at-work vs at-home projects. Super helpful, except that it's only available on git 1.7.10+ and Ubuntu 12.04 installs 1.7.9 via package. :[ – AL the X Apr 01 '13 at 15:35
  • It seems that tilde expansion does not work in 1.7.2 (Debian squeeze). – Alois Mahdal Apr 16 '13 at 15:47
  • 2
    Does this support globs? e.g. path = ~/gitconfig.d/* ? – Bret Dec 20 '15 at 20:56
  • 2
    @Bret no, this does not support globs. – Mike Morearty Dec 21 '15 at 03:13
  • 14
    Note that configurations included this way will not be shown when a specific file is given (that is, with `--global`, `--local` or `--file`), unless told explicitly with [`--includes`](https://git-scm.com/docs/git-config/2.13.0#git-config---no-includes) (like `git config --global --includes --list`). – Franklin Yu May 12 '17 at 03:18
  • e.g. sharing "common" aliases `[include] path = "~/.gitconfig.common"` – Schemiii Oct 15 '19 at 06:19
17

Update 2012:

See Mike Morearty's answer:

Includes

You can include one config file from another by setting the special include.path variable to the name of the file to be included.
The included file is expanded immediately, as if its contents had been found at the location of the include directive.
If the value of the include.path variable is a relative path, the path is considered to be relative to the configuration file in which the include directive was found.
The value of include.path is subject to tilde expansion: ~/ is expanded to the value of $HOME, and ~user/ to the specified user's home directory.


I do not think so.

I would rather put that setting in the ~/.gitconfig file

User-specific configuration file. Also called "global" configuration file.

That way, it completes the .gitconfig project-specific file, without being published when pushed to GitHub. See also this SO answer for more on the global config file.
Git has 3 config files.


bjeanes adds in the comments:

it looks like everyone missed the point of this question.
David obviously wants to push up a repo of all his dot files (bashrc, gemrc, etc.) INCLUDING his .gitconfig so he can have all his settings on all his machines.
A way to push parts of a .gitconfig file by including and ignoring private entries is what he (and I, for that matter) is after.

A possible way would be to use a smudge/clean filter driver to decrypt/encrypt one file with private sensitive informations (see this thread), in order to complete a local file like ~/.gitconfig with the decrypted parts that are relevant to that file.

That way you can have a Git repo with all your dot files, plus one file with encrypted information meant to be decrypted and added to said dot files.

alt text

In .gitattributes (or .git/info/a..) use:

myPrivateInfosFile filter=gpg diff=gpg

In your repo .config file:

[filter "gpg"]
smudge = gpg -d -q --batch --no-tty
clean = gpg -ea -q --batch --no-tty -r C920A124
[diff "gpg"]
textconv = decrypt

(a GPG-based solution means, off course, you have communicated your private/public keys by another mean onto the destination computer where you want to restore all your dot files by cloning this special repo)

Actually, in your case, the smudge script needs to be completed as it must, after decrypted that file, go on and add relevant parts to your global ~/.gitconfig file (unless you overwrite the global config file with another location) or other dot files for that matter.

https://kerneltrap.org/mailarchive/git/2008/3/13/1153274/thread (gpg inconveniences are discussed further in this thread) (this is different than having a full encrytped Git repo, as discussed here)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 5
    I want to publish my basic .gitconfig file in a git repository, but I don't want to include the github details in it, hence why I would like to include them from an external file somehow. – David Reynolds Oct 13 '09 at 08:17
13

You may load it from the command-line:

$ git config --local include.path "/path/to/.gitconfig"

Use "$PWD"/.gitconfig instead, if you want to load the file from the current directory.

After running above command, the following lines are added into your .git/config file:

[include]
        path = /path/to/.gitconfig
kenorb
  • 155,785
  • 88
  • 678
  • 743
2

I believe you can accomplish this using defunkt's hub tool. This is a wrapper for the git command which among other things, allows you to have GITHUB_USER and GITHUB_TOKEN environment variables. Which will override the settings in the local .gitconfig file.

Then to make it seamless the user you pointed to aliased alias git=hub in his ZSH config. You should be able to then source a local file where you set your environment variables and push your repository to the public world with all of your private information in tact.

**NOTE for homebrew users on OSX, you can install the tool via brew install hub.

Geoff Lanotte
  • 7,490
  • 1
  • 38
  • 50