How do I override a particular extension on a .gitignore file?

106 Views Asked by At

I have a rule in the .gitignore file of my GitHub repo to ignore all .jar files that will come up during development.

Now, I am working on an upgrade task, which has some Jar files that need to go into the remote repository. I can remove the rule from the .gitignore file but I will have to cherry-pick each relevant jar file among 100 more Jars that were been ignored previously.

The work I am doing is isolated to a few folders and subfolders on the repo.

There must be some easier way to achieve this goal. Googling won't help because I don't even know what I should be searching for.

From the answer to this question https://stackoverflow.com/a/60558023/14310825, I have tried to try to override that rule. For example, if my project directory looks like this.

ProjectFolderStructure

This is the real folder structure of my local, .git and .gitignore are present inside the custom folder [invisible here]

and my .gitignore looks like this:

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

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

# Package Files #
*.jar
!solr-commerce-plugins*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

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


##############################
## Eclipse
##############################
.metadata
*.tmp
*.bak
*.swp
*~.nib
.loadpath
.factorypath
eclipsebin

##############################
## OS X
##############################
.DS_Store       

##############################
## Maven
##############################
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
pom.xml.bak
release.properties
dependency-reduced-pom.xml
*build.number
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar

##############################
## SAP CC
##############################
/core-customize/c4c-integration
/core-customize/licenses
/core-customize/hybris/temp
/core-customize/installer
/core-customize/hybris/log
/core-customize/hybris/data
/core-customize/hybris/bin/platform
/core-customize/hybris/bin/modules
/core-customize/hybris/bin/dummy.txt
/core-customize/README
/core-customize/SIGNATURE.SMF
/core-customize/build-tools
*classes/
/core-customize/hybris/bin/custom/avantor*/**/*jalo/
*addonsrc/
*addons/
*addontestsrc/
*_ui/
!*rhino-1.7.7.jar
!*wro4j-core-1.8.0.jar
*commonwebsrc/
*gensrc/
/core-customize/hybris/config/backup
/core-customize/hybris/config/tomcat
/core-customize/hybris/config/solr/instances/default/*
!/core-customize/hybris/config/solr/instances/default/configsets
core-customize/hybris/config/solr/instances/default/configsets/default/conf/*
!/core-customize/hybris/config/solr/instances/default/configsets/default/conf/*.xml
!/core-customize/hybris/config/solr/instances/default/configsets/default/conf/lang
/core-customize/hybris/config/orbeon
/core-customize/hybris/config/licence
/core-customize/hybris/config/security
/core-customize/hybris/config/local.properties

# Exclude build-regenerated misc files
/core-customize/hybris/bin/custom/**/build.xml
extensioninfo.xsd
beans.xsd
items.xsd
platformhome.properties
*-webtestclasses.xml
*-testclasses.xml
.lastupdate
core-customize/hybris/bin/custom/avantor/avantorcore/.classpath
*node_modules/
*.rush/
*dist/

\custom\otmmaddon\bin\otmmaddonserver.jar and \custom\otmmaddonbackoffice\bin\otmmaddonbackofficeserver.jar are the jars I need to add to the commit along with two more Jar file for OTMMsmartedit and OTMMws having similar folder structure

what I did was add this two lines on the git ignore

!\custom\otmmaddon\bin\**\.jar
!\custom\otmmaddonbackoffice\**\*.js

Git is still ignoring the files.

Please note that I am working on a Java-Springboot project, the above is an example of what issues I am facing and what I am expecting. I do not want to ignore any .jar files in some directories of my repo

1

There are 1 best solutions below

3
Jonathan Leffler On BEST ANSWER

Transcribing edited forms of my comments ([1] [2] [3]) into an answer.

It's interesting that the text of the question mentions .jar files but the example only covers .js and .css files.


You should be able to create a .gitignore file in the directories where you want to permit tracking of the .jar files. That file should contain !*.jar to permit any .jar file in the directory (and its sub-directories) to be tracked.


You can read about how the .gitignore files work in the Git Book's manual page for gitignore. If there's a .gitignore file in your directory bootstrap/js/Extras that contains !*.js, Git should not ignore the .js files in Extras or its sub-directories.


Your problem with your 'ignore' lines is that you've included bootstrap/ at the start:

!bootstrap/css/Extras/*.css
!bootstrap/js/Extras/**/*.js

but there's no subdirectory bootstrap that those lines apply to. You need to replace these lines with:

!css/Extras/**/*.css

and similarly for .js files and .jar files. You don't need both lines because the /**/ notation is explicitly documented at the linked page as matching zero or more directory levels:

  • A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

Personally, I'd put the rule in a lower-level .gitignore file, as I noted previously. However, I can see a case for having all the rules at the top level. I think it is harder to manage, but that could be a project-wide decision that takes more effort to undo than to add the right rules to the top-level .gitignore file.