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

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


我试着:

git remote rename regacy legacy

但这给出了一个错误:

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


当前回答

如果您已经将错误的名称推到远程,请执行以下操作:

切换到要重命名的本地分支 Git checkout <old_name> 重命名本地分支 Git分支-m <new_name> 推送<new_name>本地分支并重置上游分支 Git push origin -u <new_name> 删除<old_name>远程分支 Git push origin——delete <old_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>

我使用这些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等,它就可以工作。 你可以根据自己的意愿进行修改,但这只是个概念。

除了其他人列出的其他步骤,请记住:

如果你试图删除默认的分支,例如master,当你运行git push origin:<branch_name>时,你会得到这个错误

! [remote rejected] master (refused to delete current branch: refs/heads/<branch_name>) error: failed to push some refs to '<repo_name>'. [remote rejected] master (refused to delete current branch: refs/heads/<branch_name>)

a)在删除分支之前更改默认值(Github示例)

去回购吧。 点击“设置” 修改默认分支如下图所示:

b)删除[target] remote:

$ git push origin:master

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

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.

如果你想用一个命令重命名当前分支,就像这样:

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"