Git Branches: Creating, Switching, Listing, Renaming and Deleting
What a branch really is, and every everyday branch task: creating and switching with git switch, start points, what happens to uncommitted changes, detached HEAD, listing, filtering and sorting branches, renaming, deleting safely, moving a branch, orphan branches, and naming rules.
kvi5hnuCode Sprout team member10 min readOn this page
Branches let you work on several things at once: a feature on one branch, an urgent fix on another, while main stays stable. In Git, branches are cheap and fast, so it's normal to create one for every piece of work, even a tiny one, and delete it once it's merged.
What a branch is
A branch is just a name that points to a commit. Creating a branch doesn't copy any files: Git writes a small record saying "lake points to commit C". When you commit on a branch, the new commit's parent is the commit the branch pointed to, and the branch moves forward to the new commit. Other branches don't move.
HEAD says which branch you're on. Normally it doesn't point to a commit directly but to a branch, such as main, and the branch points to a commit. Switching branches changes what HEAD names, and updates your files to match that branch's commit.

You can see this inside the .git folder:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md
git add . && git commit -q -m "Add README"
git branch lake # create a branch: no files are copied
echo "--- both branches point to the same commit"
git rev-parse main lake
echo "--- HEAD names a branch, not a commit"
cat .git/HEAD
git symbolic-ref HEAD
git switch -q lake
echo "Lake path: 5 km" > lake.txt
git add . && git commit -q -m "Add lake path"
echo "--- after committing on lake, only lake moved"
git log --oneline --all --format='%h %s%d' --decorate
--- both branches point to the same commit
61da1f1bc4c3d2495583c93941ff462706185b77
61da1f1bc4c3d2495583c93941ff462706185b77
--- HEAD names a branch, not a commit
ref: refs/heads/main
refs/heads/main
--- after committing on lake, only lake moved
4d8783b Add lake path (HEAD -> lake)
61da1f1 Add README (main)
The decorations at the end show HEAD -> lake on the new commit and main on the old one. git rev-parse turns a name into the commit ID it points to. The way Git stores these names, files under .git/refs/heads/ or the newer reftable format, is covered in article 49, and you never need to edit them by hand.
Creating and switching
| Task | Command |
|---|---|
| Create a branch, stay where you are | git branch lake |
| Switch to an existing branch | git switch lake |
| Create a branch and switch to it | git switch -c lake |
| Create it from another starting point | git switch -c hotfix v1.2 or git branch hotfix a1b2c3d |
| Switch back to the previous branch | git switch - |
| Show the current branch | git branch --show-current |
The start point can be any commit: a branch name, a tag, a commit ID, HEAD~3, or a remote-tracking branch such as origin/main. Without one, the new branch starts at the current commit.
git switch arrived in Git 2.23, split off from the older, overloaded git checkout, which also restores files. The old forms still work everywhere, and you'll see them in older guides and scripts:
git switch | git checkout equivalent |
|---|---|
git switch lake | git checkout lake |
git switch -c lake | git checkout -b lake |
git switch -c lake origin/lake | git checkout -b lake origin/lake |
git switch - | git checkout - |
git switch --detach v1.0 | git checkout v1.0 |
git switch lake, when no local lake exists but one remote has a branch lake, creates a local lake tracking it. Article 17 covers this and remote-tracking branches.
What happens to uncommitted changes
Switching updates the working tree to the other branch's files. Your uncommitted changes, staged or not, come with you if the other branch has the same version of those files. If switching would overwrite a change, Git refuses:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md
echo "Ridge loop: 8 km" > trails.txt
git add . && git commit -q -m "Start project"
git switch -q -c lake
echo "Lake path: 5 km" >> trails.txt
git commit -q -am "Add lake path"
git switch -q main
echo "=== an edit to README.md, which is the same on both branches"
echo "Maps included." >> README.md
git switch lake
git status -s
git switch -q main
echo "=== an edit to trails.txt, which differs between the branches"
echo "Forest walk: 3 km" >> trails.txt
git switch lake
echo "exit status: $?"
=== an edit to README.md, which is the same on both branches
M README.md
Switched to branch 'lake'
M README.md
=== an edit to trails.txt, which differs between the branches
error: Your local changes to the following files would be overwritten by checkout:
trails.txt
Please commit your changes or stash them before you switch branches.
Aborting
exit status: 1
Git never silently loses work when switching. When it refuses, you have these choices:
- Commit the changes on the current branch first.
- Stash them:
git stash, switch, thengit stash popwhere you want them (article 27). - Carry them over with a merge:
git switch -m lakemerges your changes into the other branch's version, possibly leaving conflicts to resolve. - Throw them away:
git switch --discard-changes lake, orgit checkout -f lake. This can't be undone.
Untracked files are left alone when switching, unless the other branch has a tracked file with the same name, in which case Git refuses too.
Detached HEAD
You can switch to a commit that isn't a branch, such as a tag or an old commit, to look around or test it. HEAD then points directly to the commit instead of to a branch. This is called detached HEAD:
git init -q trail-guide && cd trail-guide
echo "v1" > README.md; git add .; git commit -q -m "First version"
git tag v1.0
echo "v2" > README.md; git commit -q -am "Second version"
git switch --detach v1.0
echo "--- HEAD now holds a commit ID"
cat .git/HEAD
git status | head -1
echo "experiment" > idea.txt; git add .; git commit -q -m "Try an idea"
echo "--- keep the commit by giving it a branch"
git switch -c idea
git log --oneline --all --format='%h %s%d' --decorate
HEAD is now at 43752bf First version
--- HEAD now holds a commit ID
43752bf34c45c4c18c71c5363d2dbf4315fe14de
HEAD detached at v1.0
--- keep the commit by giving it a branch
Switched to a new branch 'idea'
c9d5848 Try an idea (HEAD -> idea)
72dd073 Second version (main)
43752bf First version (tag: v1.0)
Detached HEAD is safe for looking around and testing. Commits made there belong to no branch: when you switch away, nothing points to them, and Git warns you. They can still be found with git reflog for a while (article 26), but the simplest habit is to run git switch -c <name> as soon as you want to keep something.
You'll also find yourself in detached HEAD during a rebase or bisect, or in a submodule (article 41), where it's expected.
Listing branches
git init -q trail-guide && cd trail-guide
at() { export GIT_AUTHOR_DATE="$1" GIT_COMMITTER_DATE="$1"; }
at 2026-02-01T09:00; echo "# Trail guide" > README.md; git add .; git commit -q -m "Add README"
at 2026-02-02T09:00; git switch -q -c fix/typo; sed -i 's/guide/Guide/' README.md; git commit -q -am "Fix capital letter"
at 2026-02-03T09:00; git switch -q main; git merge -q fix/typo
at 2026-02-04T09:00; git switch -q -c feature/lake; echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
at 2026-02-05T09:00; git switch -q -c feature/ridge main; echo "Ridge" > ridge.txt; git add .; git commit -q -m "Add ridge page"
git switch -q main
echo "=== git branch"
git branch
echo "=== -v: each branch's last commit"
git branch -v
echo "=== merged into the current branch, and not merged"
git branch --merged
echo "--"
git branch --no-merged
echo "=== matching a pattern"
git branch --list 'feature/*'
echo "=== most recently committed first"
git branch --sort=-committerdate --format='%(committerdate:short) %(refname:short)'
=== git branch
feature/lake
feature/ridge
fix/typo
* main
=== -v: each branch's last commit
feature/lake a7f7161 Add lake page
feature/ridge 6660126 Add ridge page
fix/typo 4e6d4a5 Fix capital letter
* main 4e6d4a5 Fix capital letter
=== merged into the current branch, and not merged
fix/typo
* main
--
feature/lake
feature/ridge
=== matching a pattern
feature/lake
feature/ridge
=== most recently committed first
2026-02-05 feature/ridge
2026-02-04 feature/lake
2026-02-02 fix/typo
2026-02-02 main
| Option | Lists |
|---|---|
| (none) | local branches, with * on the current one |
-v, -vv | adds each branch's last commit; -vv also shows its upstream and how far ahead or behind it is |
-r, -a | remote-tracking branches only / local and remote-tracking (article 17) |
--merged [commit] | branches whose tip is already included in the current branch (or the given commit): safe to delete |
--no-merged [commit] | branches with commits the current branch doesn't have |
--contains <commit> | branches that include a commit, such as "which branches have this fix?" |
--list 'pattern' | branches matching a pattern. git branch 'feature/*' without --list would try to create a branch |
--sort=-committerdate | sorted by the last commit's date, newest first. Set branch.sort to make it the default |
--format=... | custom output, using the same fields as git for-each-ref |
fix/typo is listed as merged because main contains its commit. It's ready for deleting. In scripts, prefer git for-each-ref refs/heads --format='%(refname:short)' to parsing git branch output, which is meant for people.
Renaming
git branch -m new-name # rename the current branch
git branch -m old-name new-name # rename another branch
git branch -M old-name new-name # rename even if new-name exists (replacing it)
Renaming keeps the branch's reflog. If the branch was pushed, the old name still exists on the remote: push the new name and delete the old one there (article 20).
Deleting
git branch -d fix/typo # delete a merged branch
git branch -D experiment # delete it even if it has unmerged commits
-d only deletes a branch whose commits are merged into its upstream branch, or into the current branch if it has no upstream. Otherwise it refuses, so -d never loses work. -D forces it:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Add README"
git switch -q -c experiment
echo "idea" > idea.txt; git add .; git commit -q -m "Try an idea"
git switch -q main
git branch -d experiment
echo "--- forcing"
git branch -D experiment
error: the branch 'experiment' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D experiment'
hint: Disable this message with "git config set advice.forceDeleteBranch false"
--- forcing
Deleted branch experiment (was ce37c8a).
-D printed the deleted branch's last commit ID, (was ...). Its commits still exist for a while, so a branch deleted by mistake can be recreated with git branch experiment <that-id>, or found in git reflog (article 26).
You can't delete the branch you're on. Switch to another branch first. Deleting a local branch doesn't touch the remote: git push origin --delete <branch> does that (article 20). Git 2.56 added git branch --delete-merged <pattern> for clearing out branches that are merged into their upstream. Article 23 covers branch cleanup.
Moving a branch
Since a branch is a pointer, you can move it to any commit:
git branch -f lake <commit>moves another branch, without switching. Git refuses to move the current branch this way.git reset --hard <commit>moves the current branch and resets your files to match, discarding uncommitted changes (article 24).
A common use is "I committed on main, but it should have been a new branch":
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Add README"
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Boats" >> lake.txt; git commit -q -am "Mention boat hire"
git branch lake # a new branch at the current commit keeps the work
git reset -q --hard HEAD~2 # move main back two commits
git switch -q lake
git log --oneline --graph --all --format='%h %s%d' --decorate
* 4b9a13e Mention boat hire (HEAD -> lake)
* c8e6534 Add lake page
* 61da1f1 Add README (main)
Only do this before pushing main. If others already have those commits, rewriting main causes problems for them (article 20).
Orphan branches
An orphan branch starts with no history: its first commit has no parent. It's occasionally useful for unrelated content kept in the same repository, such as a gh-pages branch holding a generated website:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Add README"
git switch -q --orphan pages
echo "--- the working tree is empty, and git branch doesn't list pages yet"
ls
git branch
echo "<h1>Trail guide</h1>" > index.html
git add . && git commit -q -m "Start website"
echo "--- two unrelated histories"
git log --oneline --all --graph --format='%h %s%d' --decorate
--- the working tree is empty, and git branch doesn't list pages yet
main
--- two unrelated histories
* 61da1f1 Add README (main)
* fbfdd11 Start website (HEAD -> pages)
git switch --orphan empties the working tree. The older git checkout --orphan keeps the files staged instead, so you can commit them as a fresh start with no history. The branch only appears in git branch once it has a commit.
Naming branches
Branch names may contain letters, digits, -, _, . and /, which groups related branches: feature/lake-page, fix/map-404, ana/experiment. Git rejects names with spaces, .., ~, ^, :, ?, *, [, a backslash, or a leading -, and names ending in .lock or /. git check-ref-format --branch <name> tests a name.
Two traps:
- A branch can't be both a name and a folder: if
fixexists, you can't createfix/map-404, and the other way round. - On Windows and macOS, file names are usually case-insensitive, and so are branch names stored as files there.
Featureandfeaturecan collide. Stick to lower case.
Teams usually agree on prefixes, and often include a ticket number: feature/TRAIL-88-lake-page.
What's current
git switchis the clear way to change branches.git checkoutstill works for everything, and remains in Git 3.0.- The default first branch is set by
init.defaultBranch, and Git 3.0 changes the built-in default tomain. - Git 2.56 added
git branch --delete-mergedfor removing branches merged into their upstream, and--forked <branch>for listing branches whose upstream matches (article 23).
Common questions
How many branches can I have? Do they slow Git down? As many as you like. A branch is a tiny pointer, and creating or switching branches is instant for most projects. Still, delete merged branches, so lists stay readable.
What's the difference between git switch and git checkout?
git checkout both switches branches and restores files, which made it confusing. git switch only switches, and git restore only restores. They do the same work underneath.
I switched branches and my changes came with me. Is that a bug? No. Uncommitted changes aren't on any branch: they live in your working tree and staging area. Git keeps them when the switch doesn't touch those files. Commit or stash them if they belong to the first branch.
Why does git branch -d say "not fully merged" when the branch was merged on GitHub?
If the pull request was squash-merged or rebase-merged, the branch's own commits aren't in main: new commits with the same changes are. Git can't see that, so it refuses. Check that the change is in main, then use -D.
Can I recover a branch I deleted?
Usually, yes. Find its last commit in the -D message or with git reflog, then run git branch <name> <commit> (article 26).
Summary
- A branch is a movable pointer to a commit, and
HEADnames the current branch. Committing moves the current branch forward. git switch -c name [start]creates and switches.git switch nameandgit switch -change branches.- Uncommitted changes come along when they don't conflict. Otherwise Git refuses: commit, stash,
-mor--discard-changes. - Detached HEAD means
HEADpoints straight to a commit. Create a branch to keep commits made there. git branch -v,--merged,--no-merged,--containsand--sortlist and filter branches.git branch -mrenames,-ddeletes merged branches,-Dforces, andgit branch -fmoves another branch.git switch --orphanstarts an unrelated history.
Practice questions
You're on main. What's the difference between git branch hotfix and git switch -c hotfix?
Answer
Both create hotfix at the current commit. git branch hotfix leaves you on main, while git switch -c hotfix also switches to the new branch, so your next commit goes on hotfix.
You run git switch v2.0 and get an error, but git switch --detach v2.0 works. Why, and what state are you in afterwards?
Answer
git switch expects a branch, and v2.0 is a tag, so it refuses unless you ask for a detached HEAD explicitly (git checkout v2.0 detaches without asking). Afterwards, HEAD points directly to the tagged commit, not to a branch. You can look around and build. Commits you make there belong to no branch until you run git switch -c <name>.
git branch --merged lists feature/lake, but git branch --no-merged lists feature/ridge. Which can you delete with -d, and what does that tell you?
Answer
feature/lake can be deleted with -d: its tip is already included in the current branch, so no commits are lost. git branch -d feature/ridge would refuse, because it has commits the current branch doesn't contain. Merge it first, or use -D if the work isn't needed.
Previous: Inspecting Commits and Files: git show, diff-tree and Files at Any Revision
Next: Merging Branches: Fast-Forward, Three-Way Merges, --no-ff and Squash Merges

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.