0

I need to bulk move remote git branches from one directory to another

My current setup:

remote:
      Origin
            bug 
               bug1_abcbug
               bug2_xyzbug
               .
               .
               .
               bug50_bug
            
            bugfix
                bug51
                bug52
                .
                .
                .
                Bug200

Now, I want to move all 50 branches from the 'bug' folder to the 'bugfix' folder. I know I can individually rename the branch name and its remote name. However, is there any easier way to just bulk move all branches from one folder to another ?

Thanks in Advance

Maulzey
  • 4,100
  • 5
  • 22
  • 30
  • 1
    Branches don't come in "folders". – matt Sep 17 '22 at 01:39
  • 1
    Agree! something how it is set up at the place where I am trying to solve it. – Maulzey Sep 17 '22 at 01:44
  • I'm not really clear on what you're trying to accomplish. Can you please replace your "setup currently" with the output of something like `git branch -r` if you're asking about renaming branches, or something like `ls` if you're talking about renaming files. – larsks Sep 17 '22 at 01:53
  • 1
    You can certainly write a script that renames a bunch of branches. But if these are on the remote it's not so easy. – matt Sep 17 '22 at 01:56

1 Answers1

2

You can do this in 3 steps:

  1. Run git fetch
  2. Identify the command to isolate the 50 branches. In your example it might be something like (in a *nix shell):
git branch -r | grep origin/bug/bug
  1. Loop through the list of branches, and for each branch name, use your tool of choice to parse out the branch name after the 1st '/' character (bug/bug1...), and again after the 2nd '/' character (bug1...), and then use those strings in conjunction with this command (or function) to rename the branch remotely:
git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>
TTT
  • 22,611
  • 8
  • 63
  • 69