Why Git Added git switch and git restore to Replace git checkout
Git 2.23 splits the overloaded git checkout command into two focused commands—git switch for branch operations and git restore for file recovery—making version‑control workflows clearer and reducing confusion for new users.
Background
git checkout has long served both branch switching and file restoration, causing confusion for newcomers.
New Commands in Git 2.23
Git 2.23 introduced git switch and git restore to separate these responsibilities, aiming to simplify the workflow.
Branch Management with git switch
Traditional commands:
git checkout <branch>
git checkout -b <branch>Replaced by:
git switch <branch>
git switch -c <branch>Examples:
$ git switch my-feature
Switched to branch 'my-feature'
Your branch is up to date with 'origin/my-feature' $ git switch -c my-new-feature
Switched to a new branch 'my-new-feature'Switch can also create a branch from a specific commit:
$ git switch -c my-new-feature 0810beaed7
Switched to a new branch 'my-new-feature'File Restoration with git restore
git restorefocuses on restoring file contents, with options to affect the working tree, the index, or both.
Key forms:
git restore --worktree --staged <path>
git restore --source HEAD~3 --staged --worktree main.cBy default it restores from the index; using --source you can specify another commit.
Note: --no-overlay vs --overlay control whether missing tracked files are deleted.
Conclusion
The new commands provide clearer, more focused operations, reducing the cognitive load of git checkout and helping users adopt best practices for branch handling and file recovery.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
