The difference between git checkout
and git switch
:
- Both are used to perform actions on branches (create, delete, navigate)
git checkout
is doing more thangit switch
git switch
has been created to be more simpler
In practice, I always use git switch
instead of git checkout
. But let’s explore some use cases.
git switch
To switch to an existing branch:
git switch <existing-branch>
To create a new branch and directly jump into it:
git switch -c <new-branch>
Notes:
- The
-c
option is short for--create
. - There are rules regarding branch naming convention. See What are the git branch naming conventions.
- You can create an alias to write
git sc <new-branch>
instead ofgit switch -c <new-branch>
(see How to create git aliases).
git checkout
To switch to an existing branch:
git checkout <existing-branch>
To create a new branch and directly jump into it:
git checkout -b <new-branch>
To delete a branch:
git checkout -D <deleted-branch>
Note: in practice you won’t have to delete a branch manually. This will be done automatically after merging the Pull Request (on Github) or Merge Request (on Gitlab).
Last few words
We haven’t explored the full capacities provided by both methods. However, we have seen that both of them provides overlapping logics. To go in-depth, you can dive into the official documentation:
On more thing: we didn’t had the chance to stop and talk about git branch
, a functionality allowing us to perform more actions on branches such as renaming a branch This will be done in another chapter (in progress).