# Create and switch to a new Git branch

Published: 2026-11-27

Rights: original
Source: https://kairos-2z6s.onrender.com/res/create-and-switch-git-branch-en
## What a branch is for
A branch is a separate line of work. You try an idea on it, and `main` stays untouched until you decide to merge.

```
git switch -c feature-x
git branch
```
(Test: the output listed `* feature-x` and `main`, the star marking the current branch.)

Go back with `git switch main`. Delete a merged branch with `git branch -d feature-x`.

## Common mistakes
- Switching with uncommitted edits. (Test: `git switch x` stopped with `error: Your local changes to the following files would be overwritten by checkout`. Commit or `git stash` first.)
- Creating a name that exists. (Test: `git switch -c main` gave `fatal: A branch named 'main' already exists.`)
- Typing a branch name that is not there. (Test: `git switch nope` gave `fatal: invalid reference: nope`. Run `git branch` to see the real names.)
- Deleting a branch with unmerged commits. (Test: `git branch -d x` gave `error: The branch 'x' is not fully merged.` Git then names `git branch -D x` as the force option; use it only if you accept losing those commits.)

Q: Does `git switch -c` copy my files?
A: No. It makes a new pointer at your current commit and moves you to it. Your files stay as they are.

Q: What is the difference between `git switch` and `git checkout`?
A: `git switch` only changes branches, so it is harder to misuse. `git checkout` also restores files and does more than one job.

Source: https://git-scm.com/docs/git-switch and https://git-scm.com/docs/git-branch

## Related guides
- [How to fix a Git merge conflict step by step](https://kairos-2z6s.onrender.com/res/fix-git-merge-conflict-en)
- [curl basics: fetch a page, save it, read the status code](https://kairos-2z6s.onrender.com/res/curl-basics-full-en)
- [chmod: read the permission string and change it](https://kairos-2z6s.onrender.com/res/chmod-basics-full-en)
- [Bash variables, loops and if: the basics](https://kairos-2z6s.onrender.com/res/bash-variables-loops-full-en)
