Branches & git switch

Git · Lesson 10 of 28 8 min read Updated

A branch is an independent line of work. While the main branch keeps the working website, you can build a new feature on another branch, commit as often as you like, and only bring it into main when it is ready. If the experiment fails, you delete the branch and main never knew it existed.

Branches are Git's most loved feature because they are so cheap: creating one takes a fraction of a second and almost no disk space. This lesson shows how they work and the everyday commands, with git switch as the modern way to move between them.

What a branch really is

Every commit points back to its parent, forming a chain. A branch is just a movable label pointing at one commit in that chain, and HEAD is a pointer to the branch you are on. When you commit, Git creates the new commit and moves the current branch label forward to it.

Text
                      main
                       |
dae5ca8 <- ... <- 3aa8447
                       |
                  diwali-offer   (new branch: same commit, new label)

Creating a branch does not copy any files. It writes a tiny file containing a commit ID, which is why it is instant even in a huge project.

Creating and switching

Priya wants to add a Diwali offers page without disturbing the live site. git branch lists branches (the * marks the current one); git branch <name> creates one; git switch <name> moves to it:

Terminal
git branch
git branch diwali-offer
git branch
git switch diwali-offer
Output
* main
  diwali-offer
* main
Switched to branch 'diwali-offer'

Read the output in order. At first only main exists. After git branch diwali-offer, the list has two branches, but the * is still on main: creating a branch does not move you to it. Only git switch does that. To check where you are at any time, ask directly:

Terminal
git branch --show-current
Output
diwali-offer

Most of the time you create and switch in one step with -c (create):

Terminal
git switch -c fix-footer
Output
Switched to a new branch 'fix-footer'

Working on a branch

On diwali-offer, Priya adds a page and links it from the menu, in two commits. The graph view of the log shows both branches. --all includes every branch, and --graph draws the lines:

Terminal
git log --oneline --graph --all -5
Output
* 8b52b9c (HEAD -> diwali-offer) Link offers from the menu
* fcd55f3 Add Diwali offers page
* 3aa8447 (main) Tweak styles
* 24d8d06 (tag: v1.1.0) Revert "Change opening hours"
* 84059e3 Change opening hours

diwali-offer is two commits ahead; main has not moved. When she switches back to main, Git changes the files in her folder to match that branch, so the offers page disappears from the folder (it is safe inside the branch):

Terminal
git switch main
ls
Output
Switched to branch 'main'
contact.html
index.html
logs
main.css
menu.html

This is the part that amazes beginners: one folder, several versions, and Git swaps the files for you when you switch.

Useful branch commands

CommandWhat it does
git branch -vLists branches with their latest commit
git switch -Switches back to the previous branch (like a TV remote's "last channel")
git switch -c hotfix v1.0.0Creates a branch starting at a tag or commit instead of the current one
git branch -m old newRenames a branch (-m new renames the current one)
git branch -d nameDeletes a branch that has been merged
git branch -D nameDeletes a branch even if its work is not merged
Terminal
git branch -v
git branch -m fix-footer footer-fix
Output
  diwali-offer 8b52b9c Link offers from the menu
* fix-footer   3aa8447 Tweak styles
  main         3aa8447 Tweak styles

Git protects unmerged work. Trying to delete diwali-offer before it is merged fails with a clear message:

Terminal
git branch -d diwali-offer
Output
error: the branch 'diwali-offer' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D diwali-offer'
hint: Disable this message with "git config set advice.forceDeleteBranch false"

Switching with uncommitted changes

If you have edited a file that is also different on the other branch, Git refuses to switch rather than overwrite your work:

Terminal
git switch diwali-offer
Output
error: Your local changes to the following files would be overwritten by checkout:
	menu.html
Please commit your changes or stash them before you switch branches.
Aborting

You have three choices: commit the change, throw it away with git restore, or put it aside with git stash. Changes to files that are the same on both branches simply come with you when you switch.

Detached HEAD

You can also switch to a tag or a commit to look at old code. Because a tag is not a branch, Git puts you in detached HEAD state: HEAD points at a commit directly instead of at a branch.

Terminal
git switch --detach v1.0.0
git branch
Output
HEAD is now at daae9bf Use a hyphen in the menu page title
* (HEAD detached at v1.0.0)
  diwali-offer
  footer-fix
  main

Looking around is completely safe. If you make commits here, though, no branch points at them, and they become hard to find once you switch away. If you decide to keep that work, create a branch on the spot with git switch -c <new-name>. To leave, just git switch main.

Naming branches

Use short, lowercase names with hyphens that say what the work is: diwali-offer, fix-login-timeout, add-gst-invoice. Many teams add a prefix such as feature/ or fix/ (feature/order-form), and some include an issue number (fix/42-price-rounding). Avoid spaces and very long names.

Common mistakes

  • Committing to the wrong branch. Check the branch in git status or your prompt before committing. (If it happens, cherry-pick or reset can move the commit.)
  • Working for days in detached HEAD. Create a branch with git switch -c as soon as you start making commits.
  • Using -D to "fix" the not-merged error. That deletes unmerged work. Make sure you really do not need it.
  • Thinking switching branches loses your files. Committed work is safe in its branch; switch back to see it again.

Key points

  • A branch is a lightweight, movable label pointing at a commit; HEAD points at the current branch.
  • git switch <name> changes branch and git switch -c <name> creates and switches in one step.
  • Switching updates the files in your folder to match the branch.
  • git branch lists, renames (-m) and deletes (-d, -D) branches; Git protects unmerged work.
  • Switching to a tag or commit gives a detached HEAD; create a branch if you want to keep new commits.

Exercise

In a practice repository, create a branch called add-contact and switch to it in one command. Add a new file and commit it. Switch back to main and confirm the file is not there. Show the graph of all branches. Finally, rename add-contact to contact-page.

Show solution
Terminal
git switch -c add-contact
git add contact.html
git commit -m "Add contact page"
git switch main
ls
git log --oneline --graph --all
git branch -m add-contact contact-page

After switching to main, ls does not list contact.html, because that file exists only in the branch's commits. The graph shows the branch one commit ahead of main.

Quick Quiz

1. What is a Git branch?

2. Which command creates a branch called search and switches to it?

3. Git says your local changes would be overwritten when you try to switch. What is a safe fix?

4. What is detached HEAD?

Learn Git with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call