我有一个本地分支主指向一个远程分支原点/regacy(哎呀,拼写错误!)

如何将远程分支重命名为原点/遗留或原点/主?


我试着:

git remote rename regacy legacy

但这给出了一个错误:

错误:不能重命名配置区段的远程。Regacy '到'remote.legacy'


当前回答

我必须执行以下任务来重命名本地和远程分支:

# Rename the local branch to the new name
git branch -m <old_name> <new_name>

#  Delete the old remote branch
git push origin --delete <old_name>

# push to new remote branch - creates new remote branch
git push origin <new_name>

# set new remote branch as default remote branch for local branch
git branch --set-upstream-to=origin/<new_name> <new_name>

其他回答

如果您错误地命名了一个分支并将其推到远程存储库,请按照以下步骤重命名该分支(基于本文):

重命名你的本地分支: 如果你在你想重命名的分支上: Git branch -m new-name 如果你在不同的分支上: Git branch -m old-name new-name 删除旧名称的远程分支,并推送新名称的本地分支: Git推送源:old-name new-name 重置新名称本地分支的上游分支: 切换到分支,然后: Git推送origin -u new-name

这甚至可以在不重命名本地分支的情况下完成,只需简单的三个步骤:

去你的GitHub仓库 从要重命名的旧分支创建一个新分支 删除旧的分支

附加一个简单的片段来重命名你的当前分支(本地的和原始的):

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.

托管在GitHub上的Git存储库中的分支可以使用存储库设置重命名。作为一个副作用,GitHub中的分支保护规则也将被改变。

访问存储库设置中的“分支”:https://github.com/<name>/<repository-name>/settings/ Branches 重命名分支 每个使用这个存储库的人都必须在本地做:

$ git获取 $ git checkout <new_name>

它也可以通过以下方式完成。

首先重命名本地分支,然后重命名远程分支。

重命名本地分支:

如果登录到另一个分支,

git branch -m old_branch new_branch 

如果登录到同一个分支,

git branch -m new_branch

重命名远程分支:

git push origin :old_branch    // Delete the remote branch

git push --set-upstream origin new_branch   // Create a new remote branch