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

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


我试着:

git remote rename regacy legacy

但这给出了一个错误:

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


当前回答

我使用这些git别名,它几乎自动完成工作:

git config --global alias.move '!git checkout master; git branch -m $1 $2; git status; git push --delete origin $1; git status; git push -u origin $2; git branch -a; exit;'

用法:git从_branch移动到_branch

如果你有默认的名称,如master, origin等,它就可以工作。 你可以根据自己的意愿进行修改,但这只是个概念。

其他回答

重命名本地分支

 git branch -m <old_name> <new_name>

将本地分支从master重命名为legacy

git branch -m master legacy

重命名远程分支

删除使用旧名称的远程分支 将具有新名称的本地分支推到远程存储库

git push origin :regacy

git push origin legacy

Git push origin:regacy删除名为regacy的远程分支。 Git push origin legacy将名为legacy的本地分支推到远程存储库,并创建一个名为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>

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

# 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>


有几种方法可以做到这一点:

更改您的本地分支,然后推动您的更改 使用新名称将分支推到远程,同时在本地保留原始名称

重命名本地和远程

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

# Delete the old branch on remote - where <remote> is, for example, origin
git push <remote> --delete <old_name>

# Or shorter way to delete remote branch [:]
git push <remote> :<old_name>

# Prevent git from using the old name when pushing in the next step.
# Otherwise, git will use the old upstream name instead of <new_name>.
git branch --unset-upstream <new_name>

# Push the new branch to remote
git push <remote> <new_name>

# Reset the upstream branch for the new_name local branch
git push <remote> -u <new_name>


只重命名远程分支

信用:ptim

# In this option, we will push the branch to the remote with the new name
# While keeping the local name as is
git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>

重要提示:

当你使用git分支-m (move)时,git也会用新名称更新你的跟踪分支。

Git远程重命名遗留遗留

Git远程重命名正在尝试更新配置文件中的远程部分。它将使用给定名称将远程重命名为新名称,但在您的示例中,它没有找到任何名称,因此重命名失败。

但它不会像你想的那样;它将重命名本地配置远程名称,而不是远程分支。


请注意 Git服务器可能允许你使用web界面或外部程序(如Sourcetree等)重命名Git分支,但你必须记住,在Git中所有的工作都是在本地完成的,所以建议使用上述命令来工作。

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

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.