That won't work. You have only 3 legitimate options:
- disable depth writes (using the GL API, not in GLSL) -- this will not write depth for any pixel
- don't write to gl_FragDepth in your shader (not at all) -- this will use the interpolated depth instead
- discard -- this will kill the fragment, including depth (but also color)
Writing to gl_FragDepth for some fragments and not doing it for some others is theoretically possible, but a shader doing so is not well-formed. The outcome is undefined.
To throw away pixels that you don't want, I'd just discard, that's what it's for. It discards the color value, nothing is written out.