欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-13 15:39:47
...

本文翻译自:How do I rename both a Git local and remote branch name?

I have four branches like master -> origin/regacy, FeatureA -> origin/FeatureA. 我有四个分支,例如master-> origin / regacy,FeatureA-> origin / FeatureA。 As you can see, I typed the wrong name. 如您所见,我输入了错误的名称。

So I want to rename a remote branch name (origin/regacy → origin/legacy or 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? 我怎么解决这个问题?


#1楼

参考:https://stackoom.com/question/24LsR/如何重命名Git本地和远程分支名称


#2楼

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


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 用新名称将分支推送到远程,同时在本地保留原始名称

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>

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


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 updating your tracking branch with the new name. 当您使用git branch -m (移动)时,Git还将使用新名称更新您的跟踪分支。

git remote rename legacy legacy

git remote rename is trying to update your remote section in your configuration file. git remote rename正在尝试更新配置文件中的remote部分。 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. 它将使用给定名称的遥控器重命名为新名称,但是在您的情况下,它找不到任何名称,因此重命名失败。

But it will not do what you think; 但这不会按照您的想法进行。 it will rename your local configuration remote name and not the remote branch. 它将重命名您的本地配置远程名称,而不是远程分支。


Note Git 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中所有工作都是在本地完成的,因此建议使用上述命令去工作。


#3楼

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中远程重命名分支

See the original answer for more detail. 有关更多详细信息,请参见原始答案。


#4楼

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-name remote branch and push the new-name local branch : 删除old-name远程分支,然后推送new-name本地分支
    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


#5楼

There is no direct method, 没有直接的方法,

  1. Rename Local Branch , 重命名本地分支

    My current branch is master 我目前的分行是master

    git branch -m master_renamed #master_renamed is new name of master git branch -m master_renamed #master_renamed是master的新名称

  2. Delete remote branch, 删除远程分支,

    git push origin --delete master #origin is remote_name git push origin --delete master origin-删除 git push origin --delete master 服务器#origin是remote_name

  3. Push renamed branch into remote, 将重命名的分支推送到远程,

    git push origin master_renamed

That's it... 而已...


#6楼

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