Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Git Branch Management: List, Create, Rename, Copy, Move, and Delete Branches

Manage local branch references deliberately: inspect branch tips, create and move names, rename or copy refs, and delete branches with Git's safety checks.

Levels: Intermediate ~45 min read

Intermediate

A branch is a managed reference, not a second history

V431 established the model: a local branch name is a ref under refs/heads/ that identifies a branch tip commit. This lesson owns what you do with those local branch names. The operations here change references and their names; they do not manufacture duplicate commit histories. When a command can change a branch tip, verify the old and new object IDs explicitly.

bash
git branch
git branch --show-current
git branch -v
git branch --list 'topic/*'

Quick check

What does plain `git branch` do when no branch name or creation option is supplied?

The repository has local branches.The branch names and current-branch marker can be inspected without changing branch tips.

Inspect before changing anything

Use `git branch` to list local branches, `git branch --show-current` to print the current branch name, and `git branch -v` to include each local branch's abbreviated tip object name and subject. A pattern should be paired with `--list`; otherwise a non-option argument can be interpreted as a branch name to create. These commands are observational: they do not move a branch tip.

bash
git branch --list
git branch --show-current
git branch -v
git branch --list 'topic/*'
git branch --contains HEAD
git branch --merged HEAD

Quick check

Why is `git branch --list 'topic/*'` safer than treating `topic/*` as a bare argument?

The learner wants to inspect branches matching a pattern.The pattern is treated as a listing filter rather than as a requested new branch name.

Create a branch without switching

`git branch <name>` creates a new branch head at the current HEAD and leaves the working tree on the current branch. Supplying a start point instead places the new branch at the resolved start point. The creation operation changes the reference namespace, not the commit graph.

bash
git branch topic
git branch topic-from-main main
git branch --show-current
git rev-parse --verify topic^{commit}
git rev-parse --verify topic-from-main^{commit}

Quick check

After `git branch topic` while `main` is current, which state is expected?

main is current and points to commit C.topic also points to C, while the current branch is still main.

Move a branch tip deliberately

`git branch -f <name> <start-point>` forcibly resets an existing branch head to the specified start point. This is a reference movement: the named branch changes its tip, while the commits themselves remain objects in the object database. Git refuses forcing the currently checked-out branch with `git branch -f`; current-branch movement belongs to other state-transition commands.

bash
git branch -f topic main~1
git rev-parse --verify topic^{commit}
git rev-parse --verify main^{commit}
git branch -f main main~1

Quick check

What does `git branch -f topic main~1` change?

topic points at a newer commit than main~1.topic resolves to main~1, while other refs are unchanged.

Rename and copy branch names

`git branch -m old new` renames a branch. `-M` forces the rename when the destination exists. `git branch -c old new` copies a branch, while `-C` forces the copy. The official documentation says a copy carries the branch configuration and reflog along with the branch; a rename also handles the corresponding reflog. These operations manage branch identity rather than copying commit objects.

bash
git branch -m topic topic-renamed
git branch -c topic-renamed topic-copy
git branch -v
git rev-parse --verify topic-renamed^{commit}
git rev-parse --verify topic-copy^{commit}

Quick check

What is the key difference between `git branch -m old new` and `git branch -c old new`?

topic names a branch tip.Rename changes the branch name; copy preserves the old name and creates another managed branch name.

Delete with Git's safety boundary

`git branch -d <name>` deletes a local branch only when Git's safety rule allows it; in the normal case the branch must be fully merged in its upstream branch, or in HEAD when no upstream is configured. `git branch -D <name>` is the forced form. Deleting a branch removes the name; it does not mean the referenced commit object is instantly erased.

bash
git branch -d topic-copy
git branch -D unmerged-topic
git branch -v

Quick check

Why can `git branch -d unmerged-topic` refuse to delete a branch?

unmerged-topic has commits not safely merged into the relevant base.normal deletion refuses rather than silently discarding the branch name.

Branch management is not branch switching

Creating, renaming, copying, moving, listing, and deleting a branch name are not the same operation as changing which branch is checked out. `git branch` can create or manage a branch without switching to it. The next lessons own `git switch`, HEAD, and detached HEAD separately so the learner first understands the branch reference and then the mechanism that makes one branch current.

Quick check

Which command family is the subject of the next switching lesson rather than this branch-management lesson?

The learner understands local branch references and their lifecycle operations.Switching and HEAD can be studied without mixing their ownership into branch-management mechanics.

Practice what you learned

Use the dedicated Git code-completion exercise for this lesson.

Official-source boundary

This lesson uses only official Git documentation: `git-branch`, with supporting reference semantics from the official Git documentation. The current official `git-branch` manual is Git 2.56.0. The local verification executable is Git 2.47.3; tests certify behavior actually observed on that executable and do not silently generalize version-sensitive behavior.

Was this page helpful?

Mark this learning resource complete to track your learning progress.