# git switch

> The `git switch` command switches between branches.

## What is the difference between git switch and git checkout?

The command that switches to another branch. With the `-c` flag, it creates a new branch and switches to it. It is a simpler and safer alternative to the branch-related part of `git checkout`.

- Topic: Git Commands - Branches
- Type: command

## Example

Wrong:

```bash
git checkout -b fix/footer
git checkout a1b2c3d
```

Right:

```bash
git switch -c fix/footer
git switch --detach a1b2c3d
```

`switch` handles branches only, and inspecting a commit requires an explicit `--detach`.

## Common mistakes

- Expecting `git switch` to accept a commit hash without `--detach`.

## Key points

- Branch changes use `git switch <branch>`, and new branches use `git switch -c <branch>`.
- File restores use `git restore`, not `git switch` or `git checkout -- <file>`.
- Checking out a commit without a branch uses `git switch --detach <commit>`.

## Interview questions

### How do I go back to the previous branch?

Run `git switch -`. It switches to the branch you were on before.

### Why does git switch refuse to change branches?

Your uncommitted changes would be overwritten by the other branch. Commit or stash them first.

## Related cards

- [`git checkout <branch>`](https://wonderium.dev/processes/git-checkout.md)
- [git restore](https://wonderium.dev/processes/git-restore.md)
- [`git branch <branch>`](https://wonderium.dev/processes/git-branch.md)
- [`git checkout -b <branch>`](https://wonderium.dev/processes/git-checkout-b.md)
- [git worktree](https://wonderium.dev/processes/git-worktree.md)

---

- Level: [Processes](https://wonderium.dev/processes.md)
- HTML version: https://wonderium.dev/processes/git-switch
- Last updated: 2026-11-25
