git branch
Create, list, rename, and delete branches to organize parallel lines of development in your repository.
You're about to start working on a new feature, and you don't want to clutter up
the main branch with half-finished code. Or maybe you need to see which branches
exist in your repository, or clean up old branches after merging pull requests.
git branch is the command for managing branches—creating them, listing them,
renaming them, and deleting them when you're done.
Running git branch with no arguments lists all your local branches, with an
asterisk marking the one you're currently on. This quick overview helps you see
what branches exist and where you are in your repository. The output might show
main, develop, and a few feature branches, giving you a sense of what work is
happening in the repo.
To create a new branch, you use git branch feature-name, which creates the
branch but doesn't switch to it. The new branch starts at your current commit,
pointing to the same place in history as whatever branch you were on when you
created it. Most developers skip this step and use
git checkout -b feature-name or git switch -c feature-name instead, which
creates the branch and switches to it in one command.
Managing and Cleaning Up Branches
Deleting branches is common after merging pull requests. Running
git branch -d feature-login deletes the branch, but only if it's been fully
merged into your current branch. This safety check prevents you from
accidentally losing work. If you're certain you want to delete an unmerged
branch, use -D (capital D) to force deletion. This is useful for abandoning
experimental branches or cleaning up work you decided not to pursue.
Remote branches require a different approach. After merging a PR and deleting
the branch on GitHub, your local repository still has a reference to the remote
branch. Running git fetch --prune removes these stale references. You can also
delete a remote branch directly with git push origin --delete branch-name,
though most teams handle this through GitHub's PR interface.
Listing and Filtering Branches
The -a flag shows all branches, including remote ones: git branch -a. This
gives you a complete view of every branch in your repository and on the remote.
You'll see local branches listed normally, and remote branches prefixed with
remotes/origin/. This is helpful when you need to check out a teammate's
branch or see what feature branches exist on the remote but not locally.
To see which branches have been merged into your current branch, use
git branch --merged. This shows branches that are safe to delete because their
changes are already incorporated. Conversely, git branch --no-merged shows
branches with unmerged work, highlighting where active development is happening
or where you might have forgotten to merge something.
Renaming and Organizing
If you misnamed a branch or want to standardize naming conventions,
git branch -m old-name new-name renames it. If you're currently on the branch
you want to rename, you can just use git branch -m new-name. After renaming a
branch you've already pushed, you'll need to push the new name and delete the
old one from the remote with git push origin new-name and
git push origin --delete old-name.
Some teams use branch naming conventions like feature/add-login,
bugfix/fix-auth-redirect, or release/v1.2.0. These prefixes help organize
branches and make it clear what type of work each branch contains. While Git
doesn't enforce this, many teams build tooling or CI pipelines that expect
certain naming patterns.
Pull Request Workflows
In pull request workflows, branches are how you isolate work. Each PR lives on its own branch, keeping features independent until they're ready to merge. You create a branch for your feature, make commits, push to the remote, and open a PR. The branch exists until the PR is merged, then you delete it to keep the repository clean.
When reviewers want to test your code locally, they use your branch name to check it out. After you merge a PR, the branch usually gets deleted both on GitHub and locally. Many teams configure automatic branch deletion after PR merges to prevent accumulation of dead branches.
Checking out remote branches requires creating a local tracking branch. If
someone pushes a branch called experiment-with-caching and you want to work on
it, you run git checkout experiment-with-caching (after fetching). Git
automatically creates a local branch that tracks the remote one, so your pushes
and pulls go to the right place.
Understanding branches means understanding that they're lightweight pointers to commits, not copies of your entire codebase. Creating a branch is instant and cheap, which is why developers create branches freely for every feature, bugfix, or experiment. Branches let you work on multiple things in parallel without interference, making them fundamental to Git's flexibility and power.
