Git/
Lesson

Seeing what branches exist

git branch

Output:

* main
  feature-login
  bugfix-header

The 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-merged

The -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
FlagWhat it showsBest for
git branchLocal branch namesQuick overview
git branch -aLocal + remote branchesFull picture
git branch -vNames + last commitActivity check
git branch --mergedAlready merged branchesCleanup
git branch --no-mergedUnmerged branchesFinding active work
Quick tip
Use git branch | grep feature to filter for only feature branches, or git branch --sort=-committerdate to see the most recently active branches first.
02

Creating a branch

git branch feature-navbar

Run 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.

AI pitfall
AI tools often suggest branches with vague names like 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-v1

This 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.

03

Naming your branches

Good naming conventions tell you what the branch contains and why it exists.

Prefixes

Professional teams use prefixes to categorize branches:

PrefixWhen to use itExample
feature/New functionalityfeature/user-profile-page
bugfix/Non-urgent bug fixesbugfix/login-error-message
hotfix/Critical production fixeshotfix/security-vulnerability
docs/Documentation changesdocs/api-reference-update
refactor/Code restructuringrefactor/extract-auth-module
test/Adding or fixing teststest/user-service-unit-tests
chore/Maintenance taskschore/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-502

The 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.

04

Renaming branches

Rename the branch you're currently on

git branch -m feature/login

The -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
Heads up
If you've pushed the branch to a remote, renaming locally creates a disconnect. After renaming, push the new name and delete the old one:
> git push origin new-branch-name
> git push origin --delete old-branch-name
>
05

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.

06

Quick reference

CommandWhat it doesWhen to use it
git branchList local branchesSee what exists
git branch -aList all branchesInclude remote
git branch -vList with last commitCheck activity
git branch nameCreate a branchStart new work
git branch -m old newRename a branchFix typos
git branch -d nameDelete merged branchCleanup
git branch --mergedShow merged branchesFind safe deletions
javascript
# 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