Junior — Middle
What methods exist for creating a new branch in Git version control system?
sobes.tech AI
Answer from AI
In Git, there are several ways to create a new branch:
- Command
git branch— creates a branch but does not switch to it:
git branch <branch_name>
- Command
git checkout -b— creates a new branch and immediately switches to it:
git checkout -b <branch_name>
- Command
git switch -c— a modern way to create and switch to a branch (starting from Git 2.23):
git switch -c <branch_name>
For example, to create a branch named feature and switch to it immediately:
git checkout -b feature
or
git switch -c feature
Thus, the choice of command depends on whether you want to switch to the new branch immediately or just create it.