230

I'm using filters to mangle files during checkout like described here. Now the problem is that filter definition is only stored in my local configuration file:

$ cat .git/config
....
[filter "dater"]
        smudge = /home/.../expand_date
        clean = perl -pe \"s/\\\\\\$Date[^\\\\\\$]*\\\\\\$/\\\\\\$Date\\\\\\$/\"

If my coworkers want to benefit from this Date expansion, they need to copy my filter definition. And if I change it, I need to notify them, etc..

So can I store this filter definition part of .git/config in repository and make git use it?

Flip
  • 6,233
  • 7
  • 46
  • 75
Zaar Hai
  • 9,152
  • 8
  • 37
  • 45
  • 3
    You may find [this link](https://github.com/stefanhoelzl/share-git-hooks-and-config) useful to automatically share a team-wide configuration. – mljrg Jul 10 '20 at 11:23

2 Answers2

254

There are 3 supported scopes of .gitconfig file: --system, --global, --local. You can also create a custom configuration file, and include it in one of the supported files.

For your needs custom - is the right choice. Instead of writing your filter in .git/config you should save it in .gitconfig file in your repository root:

your-repo/
│
├── .git/
│   ├── config
│
├── .gitconfig
│

Create the .gitconfig with your filter and commit the changes. Then your colleagues will always keep it updated -- but they will have to include it manually. It is not possible to automatically include your custom configuration file through git alone, because it creates a security vulnerability.

To apply this configuration for a single repository, each user will need to run the following command in your-repo/:

git config --local include.path ../.gitconfig

Reference: https://git-scm.com/docs/git-config#_includes

Be careful not to store personal data in the custom .gitconfig, like user.*, keep those in your global .gitconfig.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
  • 69
    I want to avoid notifying each possible user that it needs to include repo's `.gitconfig` in his `~/.gitconfig`. No one will remember it. Is there a way to make git to ALWAYS read the repo's `.gitconfig`. – Zaar Hai Aug 20 '13 at 09:23
  • 16
    @ZaarHai: I'm guessing no, because that would be dangerous. (Imagine cloning a repo that aliases commands with something "interesting", eg. exporting the contents of your home dir). If you've got something like a `configure` script or similar that users must run, you could have that set the include (or prompt the user to). – Hasturkun Aug 20 '13 at 10:01
  • 2
    How do you tell git to use a repo's own .gitconfig file? I have created one but it's not being parsed. – Henry Blyth Feb 09 '15 at 13:52
  • 5
    @HenryBlyth there seems to be a `--file` option on `git config`, you can then set it to point on your repo's `.gitconfig`. I haven't tested it yet, but it seems to answer your question. You can read it on the docs. http://git-scm.com/docs/git-config#FILES – theUnknown777 Apr 16 '15 at 07:04
  • 1
    the solution works, but it doesn't work after _git pull_ by another team member – rok Dec 25 '17 at 23:44
  • 37
    Be great if Git didn't assume you're writing code distributed outside your org where you have to care about security threats. Sometimes system usability and automation has a higher priority than security. – Geordie Feb 17 '18 at 01:28
  • 2
    You can sneakily add the command to a task (assuming you're using a task runner) routinely used by your team. – Mieszko Aug 15 '18 at 08:02
  • 2
    @Mieszko Do you not find slipping your coworkers unexpected code to trick them into executing it ethically questionable? – Synoli Sep 06 '18 at 16:51
  • 2
    @Synoli I was being ironic, as It's a bad idea from the separation of concerns standpoint. Nevertheless, it would be ethical if not held secret and enforcing a legitimate rule. – Mieszko Sep 06 '18 at 20:00
  • 4
    @Mieszko and @Synoli, I often add a Makefile to my projects and when needed I insert a `make setup` command that is needed for the project to run correctly. It is explicit so no ethical concerns and semantic so no problem with separation of concerns. But it has the drawback of having everyone remembering to run it when they clone a project, but if you do this for all your projects that might need it becomes easy to remember. – VinGarcia Aug 05 '20 at 14:26
  • It looks like you can edit the pre-commit file under .git/hooks to include the command to include the local .gitconfig file `git config --local include.path ../.gitconfig` – gbro3n Oct 24 '20 at 12:58
  • 1
    @gb2d, .git/hooks is also local, not versioned, so that just adds a level of indirection to the same problem. – Anthony Hall Apr 12 '22 at 19:46
  • You could start an `.envrc` file containing `git config --local include.path .gitconfig` and if the user has direnv installed they would allow the reading of the file and it would be automated. – svandragt Nov 06 '22 at 10:45
31

You can not use .gitconfig file in a git repository by default, but you can link to it so the git config will be versioned.

You can link to it like that:

[include]
  path = ../.gitconfig

I have created a simple script gitconfig.sh which do it for you (much faster than copy) + simple .gitconfig file so if you want, take a look to this repo https://github.com/HoBi/dotfiles.


EDIT: I have deleted the file, but you can find it here https://github.com/tenhobi/dotfiles/blob/7e4376c006c508370b82bc7bd37173fab51dbd01/git/.gitconfig.sh

tenhobi
  • 1,977
  • 3
  • 23
  • 49
  • @HoBi it seems that you have now deleted your gitconfig.sh from GitHub, any reason why you no longer wanted it? – Novice C Sep 27 '16 at 22:36
  • 3
    @NoviceC There is the file if ya want it. https://github.com/HoBi/dotfiles/blob/7e4376c006c508370b82bc7bd37173fab51dbd01/git/.gitconfig.sh I have came to believe, that you shouldn't store .gitconfig or gitconfig.sh in you git project - these are personal settings and should be stored in your global settings in `~/` folder. And everybody might have different need, so why bother everybody with your config in git repo. :-) – tenhobi Sep 29 '16 at 08:06
  • 4
    @tenhobi there are some configs that might be interesting for everyone to share in a project. I will do so for aliases, with project coding flow – Bernardo Dal Corno Nov 13 '19 at 20:29