A DevOps engineer is working on a local copy of a Git repository. The engineer would like to switch from the main branch to the staging branch but notices the staging branch does not exist. Which of the following Git commands should the engineer use to perform this task?
Correct Answer: D
In Git, if a branch does not exist and you need to create and switch to it in one command, use:
git checkout -b <branch_name>
OR
git switch -c <branch_name>
(For newer Git versions, switch is preferred.)
* git checkout -b staging
* -b creates a new branch.
* staging is the new branch name.
* This also switches to the new branch immediately.
* Why the other options are incorrect?
* A. git branch -m st # Incorrect, -m is used for renaming a branch, not creating one.
* B. git commit -m staging # Incorrect, commit -m is used to commit changes with a message, not to create a branch.
* C. git status -b staging # Incorrect, git status shows the current repository state, but -b is not a valid option for it.
References:
* Git Branching - Git Docs