Use git switch & git restore instead of git checkout
Image Source: Picsum

Key Takeaways

Git’s modern ‘switch’ and ‘restore’ commands decompose the overloaded functionality of ‘git checkout’ into intuitive, specialized tools. By separating branch navigation from file restoration, Git 2.23+ eliminates the ambiguity of the legacy interface, significantly reducing the risk of destructive errors while streamlining the developer workflow.

  • Git 2.23+ replaces the overloaded ‘git checkout’ with dedicated ‘switch’ and ‘restore’ commands to improve interface clarity and prevent accidental data loss.
  • ‘git switch’ decouples branch management from file manipulation, providing a safer, specialized syntax for navigating and creating branches.
  • ‘git restore’ offers granular control over file-level recovery, allowing developers to target specific sources like the index, commits, or tags without affecting the current HEAD.
  • Transitioning to these purpose-built commands aligns with modern Git best practices, reducing the steep learning curve and cognitive load associated with multi-purpose legacy commands.

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
The SQL Whisperer

The SQL Whisperer

Senior Backend Engineer with a deep passion for Ruby on Rails, high-concurrency systems, and database optimization.

Linux command every web developer should know
Prev post

Linux command every web developer should know

Next post

Online color palette generator tools in 2022

Online color palette generator tools in 2022