问题
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?
回答1:
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
回答2:
As the others have posted, there's no folder concept in Git, these are just names. Your only choice is to rename them one by one. A script can help with doing this - here's an example for renaming the local branches:
for i in $(git branch --list 'feature/*') ; do
echo "$i"
# Cut off the first part
newbranch=new/$(basename "$i")
# Rename the existing branch
git branch -m "$i" "$newbranch"
# Push the new branche
git push origin "$newbranch"
# Delete the old branch from the remote
git push origin :"$newbranch"
done
This will rename all branches matching 'feature/' to 'new/'.
Anybody pulling from this repo will have to set up tracking for the renamed branches, as the previous branches will no longer be there.
回答3:
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.
回答4:
in git, you can name a branch in the way you want, using slashes (/) or not. These slashes DO NOT refer to folder separations. So if you want to rename all branches starting with "feature", you'll have to do it manually for each of them.
Using the same prefix can help you to list certain subsets of branches; like this:
git branch --list "feature/*"
but not much more than that afaik.
来源:https://stackoverflow.com/questions/32458667/rename-git-branch-folder