As per Exceptions in a .gitignore file, The right way to do it is:
!dir dir/* !dir/file3
Note that there are other solutions, such as creating a local
.gitignore file in
dir/, which will take precedence over the previous one.
Curiously, I also tried your solution, and
Seems to work for me. If someone could comment on this, it would be appreciated.