Git Checkout and Git Switch
Navigating between branches, restoring files, and visiting old commits are all managed through two commands:
git switch— Introduced in Git 2.23 (2019), designed specifically for switching branchesgit checkout— The older, all-purpose command that handles branches, files, and commit navigation
Both commands are used in real projects today. git switch is the modern, preferred way for branch operations, while git checkout is still widely used — especially for file restoration and visiting old commits.
git switch — The Modern Branch Command
Switch to an Existing Branch
git switch mainSwitches the working directory to the main branch. Files in the working directory immediately reflect the state of the main branch.
Create and Switch to a New Branch
git switch -c feature-dashboardCreates a new branch called feature-dashboard and immediately switches to it.
Create a Branch from a Specific Branch
git switch -c hotfix mainCreates a new branch called hotfix starting from the main branch, regardless of the currently active branch.
Switch Back to the Previous Branch
git switch -This is a quick shortcut — the minus sign means "go back to the last branch that was active." Similar to the "back" button in a browser.
Switch to a Remote Branch
When a teammate has pushed a branch to GitHub and it needs to be checked out locally:
git switch feature-team-memberGit automatically creates a local tracking branch from the remote branch if a local branch with that name does not exist yet.
git checkout — The Classic Command
Switch to an Existing Branch
git checkout mainCreate and Switch to a New Branch
git checkout -b feature-profileRestore a Specific File to Its Last Committed State
If a file has been modified and the changes need to be discarded (reverting the file to the last committed version):
git checkout -- index.htmlWarning: This permanently discards unsaved changes to that file. The changes cannot be recovered.
The modern equivalent (recommended):
git restore index.htmlVisit an Old Commit (Detached HEAD)
It is possible to "go back in time" and see the project as it was at a specific commit:
git checkout a3f92b1This puts Git in a "detached HEAD" state — meaning HEAD is pointing to a specific commit rather than a branch.
Output:
Note: switching to 'a3f92b1'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
What is Detached HEAD?
Normally, HEAD points to a branch (like main), which in turn points to the latest commit. In Detached HEAD state, HEAD points directly to a specific commit instead of a branch.
Normal: HEAD → main → Commit5
Detached: HEAD → Commit2
In detached HEAD state, the project can be explored at that point in time. However, if new commits are made here, they will be lost when switching back to a branch (unless a new branch is created first).
Getting Out of Detached HEAD State
# Option 1: Go back to main
git switch main
# Option 2: Create a new branch from this point (saves any new commits)
git switch -c explore-old-state
Checkout a File from a Specific Commit
To retrieve a specific file as it was at a specific commit, without switching to that commit entirely:
git checkout a3f92b1 -- index.htmlThis restores index.html to its state at commit a3f92b1 and places it in the staging area.
git restore — The Modern File Restoration Command
Along with git switch, Git 2.23 introduced git restore for file operations that were previously done with git checkout.
Discard Working Directory Changes
git restore index.htmlUnstage a File (Move Out of Staging Area)
git restore --staged index.htmlRestore a File from a Specific Commit
git restore --source=a3f92b1 index.htmlCommand Comparison: Old vs New
| Old Command | Modern Equivalent | Action |
|---|---|---|
git checkout branch | git switch branch | Switch to a branch |
git checkout -b branch | git switch -c branch | Create and switch to branch |
git checkout -- file | git restore file | Discard file changes |
git reset HEAD file | git restore --staged file | Unstage a file |
Summary
git switch is the recommended way to switch between branches and create new ones. git checkout still works and is needed for visiting old commits (Detached HEAD) and restoring files from specific commits. git restore handles file-level operations cleanly. Together, these commands provide full control over navigation within a Git repository.
