By admin , 28 April 2026

Introduction

Branches are how Git supports parallel lines of work. Creating a branch is essentially free: it is just a 41-byte file containing a commit hash. Use them generously for features, experiments, and bug fixes.

Modern commands

Since Git 2.23 there are two purpose-built commands: git switch for branches and git restore for files. Use them; they are clearer than git checkout's overloaded behaviour.

git switch main                    # switch to existing branch
git switch -c feature/login        # create and switch
git switch -c hotfix v1.2.3        # create from a tag
git switch -                       # previous branch (like cd -)

Listing branches

git branch                    # local
git branch -r                 # remote-tracking
git branch -a                 # all
git branch -vv                # with upstream and ahead/behind
git branch --merged           # merged into current
git branch --no-merged        # not yet merged

Renaming and deleting

git branch -m old new             # rename
git branch -m new                  # rename current branch
git branch -d feature/login        # delete merged branch
git branch -D feature/abandoned    # force-delete unmerged

-d refuses if the branch has unmerged commits, protecting work.

Tracking remote branches

git switch feature/login           # auto-creates local from origin/feature/login
git switch -c mine origin/feature/login   # explicit
git branch -u origin/main          # set upstream of current branch

The classic syntax

Older tutorials use git checkout. It still works:

git checkout main
git checkout -b feature/login
git checkout v1.2.3

The downside is the same command also restores files, which causes confusion. Prefer switch and restore in new scripts.

Branch naming conventions

Consistent names make tooling easier. Common patterns:

  • feature/<ticket>-short-desc
  • bugfix/<ticket>-short-desc
  • hotfix/<version>
  • release/<version>
git switch -c feature/PROJ-123-add-search
git config --global branch.autoSetupMerge always
git config --global branch.autoSetupRebase always

The autoSetup options ensure new branches automatically track their starting point, which makes git status's ahead/behind line meaningful from day one.

Renaming on the remote

Renaming a remote branch is two operations: rename locally and create the new ref on the server, then delete the old one:

git branch -m old new
git push -u origin new
git push origin --delete old

Common mistakes

Switching branches with uncommitted changes that conflict with the target; Git refuses, and the fix is to commit, stash, or use git switch --discard-changes if you really do not want them. Creating dozens of long-lived branches and never deleting merged ones; git branch --merged | xargs git branch -d tidies up. Naming branches with characters that confuse shells (spaces, glob characters); stick to letters, digits, slash, and dash. Finally, deleting a branch with -D and panicking when you realize it had unmerged work; the reflog still has it for 90 days: git reflog, find the SHA, git switch -c rescued <sha>.