Seeing what branches exist
git branchOutput:
* main
feature-login
bugfix-headerThe asterisk (*) tells you which branch you're currently on. Any commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. you make right now goes onto main, not onto feature-login or bugfix-header.
Getting more information
# List all branches including remote ones (on GitHub, GitLab, etc.)
git branch -a
# List branches with last commit info
git branch -v
# List branches that are merged (safe to delete)
git branch --merged
# List branches that are NOT merged (don't delete these!)
git branch --no-mergedThe -v flag is particularly useful:
* main a1b2c3d Add user authentication
feature-login e4f5g6h WIP: login form styling
bugfix-header i7j8k9l Fix header alignment on mobile| Flag | What it shows | Best for |
|---|---|---|
git branch | Local branch names | Quick overview |
git branch -a | Local + remote branches | Full picture |
git branch -v | Names + last commit | Activity check |
git branch --merged | Already merged branches | Cleanup |
git branch --no-merged | Unmerged branches | Finding active work |
git branch | grep feature to filter for only feature branches, or git branch --sort=-committerdate to see the most recently active branches first.Creating a branch
git branch feature-navbarRun git branch again:
* main
feature-login
feature-navbar ← your new branch!Notice the asterisk is still on main. Creating a branch doesn't switch you to it, any commits still go to main, not feature-navbar.
new-branch, test, or fix. Always use descriptive names like feature/search-autocomplete instead of just search.Creating from a specific point
You can branch from a specific commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. or another branch:
# Create branch from a specific commit hash
git branch hotfix/critical-bug a1b2c3d
# Create branch from another branch (without switching to it first)
git branch feature-v2 feature-v1This is useful when a bug is reported in an older release, you can branch from that commit, fix the bug, and merge back without touching newer features.
Naming your branches
Good naming conventions tell you what the branch contains and why it exists.
Prefixes
Professional teams use prefixes to categorize branches:
| Prefix | When to use it | Example |
|---|---|---|
feature/ | New functionality | feature/user-profile-page |
bugfix/ | Non-urgent bug fixes | bugfix/login-error-message |
hotfix/ | Critical production fixes | hotfix/security-vulnerability |
docs/ | Documentation changes | docs/api-reference-update |
refactor/ | Code restructuring | refactor/extract-auth-module |
test/ | Adding or fixing tests | test/user-service-unit-tests |
chore/ | Maintenance tasks | chore/update-dependencies |
Prefixes can also trigger automation, many CI/CDWhat is ci/cd?Continuous Integration and Continuous Deployment - automated pipelines that test your code on every push and deploy it when tests pass. systems use them to run different test suites or deployment pipelines.
What makes a good branch name?
bad: login-fix
good: bugfix/login-form-validation-error-502The good name tells you: it's a bugfix, about the login form, specifically validation, fixing error 502. Six months from now you'll know exactly what it was about.
Branch naming rules
Be specific but concise. feature/dark-mode over feature/add-dark-mode-toggle-to-settings-page.
Use hyphens, not spaces. feature/user-auth not feature/user auth.
Include issue numbers. bugfix/fix-login-redirect-#123, GitHub will auto-link the PR to the issue.
Avoid personal names. Use feature/payment-integration, not johns-feature.
Stick to safe characters. Lowercase letters, numbers, hyphens, and forward slashes only.
Renaming branches
Rename the branch you're currently on
git branch -m feature/loginThe -m flag stands for "move" (renaming is moving a reference from one name to another).
Rename any branch from anywhere
git branch -m old-branch-name new-branch-name> git push origin new-branch-name
> git push origin --delete old-branch-name
>Common mistakes
Creating from the wrong starting point. Make sure you're on the right branch before creating a new one. Branch from main? First run git checkout main.
Forgetting you're not automatically switched. The #1 confusion: you run git branch my-feature, start coding, and commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. to main instead. Remember: create, then switch.
Using vague names. temp, test, stuff, these mean nothing. Be descriptive.
Branching with uncommitted changes. Commit or stash your work first, then branch.
Quick reference
| Command | What it does | When to use it |
|---|---|---|
git branch | List local branches | See what exists |
git branch -a | List all branches | Include remote |
git branch -v | List with last commit | Check activity |
git branch name | Create a branch | Start new work |
git branch -m old new | Rename a branch | Fix typos |
git branch -d name | Delete merged branch | Cleanup |
git branch --merged | Show merged branches | Find safe deletions |
# See all branches
git branch
# Create a new feature branch
git branch feature-navbar
# Create a bugfix branch
git branch bugfix-typo
# List with more details
git branch -v