title | tags | |
---|---|---|
How to get (fetch) and view all of the branches on a repository in git |
|
git fetch --all
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:
- cleanup
- dev
- main
- 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.