branching
git switch
Modern, focused command (Git 2.23+) that only switches branches — no file restoration overloading like checkout. Safer and clearer for everyday branch changes.
git switch [<options>] <branch> Common flags
| Flag | Purpose |
|---|---|
| -c <new> | Create a new branch and switch to it |
| -C <new> | Create or reset the branch and switch |
| --detach | Switch to a commit in detached HEAD mode |
| --discard-changes | Throw away local modifications while switching |
| - | Switch back to the previously checked-out branch |
Examples
git switch main Switch to the main branch
git switch -c feature/auth Create and switch to a new feature branch
git switch - Toggle back to the previous branch
Gotcha
'switch' won't touch files by design; if you meant to restore a file's contents, use 'git restore' instead.
Related commands
git checkout
Legacy multipurpose command that switches branches, restores files, or detaches HEAD to a commit. In modern Git (2.23+), prefer 'git switch' for branches and 'git restore' for files.
git branch
Lists, creates, renames, or deletes branches — pointers to commits. Does not switch branches on its own; use switch or checkout for that.
git restore
Modern command (Git 2.23+) for restoring working tree files from the index or a commit, or unstaging files. Replaces the confusing file-mode of 'git checkout'.
git merge
Joins the history of another branch into the current one, creating a merge commit when histories diverge. Preserves the full branching topology.
git rebase
Replays commits from your branch on top of another base, producing a linear history without merge commits. Powerful for cleaning up history before pushing.