32

I don't want to display .class files when executing git status.

I created a file named .gitignore and entered the .class in the file but nothing happened.

What is the best way to prevent the .class file from being displayed when you execute git status command?

Brian
  • 14,610
  • 7
  • 35
  • 43
Rodel Sarate
  • 819
  • 3
  • 11
  • 11

3 Answers3

53

Make sure your .class files were not already added to the index.
You would need to git rm -r --cached path/to/.classfiles/ those files first.
(they will still be on the disk, but no longer part of the git index, and will be ignored by the git status)

If you don't want any .class file versioned (but you didn't include them in the .gitignore initially), as Michal Stefanow comments below:

git rm -r --cached *.class

Mark adds in the comments:

For Windows TortoiseGit users like me, follow the instructions in this related post to do the git in the GUI

Right click that file, choose TortoiseGit -> Delete (keep local).
This does git rm --cached.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • ```path/to/.classfiles/``` or simply ```git rm -r --cached *.class``` to remove them all – Mars Robertson May 01 '15 at 09:48
  • @MichalStefanow Indeed. I have included your comment in the answer for more visibility. – VonC May 01 '15 at 09:51
  • Just a convenience, usually we want to exclude all ```class``` files, I cannot think of any case where some of them should be kept in source control. – Mars Robertson May 01 '15 at 10:06
  • @MichalStefanow I agree. This `git rm` command is usually done when you forgot to include `.class` in the `.gitignore`, and added by mistake some `.class` files to the index. – VonC May 01 '15 at 10:09
  • For Windows TortoiseGit users like me, follow the instructions in this related post to do the git in the GUI - https://stackoverflow.com/a/12292047/271985 – Mark Feb 24 '21 at 21:13
  • @Mark Thank you. I have included your comment in the answer for more visibility. – VonC Feb 24 '21 at 22:35
15

You probably actually want to add *.class into your .gitignore file, not .class - the former will match any class file (because of the wildcard *), whereas the latter only matches a file named exactly .class.

Amber
  • 507,862
  • 82
  • 626
  • 550
2

Create a new file named ./gitignore in your directory and add the following lines in that:

target/
bin/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
target/
KayV
  • 12,987
  • 11
  • 98
  • 148