Git & GitHub · Daily Workflow

Branches

A branch is a separate line of work. main stays working while you build a feature on your own branch.

Beginner 5 min Lesson 9 of 19Shows Git 2.47 and GitHub.com as of 2026. GitHub changes its layout from time to time; button names stay the same.
Before you start:Commit, Push and Pull
0/19 in Git & GitHub
On this page · 5 min · Beginner

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#

ShellA feature branch from start to push Illustrative
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
Expected output
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#

  1. Start from an up-to-date main.
  2. switch -c creates and switches.
  3. Commits now go to the feature branch only.
  4. push -u publishes the branch and links it.
  5. 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#

Working directly on main

Half-finished work ends up live. Branch first, even when working alone.

Switching with uncommitted changes

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
Shell Illustrative
git switch -c fix/footer-link

Mini quiz#

Branches quizMultiple choice Question 1 of 1

Which command creates a branch and switches to it?

Score so far: 0/0

Want more? Practise all Git & GitHub MCQs · Git & GitHub interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.