The simple (but subtly wrong, in multiple ways) answer is that you already have all the branches.
I am trying to learn git and its workflow & I want to ask if how do I fetch all the branches in git after cloning a project repo in github?
As tymtam said, you can just git checkout
a name like basic-feature
and suddenly it's there.
This bit of magic is brought to you by what Git calls DWIM mode, where DWIM stands for Do What I Mean (as opposed to what you said). If you ask Git to check out a branch name that you don't have yet—such as basic-feature
—your Git will proceed to search through a second set of names, which are your Git's remote-tracking names. On finding an appropriate match, git checkout
will actually create the name for you, right on the spot.
To list your repository's remote-tracking names, use:
git branch --list --remote
which you can abbreviate to just:
git branch -r
(Note: if your Git is extremely old, it may have only the short form options.)
A remote-tracking name is a name like origin/master
or origin/basic-feature
. When you clone some other repository, you give your Git a short name for that other repository. You can choose a name, but the default one is origin
. This is called a remote. The short name origin
stands in for the longer URL that you entered (e.g., https://github.com/path/to/repo.git
).
When your Git calls up the other Git, your Git gets, from the other Git, a list of all of its branch names. Your Git then copies those names to your origin/*
names—or some other prefix if you chose some other name instead of origin
. These are your remote-tracking names, which remember their Git's branch names. (Git itself calls them remote-tracking branch names, but I find the extra word branch in here unhelpful. These names act sort of like branch names, but enough different that I prefer to leave out the word branch now.)
There is a problem with Git's nomenclature here, because the word branch itself has multiple different meanings. See What exactly do we mean by "branch"? Sometimes we mean a branch name, sometimes we mean a remote-tracking name, and sometimes we mean some collection of commits, probably found by using one of those names. You must infer from context which of these someone means, if they just say "a branch".
Note that while git fetch
has this --all
option, it means all remotes. You can list out which remote names you have—likely just origin
—using:
git remote
If you have more than one, git fetch --all
will invoke git fetch
on each one. If, as usual, you only have one, git fetch --all
will invoke git fetch
on all one of them, and hence not do anything different from git fetch
without --all
.