How to clone all remote branches in Git?

后端 未结 30 2166
情话喂你
情话喂你 2020-11-22 01:08

I have a master and a development branch, both pushed to GitHub. I\'ve cloned, pulled, and fetched, but I re

30条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 01:33

    I needed to do exactly the same. Here is my Ruby script.

    #!/usr/bin/env ruby
    
    local = []
    remote = {}
    
    # Prepare
    %x[git reset --hard HEAD]
    %x[git checkout master] # Makes sure that * is on master.
    %x[git branch -a].each_line do |line|
      line.strip!
      if /origin\//.match(line)
         remote[line.gsub(/origin\//, '')] = line
       else
         local << line
       end
    end
    # Update 
    remote.each_pair do |loc, rem|
      next if local.include?(loc)
      %x[git checkout --track -b #{loc} #{rem}]
    end
    %x[git fetch]
    

提交回复
热议问题