ChatGPT解决这个技术问题 Extra ChatGPT

如何重命名 Git 本地和远程分支名称?

我有四个分支,例如 master -> origin/regacy,FeatureA -> origin/FeatureA。如您所见,我输入了错误的名称。

所以我想重命名一个远程分支名称(origin/regacy → origin/legacy 或 origin/master)

我尝试以下命令:

git remote rename regacy legacy

但是 Git 控制台向我返回了一条错误消息。

 error : Could not rename config section 'remote.regacy' to 'remote.legacy'

我怎么解决这个问题?

简而言之: 1. 到当地分行结账,2. git branch -M new-name 3. git push -d origin old-name 4. git push -u origin new-name

A
Adam Burley

https://i.stack.imgur.com/YeBYR.jpg

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

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

重命名本地和远程

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

https://i.stack.imgur.com/YOHON.png

仅重命名远程分支

学分: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 branch -m(移动)时,Git 还会使用新名称更新您的跟踪分支。

git远程重命名遗留遗留

git remote rename 正在尝试更新配置文件中的远程部分。它会将具有给定名称的遥控器重命名为新名称,但在您的情况下,它没有找到任何名称,因此重命名失败。

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

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


不要忘记取消设置旧上游:git checkout <new_name> ; git branch --unset-upstream
@MiguelPing 的评论很重要。当尝试重命名一个我已经推送到 github 的分支时,删除、重命名本地、它并重新推送会导致再次使用旧名称。如果我在重新推送之前--unset-upstream,它会按预期工作。
有没有办法避免自动关闭远程旧分支上打开的 PR?在 gitlab 中遇到这个问题,旧分支名称上的 PR 在推送到原点后被关闭。
这是一个单命令版本:OLD=<old>; NEW=<new>; REMOTE=<remote>; git branch -m $OLD $NEW && git push $REMOTE --delete $OLD && git push $REMOTE $NEW && git push $REMOTE -u $NEW
@JimArcher 是的,因为您需要使用新名称而不是旧名称。我改了,但原作者没有解释就改回来了。
D
Dr1Ku

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

重命名本地分支:如果您在要重命名的分支上: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


在前 2 步之后,如果您收到有关当前分支指向远程 repo 上不存在的分支的错误消息,则第 3 步将解决此问题
@Dr1Ku 需要知道 git push <remote> --delete old_name & 之间的区别git push origin :old-name new-name 删除一个分支。
BitBucket 用户:如果重命名 master,则修复第 2 步的错误,在 Repo 详细信息中将默认分支设置为新分支。错误是:By default, deleting the current branch is denied, because the next 'git clone' won't result in any file checked out, causing confusion. You can set 'receive.denyDeleteCurrent' configuration variable to 'warn' or 'ignore' in the remote repository to allow deleting the current branch, with or without a warning message. To squelch this message, you can set it to 'refuse'. error: refusing to delete the current branch: refs/heads/master
最简单的一个
对于 Github 和 Bitbucket 用户,当您运行第 2 步时,它将创建新的远程分支,但您会看到远程错误“拒绝删除当前分支”。所以只需转到存储库设置➔ 分支➔ 更改默认/主分支➔ new_branch_name ➔ 保存。然后再次运行第 2 步,以便删除旧的远程分支名称。
V
Vitalii Andrusishyn

重命名您的本地分支。

如果您在要重命名的分支上:

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

您的解决方案简单明了。感谢@Vitaliy Andrusishyn 分享您的知识。
git push origin :old-name new-name 关闭了我的公开 PR。如果您在 PR 中有正在进行的讨论,请小心。
在输入 git push origin :old-name new-name 命令之前,请确保您的默认远程分支未设置为您要重命名和删除的当前分支
带有 sam-lahm 和 ekanem-eno 警告的几乎完全简单的解决方案。多谢你们。
E
Eric Aya

检查您正在使用以下命令的分支

git branch -a 

结帐到要重命名的分支

git checkout branch_to_rename

使用重命名分支

git branch -m new_name

推送更改

git push origin :old_name new_name

P
Peter Mortensen

似乎有一个直接的方法:

如果您真的只想远程重命名分支(而不同时重命名任何本地分支),您可以使用 git push /:refs/heads/ 之类的单个命令来执行此操作: 在 Git 中远程重命名分支

有关更多详细信息,请参阅原始答案。


在 git 2.20.1 中不起作用删除了旧分支,但未创建新分支。
P
Peter Mortensen

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

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

重命名本地分支:

如果登录另一个分支,

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

m
milesmeow

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

切换到要重命名的本地分支 git checkout 重命名本地分支 git branch -m 推送本地分支并重置上游分支 git push origin -u 删除远程分支 git push origin --delete

这是基于 this article


a
avivamg

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

git branch -m <oldBranchName> <newBranchName>
git push origin :<oldBranchName>
git push --set-upstream origin <newBranchName>

来自 git 文档的解释:

git branch -m 或 -M 选项,将重命名为 .如果有相应的 reflog,则将其重命名为 match,并创建一个 reflog 条目以记住分支重命名。如果存在,则必须使用 -M 来强制进行重命名。特殊的 refspec : (或 +: 允许非快进更新)指示 Git 推送“匹配”分支:对于本地存在的每个分支,如果已存在同名分支,则更新远程端在远端。 --set-upstream 设置跟踪信息,因此被认为是上游分支。如果没有指定,则默认为当前分支。


v
vchan

另一种解决方法如下:

结帐到要更改的分支从中创建一个新分支将上游设置为远程从本地和远程删除旧分支

进一步来说:

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

P
Peter Mortensen

没有直接的方法,

重命名本地分支,我当前的分支是master git branch -m master_renamed #master_renamed 是master 的新名称 删除远程分支,git push origin --delete master #origin 是remote_name 将重命名的分支推送到远程,git push origin master_renamed

而已...


很好很简单,只有 3 个步骤。我可以建议的唯一改进是 git push -u origin master_renamed 将分支设置为跟踪分支
P
Peter Mortensen

即使不通过三个简单的步骤重命名本地分支也可以做到这一点:

转到您在 GitHub 中的存储库 从要重命名的旧分支创建一个新分支 删除旧分支


T
Tarik

我使用这些 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 move FROM_BRANCH TO_BRANCH

如果您有默认名称(例如 master、origin 等),它就可以工作。您可以根据需要进行修改,但它会为您提供想法。


A
Anupam Maurya

重命名您的本地分支。如果您在要重命名的分支上: 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

可以了,好了!


S
Sebastian Viereck

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

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

它与现有答案有何不同?
E
Elifarley

如果要使用单个命令重命名当前分支,如下所示:

git rename my-new-branch-name

然后,您必须创建一个名为 git-rename 的文件,使其可执行 (chmod +x git-rename) 并将其保存到 $PATH 中包含 this 的文件夹中:

#!/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"