diff --git a/packages/flutter_tools/lib/src/commands/channel.dart b/packages/flutter_tools/lib/src/commands/channel.dart index 6a9ee18bc9..0b5599e12c 100644 --- a/packages/flutter_tools/lib/src/commands/channel.dart +++ b/packages/flutter_tools/lib/src/commands/channel.dart @@ -92,11 +92,26 @@ class ChannelCommand extends FlutterCommand { } static Future _checkout(String branchName) async { - final int result = await runCommandAndStreamOutput( - ['git', 'checkout', '-b', branchName], + int result = await runCommandAndStreamOutput( + ['git', 'show-ref', '--verify', '--quiet', 'refs/heads/$branchName'], workingDirectory: Cache.flutterRoot, prefix: 'git: ', ); + if (result == 0) { + // branch already exists, try just switching to it + result = await runCommandAndStreamOutput( + ['git', 'checkout', branchName], + workingDirectory: Cache.flutterRoot, + prefix: 'git: ', + ); + } else { + // branch does not exist, we have to create it + result = await runCommandAndStreamOutput( + ['git', 'checkout', '--track', '-b', branchName, 'origin/$branchName'], + workingDirectory: Cache.flutterRoot, + prefix: 'git: ', + ); + } if (result != 0) throwToolExit('Switching channels failed with error code $result.', exitCode: result); }