我有一个本地分支主指向一个远程分支原点/regacy(哎呀,拼写错误!)
如何将远程分支重命名为原点/遗留或原点/主?
我试着:
git remote rename regacy legacy
但这给出了一个错误:
错误:不能重命名配置区段的远程。Regacy '到'remote.legacy'
我有一个本地分支主指向一个远程分支原点/regacy(哎呀,拼写错误!)
如何将远程分支重命名为原点/遗留或原点/主?
我试着:
git remote rename regacy legacy
但这给出了一个错误:
错误:不能重命名配置区段的远程。Regacy '到'remote.legacy'
当前回答
另一个解决方法如下:
签出到您想更改的分支 从它创建一个新分支 上游设置为远程 从本地和远程删除旧的分支
更具体地说:
# Checkout to the branch you want to rename
git checkout <old_branch_name>
# Create a new branch from the old one and checkout to it
git checkout -b <new_branch_name>
# Push the new branch to remote
git push -u <origin> HEAD
# Delete local branch
git branch -d <old_branch_name>
# Delete remote branch
git push <origin> -d <old_branch_name>
其他回答
如果您错误地命名了一个分支并将其推到远程存储库,请按照以下步骤重命名该分支(基于本文):
重命名你的本地分支: 如果你在你想重命名的分支上: Git branch -m new-name 如果你在不同的分支上: Git branch -m old-name new-name 删除旧名称的远程分支,并推送新名称的本地分支: Git推送源:old-name new-name 重置新名称本地分支的上游分支: 切换到分支,然后: Git推送origin -u new-name
似乎有一个直接的方法:
如果您真的只想远程重命名分支(而不同时重命名任何本地分支),您可以使用如下命令来完成此操作 Git push <remote> <remote>/<old_name>:refs/heads/<new_name>:<old_name> 在Git中远程重命名分支
详见原文。
如果你想用一个命令重命名当前分支,就像这样:
git rename my-new-branch-name
然后,你必须创建一个名为git-rename的文件,使其可执行(chmod +x git-rename),并将其保存到你的$PATH文件夹中,包含以下内容:
#!/bin/sh
currentBranch="$(git rev-parse --abbrev-ref HEAD)"
test $# != 1 && cat <<EOF && exit 1
Renames the current branch ($currentBranch) both locally and remotely.
USAGE:
git rename <new branch name>
EOF
newBranch="$1"; shift
git branch -m "$newBranch" && \
git push origin :"$currentBranch" "$newBranch"
重命名您的本地分支。
如果你在你想重命名的分支上:
git branch -m new-name
如果你当前停留在不同的分支上:
git branch -m old-name new-name
删除旧名称的远程分支,并推送新名称的本地分支。
停留在目标分支上,然后:
git push origin :old-name new-name
重置新名称本地分支的上游分支。
切换到目标分支,然后:
git push origin -u new-name
附加一个简单的片段来重命名你的当前分支(本地的和原始的):
git branch -m <oldBranchName> <newBranchName>
git push origin :<oldBranchName>
git push --set-upstream origin <newBranchName>
git文档中的解释:
git branch -m or -M option, will be renamed to . If had a corresponding reflog, it is renamed to match , and a reflog entry is created to remember the branch renaming. If exists, -M must be used to force the rename to happen. The special refspec : (or +: to allow non-fast-forward updates) directs Git to push "matching" branches: for every branch that exists on the local side, the remote side is updated if a branch of the same name already exists on the remote side. --set-upstream Set up 's tracking information so is considered 's upstream branch. If no is specified, then it defaults to the current branch.