LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 714 min read

Branches: Working on Features in Parallel

Understand what a branch really is, create and switch branches with git switch, see how your files change between branches, and visualise history with git log --graph.

On this page 12 sections

Introduction

Priya wants to add a contact page, but her portfolio is live and she does not want half-finished work on it. Branches solve this: they let you develop a feature in its own line of history, while main stays exactly as it is. When the feature is ready, you merge it in (next lesson). If it does not work out, you delete the branch and nothing else is affected.

What You Will Learn
  • What a branch and HEAD actually are inside Git.
  • How to create and switch branches with git switch.
  • How your files change when you switch branches.
  • How to see all branches at once with git log --graph --all.

What Is a Branch?

A branch is simply a movable label that points to a commit. When you make a commit on a branch, the label moves forward to the new commit. That is all — which is why creating a branch in Git is instant and costs almost nothing, no matter how big the project is.

HEAD is another label that points to the branch you are currently on. When you saw (HEAD -> main) in git log, it meant “you are on main, and main points here”. Switching branches moves HEAD and updates your files to match.

main points to the latest commit on the main line of work
↓
git switch -c feature/contact-page creates a new label at the same commit and moves HEAD to it
↓
New commits move only the label HEAD points to — feature/contact-page moves forward, main stays put
↓
Switching back to main moves HEAD and restores main’s version of the files

Listing Branches

git branch
Output

Click Run to see what this code prints.

So far there is one branch, and the * marks the one you are on.

Creating and Switching

git switch -c creates a new branch and switches to it in one step (-c stands for create):

git switch -c feature/contact-page
Output

Click Run to see what this code prints.

git branch
Output

Click Run to see what this code prints.

git checkout vs git switch

git switch was added in Git 2.23 to make switching branches clearer. You will still see the older git checkout <branch> and git checkout -b <branch> in many tutorials; they do the same job.

Committing on a Branch

Priya creates contact.html, commits it, and adds a link to it from the home page:

git add contact.html
git commit -m "Add contact page"
Output

Click Run to see what this code prints.

git commit -am "Link to contact page from home"
Output

Click Run to see what this code prints.

git log --oneline
Output

Click Run to see what this code prints.

The labels tell the story: HEAD -> feature/contact-page is two commits ahead, while main still points to “Stop tracking editor settings”.

Switching Changes Your Files

git switch main
ls
Output

Click Run to see what this code prints.

contact.html has vanished — not deleted, just not part of main. Git rewrote the files in your folder to match the branch you switched to. git switch - jumps back to the previous branch, and the file returns:

git switch -
ls
Output

Click Run to see what this code prints.

Commit Before You Switch

Uncommitted changes travel with you when you switch branches, and Git refuses to switch if they would be overwritten. Commit (or stash, lesson 14) your work first, so each branch keeps its own changes.

Mini Challenge · Predict the Output

On branch feature/login you create and commit login.html. You then run git switch main. What happens to login.html in your folder?

Choose one answer

Visualising Branches

git log normally shows only the branch you are on. Add --all to include every branch and --graph to draw how they connect:

git log --oneline --graph --all
Output

Click Run to see what this code prints.

The history is still a straight line, because main has not moved since the branch was created — the feature branch is simply ahead. In the next lesson you will see lines split and join once both branches get new commits. git branch -v gives a compact summary with each branch’s latest commit:

git branch -v
Output

Click Run to see what this code prints.

Naming Branches

  • Describe the work: feature/contact-page is clearer than priya-branch or test2.
  • Use a prefix for the type of work, such as feature/, fix/, docs/ or chore/.
  • Use lowercase words separated by hyphens; avoid spaces and special characters.
  • Many teams include an issue number: fix/42-broken-contact-link.
Mini Challenge · Put It in Order

Put the steps of building a feature on its own branch in order.

  1. Make sure you are on main with a clean working tree
  2. Create and switch to a branch with git switch -c feature/name
  3. Switch back to main when you need the stable version
  4. Check the result with git log --oneline --graph --all
  5. Edit files and commit on the branch

Common Mistakes

Avoid These Mistakes
  • Committing a feature directly to main because you forgot to create a branch first — check git status before you start.
  • Creating a branch but forgetting to switch to it (git branch name only creates; git switch -c name creates and switches).
  • Switching branches with uncommitted changes and getting them mixed into the wrong branch.
  • Keeping long-lived branches for weeks; the longer a branch lives, the harder it is to merge.

Frequently Asked Questions

No. A branch is just a small label pointing to a commit, so creating one is instant. Git only stores the content that actually differs.

Run git branch -m new-name while on the branch, or git branch -m old-name new-name from anywhere.

Usually main plus one short-lived branch per feature or fix you are working on. Delete branches once they are merged (next lesson).

Key Takeaways

  • A branch is a movable label pointing to a commit; HEAD points to the branch you are on.
  • git switch -c <name> creates a branch and switches to it; git switch <name> switches.
  • Switching branches updates the files in your folder to match that branch.
  • git log --oneline --graph --all shows every branch and how they connect.
  • Commit before switching, and give branches short, descriptive names.

Summary

Priya’s contact page lives on its own branch while main stays untouched. Next you will bring branches back together with git merge — including the moment every developer meets eventually: a merge conflict.

Next Lesson →

Merging Branches and Resolving Conflicts