Several recent experiments revealed a change of the sign of the first harmonic in the current-phase relation of Josephson junctions (JJs) based on, e.g., d-wave superconductors or JJs with ferromagnetic barrier. In this situation, the role of the second harmonic can become dominant; in this case, it determines the scenario of a 0-pi transition. We discuss different mechanisms of the second harmonic generation and its sign. If the second harmonic is positive and the first harmonic changes sign as a function of any control parameter, e.g., temperature, the ground state phase changes abruptly between 0 and pi. If the second harmonic is negative, the ground state phase changes continuously passing all values between 0 and pi and the realization of a so-called phi-junction is possible in the vicinity of the point where the first harmonic vanishes. We study the unusual properties of this kind of Josephson junction such as critical currents, magnetic field penetration, plasma gap, microwave response, and zero field steps. We also analyze the possible experimental techniques for the observation of predicted effects.