0

I cloned a repo, I was defaulted to a master branch. I have to do 'git fetch origin staging' to change to staging branch. But in the github there are bunch of feature branch like f1, f2.. how to do git fetch origin all to get all of them instead fetch them one by one from the origin?

  • Did you clone with `--depth` or `--single-branch`? – torek Nov 19 '20 at 03:51
  • nope what's that for? so there's no option to fetch all branches? – Nadi Jayden Nov 19 '20 at 09:54
  • The `--depth` option implies the `--single-branch` option, and the `--single-branch` option directs `git clone` to alter the default setup that copies all commits, and turns all branch names into remote-tracking names. Based on your question, I was guessing that you had done this and therefore had only one remote-tracking name, but if not, I guess instead that you think `git clone` should copy all branches. In fact, it copies *no* branch names: instead, it runs one final Git command that creates one *new* branch. See [this answer](https://stackoverflow.com/a/64840425/1256452) for details. – torek Nov 19 '20 at 10:01
  • git clone does copy all the branches, but after that if I want to go to other branch I have to do git fetch origin && git checkout , I'm asking can I with a fetch all command, I need not to fetch origin – Nadi Jayden Nov 20 '20 at 02:49
  • All you need is `git checkout ` (or in Git 2.23 or later, `git switch `). You should, however, run `git fetch` (with no arguments at all or with the name `origin`) occasionally to pick up any *new* commits from the other Git repository, if other people are putting new commits in the other Git repository. – torek Nov 20 '20 at 02:55
  • Note that the checkout step *creates* your own branch name. Once you have done that, it's your responsibility to update this name. The remote-tracking (`origin/*`) names get updated automatically by a `git fetch`, as they are your copies of someone else's names, but *your* names are *yours* and your `git fetch` will not update them. It updates the *remote-tracking names* as that is the remote-tracking names' purpose. – torek Nov 20 '20 at 02:57
  • If all you want to know is, say, *what is the latest commit on the branch named `develop` over on `origin`*, one way to answer that question is to run `git fetch origin` (or `git fetch` with no argument) followed by `git log origin/develop`. You do not need a `develop` at all. – torek Nov 20 '20 at 02:58
  • I found a cleaner solution, git branch -r and git checkout -b LocalName origin/remotebranchname – Nadi Jayden Nov 20 '20 at 04:30
  • Yes, `git branch -r` lists your remote-tracking names. You don't need the `-b` in most cases—there are some where you do—but there's yet another option, which is `git checkout --track origin/` which is short for `git checkout -b origin/`, more or less. – torek Nov 20 '20 at 06:44

0 Answers0