How to Create and List Local and Remote Git Branches

A Git branch is a pointer to a specific commit. Branches allow you to work on new features, bug fixes, or experiments in isolation without affecting the main codebase. Once the work is complete, the branch can be merged back.
This guide explains how to create and list local and remote Git branches.
Creating a Git Branch
To create a new local branch, use the git branch command followed by the branch name:
git branch feature-loginThe command creates the branch but does not switch to it. To switch to the new branch, use git switch:
git switch feature-loginSwitched to branch 'feature-login'To create a new branch and switch to it in a single command, use git switch -c:
git switch -c feature-loginSwitched to a new branch 'feature-login'From here, you can use git add and git commit to add commits to the new branch.
git checkout -b <name> was used to create and switch to a branch. The git switch command was introduced in Git 2.23 as a clearer alternative. Both work, but git switch is the recommended approach.Creating a Branch from a Specific Commit
By default, a new branch is created from the current HEAD. To create a branch from a specific commit, pass the commit hash as the second argument:
git branch bugfix-header a1b2c3dYou can also create a branch from a tag:
git branch release-1.0 v1.0.0Pushing a Branch to Remote
A newly created branch exists only in your local repository. To push it to the remote and set up tracking, use git push with the -u flag. If you have not added a remote
yet, you will need to do that first.
git push -u origin feature-loginThe -u flag sets the upstream tracking, so future git push and git pull commands work without specifying the remote and branch name. If you want a clearer view of what git pull does under the hood, read our guide on git fetch vs git pull
.
Branch Already Exists
If a branch with the same name already exists, Git prints an error:
fatal: A branch named 'feature-login' already exists.Use git branch to list existing branches and choose a different name.
Listing Git Branches
Local Branches
To list all local branches, run git branch:
git branch dev
feature-login
hotfix
* mainThe current branch is highlighted with an asterisk (*). In this example, the active branch is main.
To see the last commit on each branch, add the -v flag:
git branch -v dev a1b2c3d Add user model
feature-login e4f5a6b Fix login redirect
hotfix b7c8d9e Patch memory leak
* main c0d1e2f Update dependenciesTo also see the upstream tracking branch, use -vv:
git branch -vvRemote Branches
To list only remote-tracking branches, use the -r flag:
git branch -r origin/dev
origin/feature-login
origin/mainAll Branches
To list both local and remote branches, use the -a flag:
git branch -a dev
feature-login
hotfix
* main
remotes/origin/dev
remotes/origin/feature-login
remotes/origin/mainFiltering Branches
To filter branches by a pattern, use the --list flag with a glob:
git branch --list 'feature*' feature-login
feature-searchThis also works with remote branches:
git branch -r --list 'origin/release*'Troubleshooting
fatal: not a git repository
Run the command from inside your Git project directory. Confirm with ls -a and check that a .git directory is present.
fatal: a branch named '<name>' already exists
Use git branch to list existing branches. Pick a different branch name, or switch to the existing branch with git switch <name>.
Remote branches are missing from git branch -r
Run git fetch --all --prune to refresh remote-tracking references, then list branches again.
Quick Reference
| Command | Description |
|---|---|
git branch | List local branches |
git branch -a | List local and remote branches |
git branch -r | List remote branches only |
git branch <name> | Create a new branch |
git switch <name> | Switch to a branch |
git switch -c <name> | Create and switch to a new branch |
git push -u origin <name> | Push a new branch to remote |
FAQ
What is the difference between git switch and git checkout?git switch is dedicated to switching and creating branches. git checkout does the same but also handles file restoration, which can lead to confusion. Use git switch for branches and git restore for files.
How do I delete a branch?
Use git branch -d <name> to delete a local branch, or git push origin --delete <name> to delete a remote branch. See our guide on deleting Git branches
.
How do I rename a branch?
Use git branch -m <old-name> <new-name>. See our guide on renaming Git branches
.
Why does git branch not show remote branches?
By default, git branch only lists local branches. Use -a to include remote-tracking branches, or -r to show only remote branches. Run git fetch first to make sure the remote branch list is up to date.
Conclusion
Use git branch to create and list branches, and git switch to move between them. The -a and -r flags show remote branches, and git push -u publishes a local branch to the remote. For related operations, see our guides on renaming
and deleting
Git branches. If you need to sync those branches with a remote safely, see git fetch vs git pull
.
If you have any questions, feel free to leave a comment below.
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page