Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell Git to use a different gitignore file than ".gitignore"?

Tags:

git

gitignore

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!

like image 490
Douglas Avatar asked Mar 21 '13 01:03

Douglas


People also ask

How do I ignore a specific file in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

Can git use multiple Gitignore files?

You can have multiple . gitignore , each one of course in its own directory. To check which gitignore rule is responsible for ignoring a file, use git check-ignore : git check-ignore -v -- afile .

Can you have two Gitignore files?

gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple . gitignore files.

How do I ignore a git file without Gitignore?

To ignore untracked files, you have a file in your git folder called . git/info/exclude . This file is your own gitignore inside your local git folder, which means is not going to be committed or shared with anyone else. You can basically edit this file and stop tracking any (untracked) file.


2 Answers

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.

like image 170
Srikanth Venugopalan Avatar answered Oct 03 '22 08:10

Srikanth Venugopalan


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.

like image 39
GoZoner Avatar answered Oct 03 '22 09:10

GoZoner