5

I can tell Git where the Git repository is with --git-dir. I can tell Git where the working tree is with --work-tree. How do I tell Git where the gitignore file is?

Q. Why would I want to do such a thing?

A. For some working trees I have two different Git repositories. One is in the standard location of .git. This repository I use for normal version control. The other respository is in .git.sync. This repository I use for periodic automatic syncing between computers. I.e., it's my own little Dropbox clone implemented with Git and a little script that runs periodically.

Ideally, I would be able to tell Git to use .gitignore.sync for the .git.sync repository, rather than having Git use the very same .gitignore that it uses for normal version control.

Q. Why don't I just use Dropbox?

A. It doesn't sync symlinks. Bad Dropbox!

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
Douglas
  • 2,555
  • 3
  • 24
  • 30

3 Answers3

4

You can use core.excludesfile to specify the new ignore file. By default this will only affect the current repository; you can use the --global option if you want to change the default value.

git config core.excludesfile ".new_gitignore"

Edit 1

AFAIK, .gitignore cannot be disabled. And it takes precedence over .git/info/excludes and core.excludesfile. The only way I can think of is having some hacks using filters. This answer explains the use of filters well.

Community
  • 1
  • 1
Srikanth Venugopalan
  • 9,011
  • 3
  • 36
  • 76
  • 1
    Thanks! Apparently this allows you to specify a third gitignore file, in addition to `.gitignore` and `.git.sync/info/exclude`. I also need to be able to ***turn off*** `.gitignore`, since the directory may have a `.gitignore` that is not within my ability to control. – Douglas Mar 21 '13 at 01:57
  • @Douglas, have some edits based on your comment. AFAIK there is no way to turn off .gitignore. – Srikanth Venugopalan Mar 21 '13 at 06:28
  • @Srinkanth, thanks for the update! I'm having a hard time figuring out, though, how the filter mechanism could be used to solve this particular problem. – Douglas Mar 21 '13 at 14:11
1

Each repository has an info/exclude file - there should be one in .git/info/exclude and in .git.sync/info/exclude. Populate those exclude files just as you would .gitignore.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • Thanks! I don't think that would *always* work for me, though, unless I can tell Git also to not also use `.gitignore`. For projects that I work on with other people, I'm not always free to get rid of `.gitingore`. – Douglas Mar 21 '13 at 01:45
1

".gitignore" seems to be hard-coded as a per-directory exclude file in git in most commands, taking precedence over all other ignore files.

However, I created a small patch for Git to overcome this limitation: https://gist.github.com/rev22/9954517

It adds the following configuration option:

core.ignoreperdir: Specify an alternative to '.gitignore' as a per-directory exclude-file. If this is not set, the default '.gitignore' is used. If this is empty, no per-directory exclude file is used.

Rev22
  • 11
  • 2