Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Git branch folder

I have a Git repository with a remote branch structure like this:

bugfix001 
bugfix002     
feature
    feature001
    feature002
    feature003
master
task
    task001
    task002

where 'feature' and 'task' are folders within the branch structure and the 'feature001', 'feature002' branches are under the parent 'feature' branch folder.

My question is - is it possible to rename the 'feature' parent folder and keep all the branches under it. So I could rename it to 'hamster' and the structure would look like this

hamster
    feature001
    feature002
    feature003

Just to be clear, I'm not referring to the local file structure, this is the branch structure on the remote. To add a branch under the 'feature' folder I would use:

git checkout -b feature/feature004

I realise that I can rename each sub branch individually and push to the remote but can I rename the 'feature' parent folder which will cause will the sub branches follow the new name?

like image 619
Chris B Avatar asked Aug 14 '26 17:08

Chris B


2 Answers

There is no such thing as "branch folders" in git.

You only have named branches, and the names you are using happen to include a slash, which reminds the humans of a folder-structure; but it really could be something else as well (e.g. @).

To conclude: you will have to rename every branch, and push the new branch names (and delete the old ones)

But that should be simple enough:

remote=origin
for branch in $(git branch --list "feature/*")
do
 newbranch="hamster/${branch#feature/}"
 # locally create new branch based on old one
 git checkout "${branch}"
 git checkout -b "${newbranch}"
 # push the new branch to the remote
 git push --set-upstream "${remote}" "${newbranch}"
 # delete old branch remotely and locally
 git push "${remote}" ":${branch}"
 git branch -d "${branch}"
done
like image 189
umläute Avatar answered Aug 16 '26 06:08

umläute


When you name branches with a / in the name, then a directory is created in refs/heads. For example a branch named feature/feature001 creates a directory named refs/heads/feature/ which contains a file feature001. If you rename the feature directory, you are, in effect, renaming every branch that started with feature/ (modulo anything you have in packed-refs). This isn't the recommended way to rename branches, but it does work. You will need to edit your config file to reconnect any of those branches that specified a tracking branch. Other than that, and again ignoring anything that might be in packed-refs, you can do what you are asking.

like image 21
Wolf Avatar answered Aug 16 '26 06:08

Wolf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!