We investigate the laminar shedding of hairpin vortices in the wake of a truncated square cylinder placed in a duct, for Reynolds numbers around the critical threshold of the onset of vortex shedding. We single out the formation mechanism of the hairpin vortices by means of a detailed analysis of the flow patterns in the steady regime. We show that unlike in previous studies of similar structures, the dynamics of the hairpin vortices is entwined with that of the counter-rotating pair of streamwise vortices, which we found to be generated in the bottom part of the near wake (these are usually referred to as base vortices). In particular, once the hairpin structure is released, the base vortices attach to it, forming its legs, so these are streamwise, and not spanwise as previously observed in unconfined wakes or behind cylinders of lower aspect ratios. We also single out a trail of Ω-shaped vortices, generated between successive hairpin vortices through a mechanism that is analogous to that active in near-wall turbulence. Finally, we show how the dynamics of the structures we identified determine the evolution of the drag coefficients and Strouhal numbers when the Reynolds number varies.