How to Create and List Local and Remote Git Branches

By 

Updated on

5 min read

How to Create and List 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:

Terminal
git branch feature-login

The command creates the branch but does not switch to it. To switch to the new branch, use git switch:

Terminal
git switch feature-login
output
Switched to branch 'feature-login'

To create a new branch and switch to it in a single command, use git switch -c:

Terminal
git switch -c feature-login
output
Switched to a new branch 'feature-login'

From here, you can use git add and git commit to add commits to the new branch.

Info
In older Git versions (before 2.23), 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:

Terminal
git branch bugfix-header a1b2c3d

You can also create a branch from a tag:

Terminal
git branch release-1.0 v1.0.0

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

Terminal
git push -u origin feature-login

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

output
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:

Terminal
git branch
output
  dev
  feature-login
  hotfix
* main

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

Terminal
git branch -v
output
  dev           a1b2c3d Add user model
  feature-login e4f5a6b Fix login redirect
  hotfix        b7c8d9e Patch memory leak
* main          c0d1e2f Update dependencies

To also see the upstream tracking branch, use -vv:

Terminal
git branch -vv

Remote Branches

To list only remote-tracking branches, use the -r flag:

Terminal
git branch -r
output
  origin/dev
  origin/feature-login
  origin/main

All Branches

To list both local and remote branches, use the -a flag:

Terminal
git branch -a
output
  dev
  feature-login
  hotfix
* main
  remotes/origin/dev
  remotes/origin/feature-login
  remotes/origin/main

Filtering Branches

To filter branches by a pattern, use the --list flag with a glob:

Terminal
git branch --list 'feature*'
output
  feature-login
  feature-search

This also works with remote branches:

Terminal
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

CommandDescription
git branchList local branches
git branch -aList local and remote branches
git branch -rList 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

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