Ignoring an already checked-in directory's contents?

GitVersion Control

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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>.

Solution 2 - Git

If you need to have a tracked file (checked in), but do not want to track further changes of a file while keeping it in your local repository as well as in the remote repository, this can be done with:

git update-index --assume-unchanged path/to/file.txt

After that any changes to this file will no longer show up in git status.

Solution 3 - Git

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/*

Solution 4 - Git

TL;DR to clean up your git repository and make sure ALL your ignored items are indeed ignored:

git rm -r --cached .
git add .
git commit -m "Clean up ignored files"

Note: you don't need to specify a directory, this way your are cleaning the entire remote repo.

To go further read this

Solution 5 - Git

Not sure if this counts or makes me a bad person, but here it goes.

  1. I added *Generated* to the root .gitignore file
  2. I submitted the files I want to keep as GeneratedFile.Whatever.ext.CheckedIn
  3. I made a git hook on post checkout to call a powershell script doing this:

dir *.CheckedIn -Recurse | %{ copy $_.FullName "$($_.FullName)".Replace("CheckedIn","") -EA SilentlyContinue}

I do feel a little bad about myself... but it does in fact work.

Solution 6 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFelixyzView Question on Stackoverflow
Solution 1 - GitGordon WilsonView Answer on Stackoverflow
Solution 2 - GitjoneView Answer on Stackoverflow
Solution 3 - GitStefView Answer on Stackoverflow
Solution 4 - GitCedView Answer on Stackoverflow
Solution 5 - GitSteveView Answer on Stackoverflow
Solution 6 - GitFelixyzView Answer on Stackoverflow