Git switch & git restore are the new commands to switch between branches and restore files respectively. They are faster and more intuitive than git checkout. Let’s see how to use them.

What is git switch?

Git switch is a new command introduced in git 2.23. It is a replacement for git checkout. It is used to switch between branches. It is also used to create a new branch and switch to it.

How to use git switch?

Switch to a branch

To switch different branches on the git repository, use the following command:

git switch <branch_name>

Create a new branch and switch to it

To create a new branch and switch to it, use the following command. -c is used to create a new branch.

git switch -c <branch_name>

What is git restore?

With newer version of git (2.23+), git restore is introduced as a replacement for git checkout. It is used to restore files from the working tree. It is also used to restore files from the index.

Restore a file

To restore a file, use the following command:

git restore <file_name>

Restore a file from a specific commit

To restore a file from a specific commit, use the following command:

git restore <commit_id> <file_name>

Restore a file from a specific branch

To restore a file from a specific branch, use the following command:

git restore <branch_name> <file_name>

Restore a file from a specific tag

To restore a file from a specific tag, use the following command:

git restore <tag_name> <file_name>

Hope you find this article useful. If you have any questions, please leave a comment below.

References

  1. Git switch
  2. Git restore
  3. Git checkout