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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30590083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:15:48  来源:igfitidea点击:

How do I rename both a Git local and remote branch name?

gitrepositoryrenamegit-branch

提问by JayD

I have four branches like master -> origin/regacy, FeatureA -> origin/FeatureA. As you can see, I typed the wrong name.

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

So I want to rename a remote branch name (origin/regacy → origin/legacy or origin/master)

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

I try the command below:

我尝试以下命令:

git remote rename regacy legacy

But Git console returned an error message to me.

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

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

How can I solve this problem?

我怎么解决这个问题?

回答by CodeWizard

Enter image description here

在此处输入图片说明



There are a few ways to accomplish that:

有几种方法可以实现:

  1. Change your local branch and then push your changes
  2. Push the branch to remote with the new name while keeping the original name locally
  1. 更改您的本地分支,然后推送您的更改
  2. 使用新名称将分支推送到远程,同时在本地保留原始名称

Renaming local and remote

重命名本地和远程

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

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

Enter image description here

在此处输入图片说明



Renaming Only remote branch

仅重命名远程分支

Credit: ptim

信用: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>


Important note:

重要的提示:

When you use the git branch -m(move), Git is also updatingyour tracking branch with the new name.

当您使用git branch -m(move) 时,Git 还会使用新名称更新您的跟踪分支。

git remote rename legacy legacy

git remote rename legacy legacy

git remote renameis trying to update your remote section in your configuration file. It will rename the remote with the given name to the new name, but in your case, it did not find any, so the renaming failed.

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

Butit will not do what you think; it will rename your localconfiguration remote name and notthe remote branch. 

它不会如你所想;它将重命名您的本地配置远程名称而不是远程分支。 



NoteGit servers might allow you to rename Git branches using the web interface or external programs (like Sourcetree, etc.), but you have to keep in mind that in Git all the work is done locally, so it's recommended to use the above commands to the work.

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

回答by ZILONG PAN

If you have named a branch incorrectly AND pushed this to the remote repository follow these steps to rename that branch (based on this article):

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

  1. Rename your local branch:

    • If you are on the branch you want to rename:
      git branch -m new-name

    • If you are on a different branch:
      git branch -m old-name new-name

  2. Delete the old-nameremote branch and push the new-namelocal branch:
    git push origin :old-name new-name

  3. Reset the upstream branch for the new-name local branch:
    Switch to the branch and then:
    git push origin -u new-name

  1. 重命名您的本地分支:

    • 如果您在要重命名的分支上:
      git branch -m new-name

    • 如果您在不同的分支:
      git branch -m old-name new-name

  2. 删除old-name远程分支并推送new-name本地分支
    git push origin :old-name new-name

  3. 重置新名称本地分支的上游分支
    切换到分支,然后:
    git push origin -u new-name

回答by ptim

It seems that there is a direct way:

好像有一个直接的方法:

If you really just want to rename branches remotely (without renaming any local branches at the same time) you can do this with a single command like

git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>

Renaming branches remotely in Git

如果您真的只想远程重命名分支(同时不重命名任何本地分支),您可以使用一个命令来执行此操作,例如

git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>

在 Git 中远程重命名分支

See the original answer for more detail.

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

回答by Atequer Rahman

It can also be done the following way.

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

At first rename local branch, then remote branch.

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

Renaming the local branch:

重命名本地分支:

If logged in another branch,

如果登录另一个分支,

git branch -m old_branch new_branch 

If logged in the same branch,

如果登录同一个分支,

git branch -m new_branch

Renaming remote branch:

重命名远程分支:

git push origin :old_branch    // Delete the remote branch

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

回答by milesmeow

If you have already pushed the wrong name to remote, do the following:

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

  1. Switch to the local branch you want to rename

    git checkout <old_name>

  2. Rename the local branch

    git branch -m <new_name>

  3. Push the <new_name>local branch and reset the upstream branch

    git push origin -u <new_name>

  4. Delete the <old_name>remote branch

    git push origin --delete <old_name>

  1. 切换到要重命名的本地分支

    git checkout <old_name>

  2. 重命名本地分支

    git branch -m <new_name>

  3. 推送<new_name>本地分支并重置上游分支

    git push origin -u <new_name>

  4. 删除<old_name>远程分支

    git push origin --delete <old_name>

This was based on this article.

这是基于这篇文章

回答by avivamg

Attaching a Simple Snippetfor renaming your current branch (local and on origin):

附加一个简单的代码片段来重命名当前分支(本地和源头):

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

Explanationfrom git docs:

来自 git 文档的解释

git branch -mor -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-upstreamSet up 's tracking information so is considered 's upstream branch. If no is specified, then it defaults to the current branch.

git branch -m或 -M 选项,将重命名为 . 如果有相应的 reflog,则将其重命名为 match,并创建一个 reflog 条目以记住分支重命名。如果存在,必须使用 -M 来强制重命名发生。

特殊的 refspec :(或 +: 允许非快进更新)指示 Git 推送“匹配”分支:对于本地端存在的每个分支,如果已存在同名分支,则更新远程端在远方。

--set-upstream设置跟踪信息,因此被视为上游分支。如果没有指定,则默认为当前分支。

回答by Mohideen bin Mohammed

There is no direct method,

没有直接的方法,

  1. Rename Local Branch,

    My current branch is master

    git branch -m master_renamed#master_renamed is new name of master

  2. Delete remote branch,

    git push origin --delete master#origin is remote_name

  3. Push renamed branch into remote,

    git push origin master_renamed

  1. 重命名本地分支

    我当前的分支是master

    git branch -m master_renamed#master_renamed 是 master 的新名称

  2. 删除远程分支,

    git push origin --delete master#origin 是 remote_name

  3. 将重命名的分支推送到远程,

    git push origin master_renamed

That's it...

就是这样...

回答by Code_Mode

This can be done even without renaming the local branch in three simple steps:

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

  1. Go to your repository in GitHub
  2. Create a new branch from the old branch which you want to rename
  3. Delete the old branch
  1. 转到您在 GitHub 中的存储库
  2. 从要重命名的旧分支创建一个新分支
  3. 删除旧分支

回答by Tarik

I use these git alias and it pretty much does the job automatic:

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

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

Usage: git move FROM_BRANCH TO_BRANCH

用法:git move FROM_BRANCH TO_BRANCH

It works if you have the default names like master, origin etc. You can modify as you wish but it gives you the idea.

如果您有默认名称,如 master、origin 等,它就可以工作。您可以根据需要进行修改,但它会给您一个想法。

回答by Sebastian Viereck

I had to do the following task to rename local and remote branch:

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

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