git checkout vs. git switch

The difference between git checkout and git switch:

  • Both are used to perform actions on branches (create, delete, navigate)
  • git checkout is doing more than git 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:

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).

Leave a Reply

Your email address will not be published. Required fields are marked *