Git Branching Explained: git branch, git switch, merge and delete, with Real Output for Fast-Forward, Merge Commits and the -d vs -D Error
TL;DR
- A branch is a pointer, not a copy. In my repository
.git/refs/heads/mainwas a 41-byte file: 40 hex characters of a commit ID and a newline. Creating a branch writes one such file, which is why it takes no noticeable time and no extra disk. git switch -c <name>creates a branch and moves to it.git branch <name>creates it and stays where you are.git switch <name>only movesHEAD.git merge <branch>brings that branch into the one you are on. If your branch has not moved since the other one split off, Git just slides the pointer forward (a fast-forward, no new commit). If both moved, you get a merge commit with two parents.git branch -drefuses to delete work that is not merged (error: the branch 'unmerged' is not fully merged).-Dforces it. Use-dby default; it is the safety net.- Dirty files can block a switch.
git switch otheraborted withYour local changes to the following files would be overwritten by checkoutwhen the same file differed between the two branches. - Not your exact case? If you want to clean up branches that are already merged, read how to delete all merged Git branches; if you want the whole course in order, start at the Git and GitHub blueprint course companion.
Table of Content
- What a branch really is
- Create, list and switch branches
- Merge: three-way and fast-forward
- Delete and rename branches
- What broke when I ran this
- Command cheat sheet
- Habits I recommend
This is the written version of my Day 3 DevOps course video. Every command below I ran on 2026-12-01 with Git 2.43.0 in a scratch repository, and the output is pasted as Git printed it. The reference pages are git branch, git switch and git merge; the Pro Git chapter Branches in a Nutshell explains the model behind them.
What a branch really is
Pro Git puts it in one sentence: a branch in Git is "simply a lightweight movable pointer" to one of the commits. Every commit stores a pointer to the commit that came before it, so a chain of commits is a linked list, and a branch name is a label stuck on the newest link. When you commit, Git moves the label forward for you.
A second pointer, HEAD, records which branch you are on. I looked at both in a fresh repository with one commit:
1$ cat .git/HEAD
2ref: refs/heads/main
3$ cat .git/refs/heads/main
421d52fc88bc06e9ac236171ec4d67798bcf2e872
5$ wc -c .git/refs/heads/main
641 .git/refs/heads/main
7$ git branch --show-current
8main
That is the whole secret. HEAD says "I am on main", and the file for main holds a commit ID. The Pro Git chapter quotes the same number: creating a branch is as quick as "writing 41 bytes to a file (40 characters and a newline)". Older tools copied the whole project into a second directory to branch; Git does not, so you can branch for every small idea.
I measured it rather than trusting the claim. On 2026-12-01 I ran git branch b$i in a loop 1,000 times in a one-commit repository and git branch | wc -l afterwards:
1$ for i in $(seq 1 1000); do git branch b$i; done # timed with date +%s%N before and after
21000 branches: 1906 ms
3$ git branch | wc -l
41002
That is 1.9 ms per branch including starting a new git process every time, so the branch itself costs far less than 2 ms. Switching and deleting are just as cheap. In the same repository, 200 git switch b$i calls took 535 ms (about 2.7 ms each), and git branch -d on 999 of those branches took 2,742 ms in total, leaving three branches. Those timings are from one small scratch repository on 2026-12-01; a big working tree makes git switch slower because files have to be rewritten, but creating the branch label stays the same.
| Thing | What it is | Where it lives | What moves it |
|---|---|---|---|
| Commit | A snapshot plus a pointer to its parent | .git/objects | Never changes once made |
| Branch | A named pointer to one commit | .git/refs/heads/<name> | Each new commit on that branch, a merge, a reset |
| HEAD | A pointer to the current branch | .git/HEAD | git switch, git checkout |
| Detached HEAD | HEAD pointing straight at a commit | .git/HEAD | git switch --detach <commit> |
Because a branch is only a label, deleting a branch does not delete commits that other branches or tags still reach. That matters in the delete section below.
Create, list and switch branches
I started with two commits on main, add app and add feature 1. git branch with no arguments lists local branches, and the star marks the current one:
1$ git branch
2* main
git switch -c creates a branch and moves to it in one step. I committed one file there and listed the branches with -v, which adds the commit each one points at:
1$ git switch -c feature-login
2Switched to a new branch 'feature-login'
3$ git branch -v
4* feature-login 3fe1435 add login
5 main 5d8c251 add feature 1
feature-login has moved on to 3fe1435; main stayed where it was at 5d8c251. That is the entire meaning of "a branch is isolated": the two labels now point at different commits, and nothing I do on one changes the other until I merge.
git switch arrived in Git 2.23 to take over the branch half of the overloaded git checkout. The git switch page lists -c to create, --detach to point HEAD at a commit, and --orphan for a branch with no history. I use switch for branches and restore for files, and keep checkout for muscle-memory moments; it still works.
Three things worth knowing, all run on Git 2.43.0:
1$ git switch --detach HEAD
2HEAD is now at 21d52fc first
3$ git switch -
4Switched to branch 'main'
git switch - jumps back to the previous branch, like cd -. Detaching HEAD lets you look at or test an old commit; commits you make there belong to no branch, so create a branch with git switch -c <name> before you leave if you want to keep them.
Branch names have rules. Git rejected a name with a space:
1$ git branch "bad name"
2fatal: 'bad name' is not a valid branch name
The rules come from git check-ref-format: no space, ~, ^, :, ?, * or [, no .., no component starting with a dot or ending in .lock, and no trailing slash or dot. A / is allowed and gives you hierarchy such as feature/login or hotfix/2026-12, which many teams use as a convention.
Merge: three-way and fast-forward
Merging takes the branch you name and joins it into the branch you are on. So the order is always: switch to the receiving branch first, then git merge <other>.
The three-way merge
While feature-login was waiting, main moved on with its own commit. I printed the graph before merging, --all shows every branch:
1$ git log --graph --oneline --all
2* 3fe1435 add login
3| * 8e32be8 main: add feature 2
4|/
5* 5d8c251 add feature 1
6* 29702e3 add app
The history forked at 5d8c251. Neither branch is an ancestor of the other, so Git cannot just slide a pointer. It combines both sets of changes and records the result in a merge commit:
1$ git merge feature-login -m "merge feature-login"
2Merge made by the 'ort' strategy.
3 login.txt | 1 +
4 1 file changed, 1 insertion(+)
5 create mode 100644 login.txt
6$ git log --graph --oneline --all
7* 8594554 merge feature-login
8|\
9| * 3fe1435 add login
10* | 8e32be8 main: add feature 2
11|/
12* 5d8c251 add feature 1
13* 29702e3 add app
ort is the default merge strategy in current Git. The new commit 8594554 has two parents, which is exactly the diamond in the graph. If both branches had changed the same lines, Git would stop here and ask me to resolve a conflict; my two changes touched different files, so it finished on its own.
The fast-forward merge
The second case is simpler. I created ff-demo from main, committed once on it, and went back to main. Now main had not moved, so the other branch is simply ahead of it on the same line. I used --ff-only so Git would refuse anything else:
1$ git merge --ff-only ff-demo
2Updating 8594554..aecd7af
3Fast-forward
4 ff.txt | 1 +
5 1 file changed, 1 insertion(+)
6 create mode 100644 ff.txt
No merge commit appeared. main was moved from 8594554 to aecd7af, and the history stays a straight line. The git merge page documents the three switches: --ff (the default, fast-forward when possible), --no-ff (always create a merge commit) and --ff-only (fail if a fast-forward is not possible).
| Situation | What Git does | New commit? | History shape |
|---|---|---|---|
| Receiving branch is an ancestor of the other | Fast-forward (moves the pointer) | No | Straight line |
| Both branches have new commits | Three-way merge with the ort strategy | Yes, with two parents | Diamond |
| Same lines changed on both | Stops with a conflict to resolve | Only after you resolve it | Diamond |
--no-ff on a fast-forwardable merge | Forces a merge commit | Yes | Diamond |
My rule of thumb: for a one-person feature, a fast-forward keeps the log tidy; for a team branch where you want the record that "this group of commits was one feature", use --no-ff. If you later want to tidy a branch before merging it, git rebase and squash are the tools, and my post on git rebase --exec shows one way to test every commit while you do it.
Delete and rename branches
After the merge, feature-login was only a label on a commit that main now contains. Deleting the label is safe, and Git said which commit it had pointed at:
1$ git branch -d feature-login
2Deleted branch feature-login (was 3fe1435).
The commit 3fe1435 is still in the history of main; only the pointer is gone. To see which branches are already merged into the current one, use --merged:
1$ git branch --merged
2 ff-demo
3 hotfix
4* main
hotfix appears there because I had created it straight from main and never committed, so its pointer equals main. The git branch page describes --merged as listing branches whose tip commits are reachable from the named commit, and --no-merged as the opposite.
-d versus -D
The safe delete refuses a branch whose commits no other branch contains. I made unmerged with one commit and tried:
1$ git branch -d unmerged
2error: the branch 'unmerged' is not fully merged.
3If you are sure you want to delete it, run 'git branch -D unmerged'
4$ git branch -D unmerged
5Deleted branch unmerged (was 4ce5e2d).
-D is a shortcut for --delete --force. Look at the last line: Git prints 4ce5e2d, the commit the branch pointed to. That ID is your way back. If you deleted by mistake, git branch unmerged 4ce5e2d recreates the branch, and git reflog will list it for a while even if you did not copy it. My post on git reset, soft, mixed and hard shows the same reflog recovery for a reset.
Renaming
git branch -m old new renames a local branch and prints nothing on success:
1$ git branch -m hotfix hotfix-1
2$ git branch
3 ff-demo
4 hotfix-1
5* main
A rename only changes your local label. If the old name already exists on a remote, you also have to push the new name and delete the old one there; that is covered in the remote-branches chapters of the course.
What broke when I ran this
Every item below printed a real error in this session, so you can recognise it when it happens to you.
1. Switching branches with a dirty file. I had edited app.txt on main without committing, while the branch other contained a different committed change to the same file. Git refused to switch:
1$ git switch other
2error: Your local changes to the following files would be overwritten by checkout:
3 app.txt
4Please commit your changes or stash them before you switch branches.
5Aborting
The fix is to commit, stash (git stash) or discard the edit, then switch. The git switch documentation describes the escape hatches: --discard-changes throws the edits away, --merge tries a three-way merge of them into the other branch. I prefer committing a work-in-progress commit; it is reversible. Note that if the file had not differed between the two branches, Git would have carried my edit across silently, which surprises people the other way round.
2. A branch name that already exists. Both git branch feature-x and git switch -c feature-x fail the same way when the name is taken:
1$ git branch feature-x
2fatal: a branch named 'feature-x' already exists
Use git switch feature-x to go to it, or pick a different name. git branch -f would reset the existing branch to a new start point, which moves the label and is a way to lose track of commits, so I avoid it.
3. A misspelled branch. Git does not guess:
1$ git switch nonexistent
2fatal: invalid reference: nonexistent
On a freshly cloned repository the branch may exist only on the remote. Run git fetch and then git switch <name>; Git creates the local branch tracking the remote one when exactly one remote has that name.
4. The not fully merged error. Shown above: it is a feature, not a bug. It saved me from deleting a commit that no other branch had. Read the commit ID Git prints before you reach for -D.
Command cheat sheet
| Goal | Command | Notes |
|---|---|---|
| List local branches | git branch | -v adds the tip commit, -a adds remote ones |
| Show current branch | git branch --show-current | Prints nothing when HEAD is detached |
| Create and move | git switch -c <name> | Starts from the current commit |
| Create from a commit | git switch -c <name> <commit> | Any commit, tag or branch as the start point |
| Create, stay here | git branch <name> | Does not move HEAD |
| Move to a branch | git switch <name> | Fails if local edits would be overwritten |
| Go back | git switch - | Previous branch |
| Merge into current | git merge <name> | Fast-forward when possible |
| Merge, fail unless fast-forward | git merge --ff-only <name> | Safe default for update scripts |
| Delete merged branch | git branch -d <name> | Refuses if not fully merged |
| Delete anyway | git branch -D <name> | Prints the old commit ID |
| Rename | git branch -m <old> <new> | Local only |
| See merged branches | git branch --merged | --no-merged for the rest |
Habits I recommend
- Branch for every change, however small. Because it costs 41 bytes, there is no reason to commit directly to
main. Name branches after the work (feature/login,fix/null-pointer) sogit branchreads like a to-do list. - Commit or stash before you switch. It removes the dirty-file error entirely and keeps each branch's work with the branch.
- Merge
maininto your branch (or rebase) often so the final merge is small. Long-lived branches are where conflicts pile up. - Delete branches after the merge. Stale branches make
git branchuseless. My post on deleting merged branches has the one-liners, including for BitBucket and remotes. - Do not force-delete without reading the commit ID.
-Dprints it for a reason. - Pushing is a separate step. Everything here was local. When you push a new branch to GitHub, authentication errors are the usual blocker; see GitHub personal access tokens.
The next chapter in the course covers merge conflicts and pull requests. For the full list of chapters with timestamps and the commands for each one, the Git and GitHub blueprint course companion is the index. I also uploaded the Day 3 video above, which walks through the same commands in a terminal in about the same order as this post. The whole course runs for 3.5 hours across 17 chapters, so Day 3 is a short, focused slice of it.
Did this save you a debugging session? Stay in the loop -
- Subscribe on YouTube - every post here has a screen-recorded companion video.
- Code on GitHub - the repositories, Terraform and scripts behind these posts.