views:

141

answers:

3

I have a git repository that's used only to hold graphics and sound files used in several projects. They are all in one directory without sub-directories. Now I just created a script to copy these assets over from another, structured directory, with several levels of sub-directories.

Now I only want the (source) hierarchical file structure to be tracked by git, and the (target) flat directory (with all the files in one pile) should be ignored.

I've added the target directory to .gitignore, but git is still tracking changes in it. I thought if I commit the deletion of the old file in the target directory, git might stop tracking the new contents (copied in by the script), but it doesn't.

How do I make git forget about the target directory?

A: 

For a subdirectory called blah/ added to git, both of the following seem to work to ignore new files in blah/. Added to .gitignore:

blah 
blah/*
Stef
Yep tried both of these, but thanks.
Felixyz
+6  A: 

This command will cause git to untrack your directory and all files under it without actually deleting them:

git rm -r --cached <your directory>

The -r option causes the removal of all files under your directory.

The --cached option causes the files to only be removed from git's index, not your working copy. By default git rm <file> would delete <file>.

Gordon Wilson
Ah, thanks. Very useful, although I don't see why this should be necessary (why isn't it enough to add the dir in .gitignore?). Could you tell me what the --cached is for?
Felixyz
sure. I've amended my answer to describe the two options.
Gordon Wilson
As for why this is necessary, I can only speculate. I'd guess that the creators wanted the untracking of files to be explicit in order to lessen the risks of mistakenly untracking important parts of your project. I imagine there are also technical reasons.
Gordon Wilson
The gitignore is used to determine what untracked files git will be aware of; it has no bearing on files already being tracked. If the user wants to track otherwise ignored files, git lets them. This could be either be done using `add -f` or be your situation. As Gordon said, this keeps you from accidentally wiping out a bunch of files - the repository is the master list, not the gitignore. This also lets you manually track a few things that would otherwise be ignored by a wildcard rule, something I've found useful.
Jefromi
A: 

Ok, it seems that you must first do a check-in with the directory completely empty (neither old nor new files), and any files added thereafter will be ignored. If you remove the old files add and new ones before committing, the new ones are added to the repo although they should be ignored.

At least, this worked for me in this situation. Would still be great if anyone could provide more insight on what's going on.

Felixyz
Short version of what we've said elsewhere - the gitignore affects what untracked data git will be aware of. It does not affect what will be done with data you've told it about (e.g. directed it to add to the repository).
Jefromi
So it makes sense that the files that were tracked and are now deleted should be tracked as such, but it still doesn't make sense to me that the files *added* (if done before the next commit) are tracked, whereas if I 1)ignore, 2)delete files, 3) commit, 4) add new file, the new files are not tracked.
Felixyz