37

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.

Community
  • 1
  • 1
Ittai
  • 5,625
  • 14
  • 60
  • 97
  • 1
    Did you commit any of those files in `target/` directory to your repository before? Adding file to `.gitignore` has no effect if the file is already tracked by git. – KL-7 Dec 26 '11 at 08:19
  • @KL-7 My partner made the initial commit but that actually sounds like it might be the issue. I'll check and report back. If so you can post it as an answer and I'll accept. – Ittai Dec 26 '11 at 08:31

3 Answers3

71

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

KL-7
  • 46,000
  • 9
  • 87
  • 74
  • Sorry for the noob but when I do `git rm -r --cached target/` does it only delete in my local repository? If so, then I later need to push the local repository to my remote repository, right? – Ittai Dec 26 '11 at 08:45
  • Yes, first you need to commit that changes and then push to remote repository. – KL-7 Dec 26 '11 at 08:46
  • I haven't forgotten. I'm having some problems with this issue but I think it's relating more to my current inexperience with git than your solution. I'll post back/accept your answer once I can. Thanks. – Ittai Dec 26 '11 at 13:33
  • Key additional gotcha - "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." many thanks – Darren Shewry Dec 19 '13 at 18:12
  • 1
    From the [docs](https://git-scm.com/docs/gitignore) "The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked. To stop tracking a file that is currently tracked, use git rm --cached." – Ahmad Abdelghany May 24 '16 at 12:26
  • Being struggling to find the problem for the last hours failing to see that I committed the "Target" folder. Thank you!! – Pavlos Klonis Sep 09 '21 at 14:41
  • I failed to see that i previously already committed the target folder, thank you very much this helped me alot ! – Anindhito Irmandharu Jun 25 '23 at 03:21
6
target
target/*

Add these lines in the gitignore to ignore the target file and all the files inside target. This should not track that folder and its content anymore.

Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
2

Alternatively, using tortoisegit on folder target simply choose "Delete and add to ignore list".

István Békési
  • 993
  • 4
  • 16
  • 27