Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with ignoring subdirectory in git

Tags:

git

gitignore

I have a maven multi-modules project which has a depth of around 5 levels.
I'm now moving to git and I see that a lot of the content of the target folder is captured by git as "Unstaged Changes".
I've googled a lot and searched SO but couldn't find an answer as to how to get git to ignore the entire directory tree of the target folder.
My .gitignore, which resides at the root of the project, looks like this:

.project   .classpath .settings/ target/ 

The strange thing is that the .class files, which reside somewhere under the target tree, do get ignored by the above definitions but files like MANIFEST.MF, pom.properties or my own application properties files which are located somewhere in the target directory tree are not ignored.
I've read Git Ignores and Maven targets but that solution does not seem to work for me.
I can also say that when I try to add target/* rule it doesn't change anything and when I replace target/ with the target/* then the .class files appear as unstaged as well.
I'd really appreciate some guidance as it seems very unlikely to me that the hordes of people using git and maven haven't already resolved similar issues. Thanks.

like image 680
Ittai Avatar asked Dec 26 '11 08:12

Ittai


People also ask

Can Gitignore ignore folders?

gitignore is a plain text file in which each line contains a pattern for files or directories to ignore. It uses globbing patterns to match filenames with wildcard characters. If you have files or directories containing a wildcard pattern, you can use a single backslash ( \ ) to escape the character.

How do I stop git from ignoring files?

If you already git add ed some files, their changes will still be tracked. To remove those files from your repository (but not from your file system) use git rm --cached on them. git rm --cached file_name. ext wroks fine for me to update gitignore for one file.

What files should be ignored in git?

Ignored files are usually build artifacts and machine generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are: dependency caches, such as the contents of /node_modules or /packages. compiled code, such as .o , .


1 Answers

One of the reasons might be that you already committed that files to your repository. In that case adding them to .gitignore doesn't prevent git from tracking these files. To fix that you should first remove them from repository like that:

git rm -r target/ 

(If your project has modules, you need to include the path down to the relevant module's target:
git rm -r parent_project/module_name/target/)

If you want to remove directory from repository but not from disk you can use --cached option:

git rm -r --cached target/ 

In any case you should then commit that deletion to the repository (you can do it in the same commit with adding target/ to .gitignore if you wish).

like image 93
KL-7 Avatar answered Sep 28 '22 02:09

KL-7