-2

I have clone the remote repo to local machine.

git clone https://my_name/repo/project.git

Now the local repo has 1 master branch while remote repo has 10-15 branches.

git branch -a

*master
*remotes/origin/another-branch
*remotes/origin/another-branch
*remotes/origin/another-branch
*remotes/origin/another-branch

I want to pull all remote branch as local branch at one command. I find similar answer that pull remote branch as local branch but its only for 1 branch, not all branch.

git checkout -b fix-failing-tests origin/fix-failing-tests

How can I pull all remote branch as lcoal branch in fewer less repetitive command line or even track them? Thank you.

matt
  • 515,959
  • 87
  • 875
  • 1,141
AkiZukiLenn
  • 337
  • 3
  • 14
  • 1
    I think there is some confusion here. The branch `remotes/origin/another-branch` refers to a commit (call it $hash). That commit is already on your local machine, and you do not need to "pull" it. IOW, the phrase "pull all remote branch as local branch" does not really mean anything. If you want a ref in $GIT_DIR/refs/heads that refers to $hash (which I suppose is what is meant by "local branch"), you just need to create it. But "pull" seems like very much the wrong word to use to describe that operation. – William Pursell Nov 05 '22 at 14:12
  • 1
    https://stackoverflow.com/a/379842/140750 – William Pursell Nov 05 '22 at 14:24
  • https://stackoverflow.com/search?q=%5Bgit%5D+clone+all+branches – phd Nov 05 '22 at 14:48

1 Answers1

1

Cloning does create any local branches except for the remote's "default branch". If you want other local branches, you must create them. That's not going to happen by magic; you need to create those local branches corresponding to each remote branch, in turn, one at a time. There is no other way.

If you don't want to do this manually for all remote tracking branches one by one yourself, you can write a script that does it for all remote tracking branches one by one. But one way or another, you must do it for all remove tracking branches one by one.

For example, see the script at https://gist.github.com/wilmarvh/95fe7daed6ee6a63d811677e040ae421. In case that link fails, here's the script (bash or zsh):

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
   git branch --track ${branch#remotes/origin/} $branch
done

(Or write a similar script in any programming language you're familiar with.)

I should add that in my opinion this is a very silly thing to want to do. There is nothing to be gained by creating a local branch you don't need; it just adds overhead in the future, because each local branch will have to be kept up with its remote tracking branch. My recommendation would be to drop this desire and just make a local branch only if you absolutely need it. (In my personal programming life, I never make local branches from remote tracking branches. If I want to see what's in a remote tracking branch, I simply check out the remote tracking branch, detached.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Seems like the suggestive way is to do it one by one from git. But would'nt it caused name confliction? like your remote branch and local branch has different name? – AkiZukiLenn Nov 06 '22 at 09:00
  • Can't they just automate say if you have the right permission, you just auto copy and track that remote branch instead of creating one then pull then merge? – AkiZukiLenn Nov 06 '22 at 09:01