Benjamin
Benjamin

Reputation: 1286

Can a git project track a directory listed in my global gitignore?

I'm working on a project that needs to track it's node_modules directory. I don't really want to remove my global gitignore reference to that directory because all other projects should not be tracking the node_modules directory.

Is there a way to force git to track something at the project level? Or do I need to go through all projects verifying each has it's own ignore and then remove the reference from me global gitignore?

Upvotes: 0

Views: 30

Answers (2)

torek
torek

Reputation: 488183

If a file is tracked, its presence in an ignore file is ignored (if I may put it this way :-) ).

I've always said that .gitignore is kind of the wrong name for the file, because it's not really a "list of files to ignore". Instead, it has two purposes. Git checks for an ignore-file entry in two cases:

  • when it's about to gripe that there's an untracked file, or
  • when it's about to clobber a file and it's not sure if this is allowed.

The first case is most often seen from git status: adding a file, or pattern, to your ignore files tells git "please shut up about *.o and the like, they're not supposed to be tracked, so don't complain that they're not tracked." In this case the entries should probably be in a file called .git-shut-up-about-these-files.

The second case is a bit more insidious. Let's say you have, in a git repository, a commit with files a1 and a2, and that's your current commit (because it's the tip of the branch, or because you did git checkout <commit>, or whatever, and those two files are in that commit: for argument sake let's say that this is the tip commit on master). Let's also say that you have another commit 1234567 with just file a1, no a2. If you now do:

git checkout 1234567

git must remove file a2. That's OK, because it's in your other commit (the tip of master), a2 is in there. If you switch back:

git checkout master

git re-creates a2.

But what if, in the tip of master, there's a file named important that's not in 1234567? You git checkout 1234567 and then you stop and make some important notes:

$EDITOR important

You write out those important notes and exit the editor, and then decide to git checkout master. Git now needs to clobber the notes you just wrote, because the tip of master contains a file named important.

What git will do in this case is check your ignore files for the name important. If that's in there, git feels free to wipe out that file and replace it with the one from the commit. If not, git checkout warns that checking out would overwrite important and asks you to get it out of the way first.

These file name entries should probably be in a file called .git-go-ahead-and-clobber-these-files-they-are-unimportant.

Note that there is no entry you can make to mark a file "not to be stored in the repostory, but precious: do not clobber".

Upvotes: 3

merlin2011
merlin2011

Reputation: 75555

Yes, you can add a negation to a local .gitignore to make git track it only for this project.

!node_modules

Upvotes: 1

Related Questions