重命名远程 git 分支

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

Renaming remote git branch

gitgithub

提问by Nathan H

I have 2 git branches: master and experimental.

我有 2 个 git 分支:主分支和实验分支。

Experimental became good, I want to make it the master. I figured I would rename to shuffle things around, but here is what I got:

实验变好了,我要让它成为主人。我想我会重命名以调整周围的事物,但这是我得到的:

nutebook:Stuff nathan$ git remote rename master old
error: Could not rename config section 'remote.master' to 'remote.old'

I use GitHub and Git-Tower.

我使用 GitHub 和 Git-Tower。

回答by djhaskin987

The following is a guide to rename your master branch. It will work just as easily to rename your experimental branch.

以下是重命名主分支的指南。重命名您的实验分支也很容易。

Here's how I did the renaming.

First, in your working tree, locally rename master to something else.

git branch -m master old-dev

Renaming a branch does work while you are on the branch, so there's no need to checkout something else.

Then, locally rename the maintenance branch (2.63-branch) to master:

git branch -m 2.63-branch master

Now, time to mess with the remote. Just in case you screw up, you might want to make sure you have a current backup. First, delete the remote's master:

git push origin :master

And now, give the remote your new master:

git push origin master:refs/heads/master

Update: When creating a new branch, the refs/heads/ prefix is needed on the remote side. If the branch already exists (as master did above) only the branch name is required on the remote side.

... and your now-renamed old master:

git push origin old-dev:refs/heads/old-dev

Finally, delete the old name of your maintenance branch to prevent confusion:

git push origin :2.63-branch

Clients will now get the 'new' master branch when they pull.

这是我重命名的方式。

首先,在您的工作树中,在本地将 master 重命名为其他名称。

git branch -m master old-dev

当您在分支上时,重命名分支确实有效,因此无需检查其他内容。

然后,在本地将维护分支(2.63-branch)重命名为 master:

git branch -m 2.63-branch master

现在,是时候弄乱遥控器了。以防万一你搞砸了,你可能想确保你有一个当前的备份。首先,删除遥控器的主人:

git push origin :master

现在,让遥控器成为你的新主人:

git push origin master:refs/heads/master

更新:创建新分支时,远程端需要 refs/heads/ 前缀。如果分支已经存在(如上面的 master 所做的那样),则远程端只需要分支名称。

...还有你现在更名的老主人:

git push origin old-dev:refs/heads/old-dev

最后,删除维护分支的旧名称以防止混淆:

git push origin :2.63-branch

客户现在将在拉取时获得“新的”主分支。

see thissite.

看到这个网站。

回答by gracchus

I think the easiest way is to checkout the experimental branch, delete the remote master branch, then push the local experimental one as the new remote master one.

我觉得最简单的方法是checkout实验分支,删除远程master分支,然后把本地的实验分支作为新的远程master分支推送。

// delete the remote master branch by pushing null
// (the space in front of the semicolon) in this branch
git push origin :master
// push local experimental to remote master
git push origin experimental:master

回答by Jacek Lampart

If you're a Mac user, you can use the GitHub Mac App (https://mac.github.com/) to rename branches.

如果您是 Mac 用户,您可以使用 GitHub Mac App ( https://mac.github.com/) 重命名分支。

回答by knittl

masteris a branch, not a remote like originis. if you want to have your experimental work to your master branch, simply merge it in:

master是一个分支,而不是像originis那样的远程。如果你想把你的实验工作放到你的 master 分支,只需将它合并到:

git checkout master
git merge experimental

回答by Ryan Stewart

What you tried to do was rename a remote repo from "master" to "old". To rename a branch on another repo, just delete it with

您试图做的是将远程存储库从“master”重命名为“old”。要重命名另一个 repo 上的分支,只需删除它

git push <remote> :<branch name>

then push it as something else.

然后把它作为别的东西推。