In a taxable account, a rebalance can cost real money. Selling $30,000 of equities with a $12,000 embedded gain at a 20% rate hands over $2,400 before the portfolio has improved at all. The order of operations matters.
A practical sequence: first direct new contributions and dividends to the underweight asset; second, rebalance inside tax-sheltered accounts where trades are free of tax consequence; third, sell high-cost-basis lots; last, realise gains only if the drift is still material. Pair any necessary gains with harvested losses where available.
Watch the wash-sale-rule when you sell at a loss and want to keep similar exposure, and see tax-loss-harvesting and cost-basis-method for the mechanics.
Related: rebalancing, wash-sale-rule