在 Git 中远程重命名分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4753888/
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-10 09:54:32  来源:igfitidea点击:

Renaming branches remotely in Git

gitbranchrename

提问by kdt

If there is a repository that I only have git://access to (and would usually just push+pull), is there a way to rename branches in that repository in the same way that I would do locally with git branch -m?

如果有一个我只能git://访问的存储库(并且通常只是推+拉),有没有办法以与我在本地使用相同的方式重命名该存储库中的分支git branch -m

回答by Sylvain Defresne

You just have to create a new local branch with the desired name, push it to your remote, and then delete the old remote branch:

您只需要使用所需名称创建一个新的本地分支,将其推送到您的远程,然后删除旧的远程分支:

$ git branch new-branch-name origin/old-branch-name
$ git push origin --set-upstream new-branch-name
$ git push origin :old-branch-name

Then, to see the old branch name, each client of the repository would have to do:

然后,要查看旧的分支名称,存储库的每个客户端都必须执行以下操作:

$ git fetch origin
$ git remote prune origin

NOTE: If your old branch is your main branch, you should change your main branch settings. Otherwise, when you run $ git push origin :old-branch-name, you'll get the error "deletion of the current branch prohibited".

注意:如果旧分支是主分支,则应更改主分支设置。否则,当您运行时 $ git push origin :old-branch-name,您将收到错误“禁止删除当前分支”

回答by sschuberth

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:

如果你真的只想远程重命名分支,而不是同时重命名任何本地分支,你可以使用一个命令来做到这一点:

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

I wrote this script (git-rename-remote-branch) which provides a handy shortcut to do the above easily.

我编写了这个脚本(git-rename-remote-branch),它提供了一个方便的快捷方式来轻松完成上述操作。

As a bash function:

作为 bash 函数:

git-rename-remote-branch(){
  if [ $# -ne 3 ]; then
    echo "Rationale : Rename a branch on the server without checking it out."
    echo "Usage     : $(basename 
git branch -m old_branch new_branch
git push -u origin new_branch
) <remote> <old name> <new name>" echo "Example : $(basename
git push origin :old_branch
) origin master release" exit 1 fi git push /:refs/heads/ : }

To integrate @ksrb's comment: What this basically does is two pushes in a single command, first git push <remote> <remote>/<old_name>:refs/heads/<new_name>to push a new remote branch based on the old remote tracking branch and then git push <remote> :<old_name>to delete the old remote branch.

整合@ksrb 的评论:这基本上是在单个命令中进行两次推送,首先git push <remote> <remote>/<old_name>:refs/heads/<new_name>基于旧的远程跟踪分支推送新的远程分支,然后git push <remote> :<old_name>删除旧的远程分支。

回答by Shashank Hegde

First checkout to the branch which you want to rename:

首先结帐到要重命名的分支:

$ git push origin :in

To remove an old branch from remote:

从 中删除旧分支remote

$ git push origin :in

回答by Lily Ballard

Sure. Just rename the branch locally, push the new branch, and push a deletion of the old.

当然。只需在本地重命名分支,推送新分支,然后推送旧分支的删除。

The only real issue is that other users of the repository won't have local tracking branches renamed.

唯一真正的问题是存储库的其他用户不会重命名本地跟踪分支。

回答by CPHPython

TL;DR

TL; 博士

"Renaming" a remote branch is actually a 2 step process (not necessarily ordered):

“重命名”远程分支实际上是一个两步过程(不一定是有序的):

  • deletion of the old remote branch (git push [space]:<old_name>as ksrb explained);
  • push into a new remote branch (difference between a couple of answers commands below).
  • 删除旧的远程分支(git push [space]:<old_name>ksrb 所解释的);
  • 推入一个新的远程分支(下面几个答案命令之间的区别)。

Deleting

删除

I use TortoiseGitand when I first tried to delete the branch through the command line, I got this:

我使用TortoiseGit,当我第一次尝试通过命令行删除分支时,我得到了这个:

git branch new-branch-name origin/old-branch-name
git push origin --set-upstream new-branch-name
git push origin :old-branch-name
  • fatal: 'origin' does not appear to be a git repository

  • fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

git checkout -b new-branch-name
git branch --unset-upstream
git push origin new-branch-name -u
git branch origin :old-branch-name
  • 致命:'origin' 似乎不是 git 存储库

  • 致命:无法从远程存储库读取。

请确保您拥有正确的访问权限并且存储库存在。

This was likely due to pageantnot having the private keyloaded (which TortoiseGitloads automatically into pageant). Moreover, I noticed that TortoiseGitcommands do not have the originref in them (e.g. git.exe push --progress "my_project" interesting_local:interesting).

这可能是因为pageant没有加载私钥TortoiseGit自动加载到pageant 中)。此外,我注意到TortoiseGit命令中没有originref(例如git.exe push --progress "my_project" interesting_local:interesting)。

I am also using Bitbucketand, as others web-based online git managers of the sort (GitHub, GitLab), I was able to delete the remote branch directly through their interface (branches page):

我也在使用Bitbucket,并且作为其他基于 Web 的在线 git 管理器(GitHub、GitLab),我能够直接通过他们的界面(分支页面)删除远程分支:

Delete branch Bitbucket

删除分支 Bitbucket

However, in TortoiseGityou may also delete remote branches through Browse References:

但是,在TortoiseGit 中,您也可以通过Browse References删除远程分支:

Browse References menu

浏览参考菜单

By right-clicking on a remote branch (remotes list) the Delete remote branchoption shows up:

通过右键单击远程分支(远程列表),将显示删除远程分支选项:

TortoiseGit remote branch delete

TortoiseGit 远程分支删除

Pushing

推动

After deleting the old remote branch I pushed directly into a new remote branch through TortoiseGitjust by typing the new name in the Remote:field of the Pushwindow and this branch was automatically created and visible in Bitbucket.

删除旧的远程分支后,我通过TortoiseGit直接推送到新的远程分支,只需在Push窗口的Remote:字段中键入新名称,该分支将自动创建并在Bitbucket 中可见。

However, if you still prefer to do it manually, a point that has not been mentioned yet in this thread is that -u= --set-upstream.

但是,如果您仍然更喜欢手动执行此操作,则该线程中尚未提及的一点是-u= --set-upstream

From git pushdocs, -uis just an alias of --set-upstream, so the commands in the answers of Sylvain (-set-upstream new-branch)and Shashank (-u origin new_branch)are equivalent, since the remote ref defaults to originif no other ref was previously defined:

来自git pushdocs-u只是 的别名--set-upstream,所以Sylvain ( -set-upstream new-branch)Shashank ( -u origin new_branch)答案中的命令是等效的,因为如果之前没有定义其他 ref ,则远程 ref默认为origin

  • git push origin -u new_branch= git push -u new_branchfrom the docs description:

    If the configuration is missing, it defaults to origin.

  • git push origin -u new_branch=git push -u new_branch来自文档描述

    如果缺少配置,则默认为origin

In the end, I did not manually type in or used any of the commands suggested by the other answers in here, so perhaps this might be useful to others in a similar situation.

最后,我没有手动输入或使用此处其他答案建议的任何命令,因此这可能对处于类似情况的其他人有用。

回答by arthur bryant

I don't know why but @Sylvain Defresne's answer does not work for me.

我不知道为什么,但@Sylvain Defresne 的回答对我不起作用。

git push origin old_branch:new_branch
git push origin :old_branch

I have to unset the upstream and then I can set the stream again. The following is how I did it.

我必须取消上游设置,然后我可以再次设置流。以下是我是如何做到的。

if git ls-remote --heads "$remote" \
    | cut -f2 \
    | sed 's:refs/heads/::' \
    | grep -q ^"$newname"$; then
    echo "Error: $newname already exists"
    exit 1
fi
git push "$oldname" "$remote/$oldname:refs/heads/$newname" ":$oldname"

回答by EpicPandaForce

I don't know if this is right or wrong, but I pushed the "old name" of the branch to the "new name" of the branch, then deleted the old branch entirely with the following two lines:

不知道是对是错,但是我把分支的“旧名称”推到了分支的“新名称”,然后用以下两行完全删除了旧分支:

##代码##

回答by Pober Wong

You can create a new branch based on old-name branch. Just like this, then delete the old branch, over!!! enter image description here

您可以基于旧名称分支创建新分支。就这样,然后删除旧分支,结束!!!在此处输入图片说明

回答by myzzzl

Adding to the answers already given, here is a version that first checks whether the new branch already exists (so you can safely use it in a script)

添加到已经给出的答案中,这是一个首先检查新分支是否已经存在的版本(因此您可以安全地在脚本中使用它)

##代码##

(the check is from this answer)

(支票来自这个答案