Branches
A branch is a separate line of work. main stays working while you build a feature on your own branch.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Nov 25, 2026
Learning objectives#
- Explain why branches exist
- Create and switch branches
- Push a branch to GitHub
- Delete a branch after merging
Explanation#
main should always work. For each feature or fix, create a branch such as feature/contact-form, commit there, and merge it into main when it is ready (usually through a pull request).
git switch -c feature/contact-form creates the branch and switches to it. git switch main goes back. Your files change on disk to match the branch you are on — commit or stash work before switching.
The first push of a new branch needs -u: git push -u origin feature/contact-form. On GitHub you can also create a branch from the branch dropdown above the file list.
After the branch is merged, delete it (GitHub offers a button) and run git branch -d feature/contact-form locally to keep things tidy.
Step-by-step screens#
Drawings of github.com, the Git installer, VS Code and a terminal. GitHub changes its layout from time to time; the button names stay the same. The red numbers match the numbered actions under each picture; names on your computer will differ.
Loading pictures…
Code example: A feature branch from start to push#
git switch main
git pull
git switch -c feature/contact-form
# ...edit contact.html, commit...
git commit -am "Add contact form"
git push -u origin feature/contact-form
git branch
Switched to a new branch 'feature/contact-form' [feature/contact-form 9a8b7c6] Add contact form branch 'feature/contact-form' set up to track 'origin/feature/contact-form'. * feature/contact-form main
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Start from an up-to-date main.
- switch -c creates and switches.
- Commits now go to the feature branch only.
- push -u publishes the branch and links it.
- git branch lists branches; * marks the current one.
Real-world use case#
While a contact form is half-finished on its branch, a typo on the live site is fixed on main and deployed — the unfinished work never goes live.
Common mistakes#
Half-finished work ends up live. Branch first, even when working alone.
Git either blocks the switch or carries the changes along. Commit or git stash first.
Best practices#
- One branch per feature or fix
- Names like feature/…, fix/…
- Keep branches short-lived
- Delete after merging
Remember this#
git switch -c name = new branch.
push -u the first time.
main stays working.
Practice exercise#
Your task: Create and switch to a branch called fix/footer-link.
Show solution
git switch -c fix/footer-link
Mini quiz#
Which command creates a branch and switches to it?
Want more? Practise all Git & GitHub MCQs · Git & GitHub interview questions
My notes#
Saved in this browser.