Skip to content

Instantly share code, notes, and snippets.

@devinschumacher
Last active September 16, 2024 22:48
Show Gist options
  • Save devinschumacher/4eb9592fa5b823ace03bc4d06701793d to your computer and use it in GitHub Desktop.
Save devinschumacher/4eb9592fa5b823ace03bc4d06701793d to your computer and use it in GitHub Desktop.
How to get (fetch) and view all of the branches on a repository in git
title tags
How to get (fetch) and view all of the branches on a repository in git
git

How to get (fetch) and view all of the branches on a repository in git

  1. git fetch --all
  2. git branch --all

First you have to fetch from the remote, incase there are things you don't yet have locally.

$ git fetch --all

Then, to see all the branches you run a git branch command with the same --all flag. Here's the different between the two commands:

$ git branch # show all local branches
$ git branch -r # show only remote branches
$ git branch --all # show all branches, both local and remote
git branch --all


  cleanup
* dev
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/cleanup
  remotes/origin/dev
  remotes/origin/main
  remotes/origin/remove-secrets-and-mock-api

Understanding the output:

There's three local branches: cleanup, dev, and main. You're currently on the dev branch. The remote (named "origin") has four branches:

  1. cleanup
  2. dev
  3. main
  4. remove-secrets-and-mock-api
  • the * next to a branch name indicates that this is your currently checked-out branch.
  • the branches starting with remotes/ show the branches on remote

To checkout that 4th branch, you would run:

$ git checkout -b remove-secrets-and-mock-api origin/remove-secrets-and-mock-api

Syntax:

git checkout -b <local-branch-name> origin/<remote-branch-name>

  • git checkout -b: This command creates a new branch and immediately switches to it.
  • <local-branch-name>: This is the name you want to give to your new local branch. You can choose any name, but it's often practical to use the same name as the remote branch.
  • origin/<remote-branch-name>: This specifies which remote branch you want your new local branch to track.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment