6

I guess this kind of question has been asked many times (and I've seen several questions on SO dealing with this subject) but I still have a problem to ignore a file in my Git.

The structure of my project looks like this:

myProject/
    .gitignore
    src/test/java/packageName/
        Main.java

I would like to ignore Main.java (the file is different between each user).


.gitignore file

.settings
.project
.classpath
target/
test-output/
node_modules/**
.orig

I've tried:

  • Adding src/test/java/packageName/Main.java to my .gitignore.

  • Creating a .gitignore file in src/test/java/packageName/ that just contains Main.java.

Both solutions have no effect: I still see Main.java in the Git Staging view after restarting Eclipse.


I guess the solution is simple, but I'm stuck on it for several hours, and I hope you can help me.

Thanks! :-)

UPDATE

All Git users have Main.java, but the class is different from one person to another (user name hardcoded in the class).

I want this class to be present on the HEAD branch (so that new developers can recover it), but each developer can have a local version (invisible in Git Staging).

Community
  • 1
  • 1
Mistalis
  • 17,793
  • 13
  • 73
  • 97

5 Answers5

36

Your problem is probably that Main.java is already in your index. The solution

First make it disappear

git rm src/test/java/packageName/Main.java

then add it to your gitignore

echo "src/test/java/packageName/Main.java" >> .gitignore

now commit these changes

git add .gitignore
git commit -m "please ignore me!"

And it should work :-)

You can now recreate your Main.java and it should no longer appear in git status.

BartBog
  • 1,889
  • 14
  • 28
  • The first line deleted my `Main.java` class, the second added `.origsrc/test/java/packageName/Main.java` to .gitignore. So I created again my `Main.java`, but this class is still not ignored by Git... :( – Mistalis Jul 11 '16 at 11:40
  • That is weird... Did you also commit (lines 3 and 4)? Can you give me the output of git status after you did everything I asked? – BartBog Jul 11 '16 at 12:20
  • Regarding the output of git status. Please: give me output of git status BEFORE you recreate the main.java and also AFTER you do that. – BartBog Jul 11 '16 at 12:21
  • When should I recreate Main.java (after which line?) – Mistalis Jul 12 '16 at 08:36
  • After the commit line, you can do anything you want :-) – BartBog Jul 12 '16 at 13:58
  • As I understand, this involves to remove `Main.java` from head branch. I want something more specific: Each user can have a different version of `Main.java`, but a standardized version is available at the main branch. Maybe it is not possible, or not a good practice? I've updated my question to clarify this point. :-) – Mistalis Jul 12 '16 at 14:33
  • Thats not possible to enforce globally. – BartBog Jul 12 '16 at 14:46
  • You can add a "standard copy" in some other folder and put in a readme that users have to copy that one manually to some (ignored) location. – BartBog Jul 12 '16 at 14:47
5

Able to achieve it in Eclipse Oxygen. Here step follows.

  1. Enable "Automatic Ignore" .ignore file from window-> preference. enter image description here

  2. Right Click on file/folder to ignore. For example right click on target folder(contains .class file). Team -> Ignore

  3. Go to Staging view, you will see .ignore file generate.
  4. Right click on .ignore file and ignore that again.
Swarit Agarwal
  • 2,520
  • 1
  • 26
  • 33
0

For disappearing auto-generated compiled .class like files from Git Staging view, Please follow following steps in eclipse.

  1. Refresh Repository
  2. Open navigator perspective (You can search this in perspective).
  3. Now look for .gitignore file in the project directory if not present you can create this file in the project directory.
  4. add the following entries to the .gitignore file -

/.class

/bin/

OR

.class

bin/

  1. Now save the .gitignore file and refresh the repository or eclipse.

The unwanted files are will be disappeared.

Tainoor
  • 21
  • 1
  • 7
0

In eclipse where all the projects names are placed right next to there's small face down arrow ⬇️ click on it then uncheck Class resources

Then you are good to go

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Jeyhun
  • 1
0
  1. Make a copy of Main.java on your desktop.

  2. Delete Main.java files from your project.

  3. Add Main.java to your .gitignore.

  4. Commit these changes.

  5. Place the Main.java file from your desktop back to your project.

Voila! It won't be part of your git index anymore

General Grievance
  • 4,555
  • 31
  • 31
  • 45