1

At the top level of our project folder hierarchy, we've created the standard ignore rules to exclude our compiled exe's, dll's, etc. As these rules are specified at the top level of our repository, they are effective for all folders within the project hierarchy.

I have one specific folder that contains several exe's which I do want to synchronize with our Git repository. How do I turn off the exclusion at the subfolder level so that exe's within that folder will be synchronized with our Git repository? (The subfolder is a 'leaf' so once the exclusion is disabled, it can remain as such.)

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Yossi G.
  • 939
  • 1
  • 8
  • 22
  • 1
    You can have a `.gitignore` in that directory, or add a reference to that directory to the one at the root. You can use `!` to negate a pattern. See https://git-scm.com/docs/gitignore – jonrsharpe Jan 21 '20 at 21:25
  • I think this is a duplicate of https://stackoverflow.com/q/5533050/1256452 but if I mark it that way it will close right away and I don't have time to figure out if that's OK, so here's a comment instead. – torek Jan 21 '20 at 21:26
  • @torek No, not a dup. The question is how to unignore some files in a subdirectory ignored at the top-level `.gitignore`. – phd Jan 21 '20 at 21:28
  • @phd: Ah, good thing I didn't close it as a dup then. :-) What's wrong with just modifying the top level `.gitignore`? – torek Jan 21 '20 at 21:35

2 Answers2

2

The answer seems to be very easy:

!*.exe

! means reverse ignore rule. See https://git-scm.com/docs/gitignore#_pattern_format:

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again.

You can add the rule directly in the directory (let's call it path/to/the/leaf) i.e. in the file path/to/the/leaf/.gitignore. Or you can add it in the top-level .gitignore in the form:

!/path/to/the/leaf/*.exe

Add it after ignored *.exe

phd
  • 82,685
  • 13
  • 120
  • 165
  • This answers my question - quick follow up; one of the developers manually added the folder, with its exe files into the git and now it appears that the exe and dll within that folder are being synchronized. Is it true then that gitignore directives apply *before* files are added into the repository, but these directives are irrelevant for files which are already in the repository? Thanks for your advice! – Yossi G. Jan 22 '20 at 17:39
1
# ignore all . files and . folders
.*

# Dont ignore .gitignore (this file)
# This is just for verbosity, you can leave it out if
# .gitignore is already tracked or if you use -f to
# force-add it if you just created it
!/.gitignore

# ignore all . folders but include . files
.*/

What is this pattern?

.* - This patter tells git to ignore all the files which starts with .

! - This tells git not to ignore the pattern. In your case /.gitignore

CodeWizard
  • 128,036
  • 21
  • 144
  • 167