Git list branches commands help you view local, remote, and all branches in a repository as part of working with
git branch basics. Using git branch, git branch -r, and git branch -a, you can quickly check available branches and manage your workflow efficiently as part of a complete
Git workflow.
Git list branches - quick cheat sheet
| Task | Command |
|---|---|
| List local branches | git branch |
| List remote branches | git branch -r |
| List all branches (local + remote) | git branch -a |
| List branches with last commit | git branch -v |
| List all branches with tracking info | git branch -vv |
| List merged branches | git branch --merged |
| List unmerged branches | git branch --no-merged |
| List remote branches (all, even unfetched) | git ls-remote --heads origin |
| List all references (advanced) | git for-each-ref --format='%(refname:short)' refs/heads/ |
| List remote repositories | git remote |
| List remote repositories with URL | git remote -v |
| Show detailed remote info | git remote show origin |
| Fetch latest remote branches | git fetch |
| Fetch all remotes | git fetch --all |
| Remove stale remote branches | git remote prune origin |
| Search branch by name | git branch | grep <name> |
| Show branches sorted by last commit | git branch --sort=-committerdate |
| List only local branches (explicit) | git for-each-ref refs/heads/ |
| List only remote tracking branches | git for-each-ref refs/remotes/ |
Git list branches
Understanding the difference between local and remote branches is the foundation of working efficiently with Git. Local branches exist in your system, while remote branches are hosted on repositories like GitHub or GitLab and are managed using
git remote.
List only local branches using git branch
To view all branches available in your local repository, use:
git branchThis command lists only local branches and highlights the current branch with an asterisk (*).
List only remote branches using git branch -r
To see branches available on the remote repository (tracked locally), use:
git branch -rThis shows remote-tracking branches such as:
origin/main
origin/devList all branches using git branch -a
To display both local and remote branches together:
git branch -aThis is useful when you want a complete view of all available branches.
Understand origin/HEAD references
You may see entries like:
origin/HEAD -> origin/mainorigin→ default remote repository nameHEAD→ pointer to the default branch on remoteorigin/main→ actual default branch
This helps Git identify which branch is used by default when cloning or pulling.
List local branches in Git
Check current branch (* symbol explained)
Run:
git branchExample output:
* main
feature-login
bugfix-issue1The * symbol indicates the current active branch where you are working.
Create and verify branches
Create new branches:
git branch feature_A
git branch feature_BVerify branches:
git branchNow you will see:
* main
feature_A
feature_BThis confirms that the new branches have been successfully created and are available for use with git branch basics.
List Remote Branches in Git
Sometimes remote branches exist but are not visible locally. This usually happens because your local repository is not updated with the latest remote state, which can be resolved using
git fetch.
Why git branch -r may not show all branches
The command:
git branch -ronly shows branches that are already fetched and tracked locally. Newly created branches on remote will not appear until you fetch them.
Use git fetch to sync remote branches
To update your local repository with the latest remote branches:
git fetchAfter fetching, run:
git branch -rto see updated remote branches.
Use git ls-remote to see all remote branches
To view all branches directly from the remote without fetching:
git ls-remote --heads originThis command queries the remote repository and lists all available branches without needing
git clone.
Difference between fetched vs unfetched branches
- Fetched branches → Available locally after running
git fetch - Unfetched branches → Exist on remote but not yet visible locally
To always stay updated, regularly run:
git fetch --allCheck and Verify Remote Repositories
List all remotes using git remote -v
To view configured remote repositories along with their URLs using
git remote:
git remote -vExample output:
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)Show remote details using git remote show origin
To get detailed information about a specific remote:
git remote show originThis shows:
- tracked branches
- default branch
- fetch/push configuration
Identify upstream branch configuration
To check which local branch is linked to which remote branch:
git branch -vvExample:
main abc123 [origin/main] latest commit messageThis indicates the local branch is tracking origin/main.
Check which branch tracks which remote
To explicitly set or verify tracking:
git branch --set-upstream-to=origin/mainTracking branches allow simplified commands like
git pull and
git push.
git pull
git pushwithout specifying remote and branch every time.
Filter, Search and Format Branch Output
When working with repositories that have many branches, filtering and formatting output becomes essential to quickly find relevant information.
Search branch by name using grep
To search for a specific branch:
git branch | grep <branch-name>Example:
git branch | grep featureThis helps locate branches matching a pattern before switching using
git checkout.
Sort branches by last commit date
To list branches sorted by most recent activity:
git branch --sort=-committerdateThis is useful to identify recently updated branches before merging changes using
git merge.
Show only active branches
To view only branches that are actively being worked on (recent commits):
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/This provides a cleaner and more customizable output when managing repositories with multiple branches using
git rebase.
Format output using git for-each-ref
For advanced formatting:
git for-each-ref --format='%(refname:short) - %(committerdate)' refs/heads/You can customize output to include:
- branch name
- commit date
- author
- tracking info
List Branches with Additional Details
Sometimes you need more than just branch names — such as commit details, merge status, or tracking info.
Show last commit on each branch
To display branches with their latest commit:
git branch -vThis shows:
- branch name
- last commit hash
- commit message
Show merged vs unmerged branches
To list branches already merged into the current branch:
git branch --mergedTo list branches not yet merged:
git branch --no-mergedThis helps in cleanup and review processes before removing branches using
git delete branch.
Identify stale or unused branches
To find branches not updated recently:
git branch --sort=committerdateOlder branches appear at the top, helping identify stale branches before cleanup using
git prune.
Display tracking information
To see which remote branch each local branch tracks:
git branch -vvExample output:
feature1 abc123 [origin/feature1] commit messageTroubleshoot Missing Remote Branches
Branch exists on GitHub but not locally
If a branch is visible on GitHub but not in your local repo, it means your local repository is not synced and needs
git fetch.
Fix using git fetch --all
Fetch all updates from all remotes:
git fetch --allThen verify:
git branch -rClean stale references using git remote prune
To remove references to deleted remote branches:
git remote prune originThis ensures your branch list is clean and accurate using
git prune.
Handle deleted remote branches
Sometimes remote branches are deleted but still appear locally. To clean them:
git fetch --pruneor
git remote prune originFrequently Asked Questions
1. How to list all branches in Git?
Use git branch -a to list all local and remote branches in a Git repository.2. How to list local branches in Git?
Use git branch to show all local branches in your repository.3. How to list remote branches in Git?
Use git branch -r to list all remote branches connected to your repository.4. What is git show-branch?
git show-branch displays branches along with their latest commits for comparison.5. How to check current branch in Git?
Use git branch to see the current branch marked with an asterisk (*).6. Why git branch -r not showing all branches?
git branch -r only shows fetched branches. Run git fetch or git fetch --all to update remote branch list.7. How to list remote repositories in Git?
Use git remote -v to list all configured remotes along with their URLs.8. What is git ls-remote used for?
git ls-remote lists references from a remote repository without fetching them locally.9. How to remove stale remote branches in Git?
Use git remote prune origin or git fetch --prune to clean up deleted remote branch references.Summary:
To effectively manage branches in Git:
- Use
git branch,-r, and-afor quick listing - Use
git fetchto stay updated with remote changes - Use
git branch -vvfor tracking details - Use filtering and sorting to manage large repositories effectively along with git branch basics.
- Use prune commands to clean stale branches
Choosing the right command based on your use case improves productivity and avoids confusion.



![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)






