0

I don't have much knowledge on .gitignore, I just know that it helps us untrack the files we don't want to commit to a repository.

In my case, I have two .gitignore files by default

first one -> inside .idea folder

enter image description here

second one -> inside project directory enter image description here

Tried as I might, I couldn't prevent it from tracking mydb folder, .dat files, .idea folder.

This is what I get when I git status

 modified:   .gitignore
        modified:   .idea/.gitignore
        modified:   .idea/APIdesign.iml
        modified:   .idea/libraries/Maven__antlr_antlr_2_7_7.xml
        modified:   .idea/libraries/Maven__ch_qos_logback_logback_classic_1_2_3.xml
        modified:   .idea/libraries/Maven__ch_qos_logback_logback_core_1_2_3.xml
        modified:   .idea/libraries/Maven__com_fasterxml_classmate_1_5_1.xml
        modified:   .idea/libraries/Maven__com_sun_activation_jakarta_activation_1_2_2.xml
        modified:   .idea/libraries/Maven__com_sun_istack_istack_commons_runtime_3_0_11.xml
        modified:   .idea/libraries/Maven__com_zaxxer_HikariCP_3_4_5.xml
        modified:   .idea/libraries/Maven__jakarta_annotation_jakarta_annotation_api_1_3_5.xml
        modified:   .idea/libraries/Maven__jakarta_persistence_jakarta_persistence_api_2_2_3.xml
        modified:   .idea/libraries/Maven__jakarta_transaction_jakarta_transaction_api_1_3_3.xml
        modified:   .idea/libraries/Maven__jakarta_xml_bind_jakarta_xml_bind_api_2_3_3.xml
        modified:   .idea/libraries/Maven__org_apache_derby_derby_10_14_2_0.xml
        modified:   .idea/libraries/Maven__org_apache_logging_log4j_log4j_api_2_13_3.xml
        modified:   .idea/libraries/Maven__org_apache_logging_log4j_log4j_to_slf4j_2_13_3.xml
        modified:   .idea/libraries/Maven__org_aspectj_aspectjweaver_1_9_6.xml
        modified:   .idea/libraries/Maven__org_dom4j_dom4j_2_1_3.xml
        modified:   .idea/libraries/Maven__org_glassfish_jakarta_el_3_0_3.xml
        modified:   .idea/libraries/Maven__org_glassfish_jaxb_jaxb_runtime_2_3_3.xml
        modified:   .idea/libraries/Maven__org_glassfish_jaxb_txw2_2_3_3.xml
        modified:   .idea/libraries/Maven__org_hibernate_common_hibernate_commons_annotations_5_1_2_Final.xml
        modified:   .idea/libraries/Maven__org_jboss_logging_jboss_logging_3_4_1_Final.xml
        modified:   .idea/libraries/Maven__org_slf4j_jul_to_slf4j_1_7_30.xml
        modified:   .idea/libraries/Maven__org_slf4j_slf4j_api_1_7_30.xml
        modified:   .idea/libraries/Maven__org_yaml_snakeyaml_1_27.xml
        modified:   .idea/misc.xml
        modified:   mydb/db.lck
        modified:   mydb/log/log.ctrl
        modified:   mydb/log/log1.dat
        modified:   mydb/log/logmirror.ctrl
        modified:   mydb/seg0/c101.dat
        modified:   mydb/seg0/c111.dat
        modified:   mydb/seg0/c121.dat
        modified:   mydb/seg0/c130.dat
        modified:   mydb/seg0/c141.dat
        modified:   mydb/seg0/c150.dat
        modified:   mydb/seg0/c161.dat
        modified:   mydb/seg0/c171.dat
        modified:   mydb/seg0/c20.dat
        modified:   mydb/seg0/c230.dat
        modified:   mydb/seg0/c31.dat
        modified:   mydb/seg0/c41.dat
        modified:   mydb/seg0/c490.dat
        modified:   mydb/seg0/c4a1.dat
        modified:   mydb/seg0/c51.dat
        modified:   mydb/seg0/c60.dat
        modified:   mydb/seg0/c71.dat
        modified:   mydb/seg0/c81.dat
        modified:   mydb/seg0/c90.dat
        modified:   mydb/seg0/ca1.dat
        modified:   mydb/seg0/cb1.dat
        modified:   mydb/seg0/cc0.dat
        modified:   mydb/seg0/cd1.dat
        modified:   mydb/seg0/ce1.dat
        modified:   mydb/seg0/cf0.dat
        modified:   pom.xml
        modified:   src/main/java/io/theBMan/springBootStarter/Application.java
        modified:   src/main/java/io/theBMan/springBootStarter/lecture/Lecture.java
        modified:   src/main/java/io/theBMan/springBootStarter/lecture/LectureController.java
        modified:   src/main/java/io/theBMan/springBootStarter/lecture/LectureRepository.java
        modified:   src/main/java/io/theBMan/springBootStarter/lecture/LectureService.java
        modified:   src/main/java/io/theBMan/springBootStarter/student/Student.java
        modified:   src/main/resources/application.properties

my two .gitignore files as follows

inside .idea ->

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
*.dat

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

mydb/*
*/idea/**
.idea/

inside project root directory ->

*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
*.dat

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

mydb/*
.idea/
target/
.dat
.gitignore

I also wonder why there are two .gitignore files. If we need both, what are their uses?

Any help is appreciated

Best,

Burakhan Aksoy
  • 319
  • 3
  • 14
  • 2
    It looks like the files in mydb are already in the githistory (they are modified not new). gitignore only works on new files and doesn't exclude already tracked ones. You need to remove them from git and you should be fine. As for multiple gitignore files, check [this](https://stackoverflow.com/questions/3305869/are-multiple-gitignores-frowned-on) question. – DerMaddi Feb 28 '21 at 10:53
  • but I haven't made a commit yet. I also didn't do git add . , how can it already track them :/ I also don't quite understand why there are two .gitignore files – Burakhan Aksoy Feb 28 '21 at 10:55
  • Does `git log` contain any entries? – DerMaddi Feb 28 '21 at 10:58
  • Yes (if you mean .git/logs). it contains HEAD.txt file and refs folder – Burakhan Aksoy Feb 28 '21 at 10:59
  • If you run the command `git log` it displays your git history and you'll see there are already commits. – DerMaddi Feb 28 '21 at 11:02

1 Answers1

1

Since you are using a build system (maven), you can completely ignore the idea directory.

In PROJECT_ROOT/.gitignore:

/.idea
/target

(/ in .gitignore means root of your the git repo, not root of the filesystem)

Just put all your temporary files in the target directory and they will be ignored.

Maven by default will generate all your .class, .jar and test reports in your target directory.

Jonas Fagundes
  • 1,519
  • 1
  • 11
  • 18
  • I see. I already committed to my repo when I was tracking .idea folder. is there any way now that I can delete .idea folder? Is it destructive to delete? – Burakhan Aksoy Feb 28 '21 at 11:08
  • `git rm --cached -r .idea` and `git commit` will remove from git but keep it in your directory. – Jonas Fagundes Mar 01 '21 at 09:45