The difference between git checkout -b and git switch -c

Both `git switch` and `git checkout` commands can be used to create a new branch and switch to the newly created branch. But they have some differences in the following aspects.

1. Command syntax: `git switch -c` is used to create and switch to a new branch, the command syntax is:

git switch -c <new_branch>

`git checkout -b` is also used to create and switch to a new branch, the command syntax is:

git checkout -b <new_branch>

2. Usage scenarios: In versions prior to Git 2.23, the `git checkout` command was used to create and switch branches, check out files, and undo changes. After Git version 2.23, the `git switch` command was introduced to divide the functions of these different scenarios into several independent commands. Therefore, if you just need to create and switch to a new branch (no need to check out files or undo changes, etc.), it is recommended to use `git switch` command.

3. Security: The `git switch` command can better ensure the security of branch operations, it will prohibit switching branches on uncommitted changes (unless the `--discard-changes` option is used to discard changes).

The `git checkout` command can create new branches on uncommitted changes, which may lead to problems such as data loss.

In summary, if you only need to create and switch to a new branch, and use a version after Git 2.23, it is recommended to use the `git switch` command. If you need to create a new branch on uncommitted changes or Git version is too old, use `git checkout` command.

Guess you like

Origin blog.csdn.net/qq_28165595/article/details/131142915