1

How can I commit only one specific jar file to the git repository and ignore the rest of all other jar files?

I have a situation where I have to commit a jar file and I also need to ignore the rest of the other jars files.

My .gitignore have *.jar which means it ignores all jar files, but my use-case should allow one specific jar file to allowed ignoring all others.

kalyan
  • 11
  • 2
  • 3
    Probably a duplicate of https://stackoverflow.com/questions/987142/make-gitignore-ignore-everything-except-a-few-files – Caius Jard Jan 13 '20 at 20:27
  • 1
    Does this answer your question? [Make .gitignore ignore everything except a few files](https://stackoverflow.com/questions/987142/make-gitignore-ignore-everything-except-a-few-files) – Zeitounator Jan 13 '20 at 20:29

2 Answers2

2
*.jar
!jar-to-keep.jar

References

  1. See: Make .gitignore ignore everything except a few files

  2. From Git Docs:

    An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "\!important!.txt".

    ...

    Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

    $ cat .gitignore
    # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar
    
Will
  • 6,601
  • 3
  • 31
  • 42
0

You can ignore all jars (add .jar to .gitignore) and do git add -f somejar so that it is added, no need to add a second rule to .gitignore.

eftshift0
  • 26,375
  • 3
  • 36
  • 60