The rebase command should do this automatically.