Himanshu SharmaSoftware engineer
Let’s talk

Git and GitHub · Lesson 10 of 30

Branches: creating, switching and deleting them

Understand that a Git branch is just a movable label, then create, switch, rename and delete branches with git branch and git switch, and read the commit graph.

14 minBranches and mergingUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonCreate and switch branches, explain what HEAD and a branch pointer are, see branches in the log graph, and delete or rename them safely.

What you can do after this lesson

You will create separate lines of work, move between them, and tidy them up. Branches are what make Git comfortable to experiment in, so it is worth understanding what they really are. We use a fictional library website for the examples.

A branch is a label, nothing more

People imagine a branch as a copy of the project. It is not. A branch is a small named pointer to one commit. Git makes a branch by writing that commit's hash into a tiny file. That is why creating a branch is instant even in a huge project.

HEAD is a second pointer that says which branch you are currently on. When you make a commit, the new commit is attached to the commit you were on, and the branch that HEAD is on moves forward to include it. Other branches stay where they were. That single rule explains everything in this section.

Start with a repository holding two commits:

bash
git branch
text
* main

The star marks the current branch. Create another, which is only a new label on the same commit:

bash
git branch opening-hours-update
git branch -v
text
* main                 594fee7 Add opening hours
  opening-hours-update 594fee7 Add opening hours

Both labels point at 594fee7. Example hashes; yours differ.

Switching

Move to the new branch with git switch:

bash
git switch opening-hours-update
git log --oneline --decorate
text
Switched to branch 'opening-hours-update'
594fee7 (HEAD -> opening-hours-update, main) Add opening hours
e6583be Add home page

HEAD -> opening-hours-update shows that HEAD points at the branch. Now commit something:

bash
echo "Opening hours: 9 to 6." > hours.txt
git commit -am "Extend hours to 6"
git switch main
cat hours.txt
text
Opening hours: 9 to 5.

Back on main, the file shows the old text. Switching rewrites your working files to match the branch you moved to, which is why Git insists on a tidy state: if you have uncommitted changes that would be overwritten, it refuses and tells you. Commit or stash first.

(git switch arrived in Git 2.23. The older git checkout <branch> still works and does the same here.)

Create and switch in one move

The most common form is -c (create):

bash
git switch -c new-books
echo "New: Persuasion" > books.txt
git add .
git commit -m "Add new books page"
git log --oneline --graph --decorate --all
text
* bb0b6f1 (HEAD -> new-books) Add new books page
| * 788946b (opening-hours-update) Extend hours to 6
|/  
* 594fee7 (main) Add opening hours
* e6583be Add home page

The graph shows history forking at 594fee7: two branches each have one commit the other lacks. --all includes every branch, not just the current one.

Handy shortcuts: git switch - jumps back to the previous branch, and git branch --show-current prints the current branch name.

Naming branches

Branch names are free text without spaces. Teams usually choose a convention, such as feature/search, fix/login-bug or docs/update-readme. A slash is allowed and groups them. Short, descriptive and lowercase works well. The main branch (main in new repositories) holds the version your team treats as the stable line.

Renaming and deleting

bash
git branch -m opening-hours-update hours-update
git branch -d new-books
text
error: the branch 'new-books' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D new-books'

-m renames. Deleting with -d is deliberately cautious: it refuses to delete a branch whose commits are not reachable from your current branch, since the branch is your only label for that work. If you decide the work is not needed, git branch -D new-books deletes it regardless:

text
Deleted branch new-books (was bb0b6f1).

Note the hash. If you delete the wrong branch, git branch new-books bb0b6f1 restores it, and the previous lesson's reflog technique also finds it. You cannot delete the branch you are currently on; switch away first.

Detached HEAD, briefly

If you check out a commit or tag directly, for example git switch --detach v1.0.0, HEAD points at that commit rather than a branch. You can look around, but any commit you make there belongs to no branch and is easy to lose. If you want to keep such work, create a branch there with git switch -c name.

Habits that keep branches useful

Create a branch for each task, however small, because it costs nothing and keeps main stable. Give it a name that says what it is for, so the list from git branch reads like a to-do list of work in flight. Check where you are with git status before you start typing commits. And when a branch has done its job and is merged, delete it. A short list of live branches is easy to reason about, while dozens of forgotten ones hide the work that actually matters.

Common mistakes

  • Committing on the wrong branch. Run git branch or git status before you start; the output names the current branch.
  • Switching with uncommitted changes that conflict. Commit them, or stash them in lesson 22.
  • Treating a branch as a backup copy. Branches share history; they are not independent copies of the files.
  • Letting branches pile up forever. Delete merged branches to keep the list readable.
  • Using -D when -d complained. The warning was telling you the branch holds unmerged work.

Exercise

  1. In a practice repository, create a branch called about-page and switch to it.
  2. Add a file about.txt, commit it, and switch back to main. Is the file there?
  3. Draw the history with git log --oneline --graph --decorate --all.
  4. Delete the branch. What does Git say, and why?
Show one possible answer
bash
git switch -c about-page
echo "About us" > about.txt
git add about.txt && git commit -m "Add about page"
git switch main
ls
git log --oneline --graph --decorate --all
git branch -d about-page

On main, about.txt is not listed, because the commit lives only on about-page. The -d delete refuses with "not fully merged" until you merge the branch, which the next lesson covers.

Next: Merging branches: fast-forward and merge commits.

Key points

  • A branch is a lightweight label pointing at a commit; creating one copies no files.
  • HEAD says which branch you are on; a new commit moves that branch's label forward.
  • git switch -c <name> creates and switches in one step; git switch - returns to the previous branch.
  • git branch -d deletes a merged branch and refuses to delete unmerged work; -D forces it.
  • git log --oneline --graph --decorate --all draws every branch so you can see how they diverge.

Check yourself

1. What is a Git branch?
2. Which command creates a branch called new-books and switches to it?
3. git branch -d feature refuses with 'not fully merged'. What does that mean?
4. You commit while on branch ideas. Which pointer moves forward?