如何重命名Git本地和远程分支名称? [英] How do I rename both a Git local and remote branch name?

查看:162
本文介绍了如何重命名Git本地和远程分支名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have four branches like 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)

我尝试以下命令:

git remote rename regacy legacy

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

But Git console returned an error message to me.

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

我该如何解决这个问题?

How can I solve this problem?

推荐答案

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

There are a few ways to accomplish that:

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

重命名本地和远程

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

信用: 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还会使用新名称更新您的跟踪分支.


Important note:

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

git remote rename legacy legacy

git remote rename试图更新配置文件中的远程部分.它将使用给定名称的遥控器重命名为新名称,但是在您的情况下,它找不到任何名称,因此重命名失败.

git remote rename is 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.

但是它不会按照您的想法进行;它将重命名您的 local 配置远程名称,而不是远程分支.

But it will not do what you think; it will rename your local configuration remote name and not the remote branch. 

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

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本地和远程分支名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆