git 重命名 GitHub 中的分支

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

Renaming a branch in GitHub

gitgithubbranchrename

提问by enchance

I just renamed my local branch using

我刚刚使用重命名了我的本地分支

git branch -m oldname newname

but this only renames the local version of the branch. How can I rename the one on?GitHub?

但这只会重命名分支的本地版本。我如何重命名? GitHub 上的那个?

回答by Adam Parkin

As mentioned, delete the old one on Github & re-push, though the commands used are a bit more verbose than necessary:

如前所述,在 Github 上删除旧的并重新推送,尽管使用的命令比必要的要冗长一些:

git push origin :name_of_the_old_branch_on_github
git push origin new_name_of_the_branch_that_is_local

Simple. Dissecting the commands a bit, the git push command is essentially:

简单的。稍微剖析一下命令,git push 命令本质上是:

git push <remote> <local_branch>:<remote_branch>

So doing a push with no local_branch specified essentially means "take nothing from my local repository, and make it the remote branch". I've always thought this to be completely kludgy, but it's the way it's done.

因此,在没有指定 local_branch 的情况下进行推送本质上意味着“从我的本地存储库中不取任何东西,并将其设为远程分支”。我一直认为这完全是笨拙的,但它就是这样做的。

Edit:As of Git 1.7there is an alternate syntax for deleting a remote branch:

编辑:Git 1.7 开始,有一种用于删除远程分支的替代语法:

git push origin --delete name_of_the_remote_branch

Edit:As mentioned by @void.pointer in the comments

编辑:正如@void.pointer 在评论中提到的

Note that you can combine the 2 push operations:

git push origin :old_branch new_branch

This will both delete the old branch and push the new one.

请注意,您可以组合 2 个推送操作:

git push origin :old_branch new_branch

这将删除旧分支并推送新分支。

This can be turned into a simple alias that takes the remote, original branch and new branch name as arguments, in ~/.gitconfig:

这可以变成一个简单的别名,它将远程、原始分支和新分支名称作为参数,在~/.gitconfig

[alias]
    branchm = "!git branch -m   && git push  :  -u #"

Usage:

用法:

git branchm origin old_branch new_branch

Note that positional arguments in shell commands were problematic in older (pre 2.8?) versions of git, so the alias might vary according to the git version. See this discussionfor details.

请注意,shell 命令中的位置参数在较旧(2.8 之前?)版本的 git 中存在问题,因此别名可能因 git 版本而异。有关详细信息,请参阅此讨论

回答by Taimoor Changaiz

Following commands worked for me:

以下命令对我有用:

git push origin :old-name-of-branch-on-github
git branch -m old-name-of-branch-on-github new-name-for-branch-you-want
git push origin new-name-for-branch-you-want

回答by Hazarapet Tunanyan

I've found 3 command how you can change your git branch name, and these command are faster way to do that

我找到了 3 个如何更改 git 分支名称的命令,这些命令是更快的方法

git branch -m old_branch new_branch         # Rename branch locally    
git push origin :old_branch                 # Delete the old branch    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

If you need step-by-step you can read this great article

如果您需要分步操作,您可以阅读这篇很棒的文章

How to Rename Git Local and Remote Branches

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

回答by Vi.

Just remove the old branch and create new one.

只需删除旧分支并创建新分支。

Example (solely renaming the remote branch):

示例(仅重命名远程分支):

git push origin :refs/heads/oldname
git push origin newname:refs/heads/newname

You also probably should rename local branch and change settings for where to push/pull.

您也可能应该重命名本地分支并更改推/拉位置的设置。

回答by Abdelrahman Mohamed

Rename branches in git local and remote

在 git 本地和远程重命名分支

1. Rename your local branch.

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

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.

2. 删除旧名远程分支,推送新名本地分支。

git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch.

3. 重置新名称本地分支的上游分支。

Switch to the branch and then:

切换到分支,然后:

git push origin -u new-name

So the conclusion is

所以结论是

git branch -m new-name
git push origin :old-name new-name
git push origin -u new-name

回答by rneves

You can do that without terminal, you just need to create a branch with the new name, and remove the old after. You can use this post to do that.

你可以在没有终端的情况下做到这一点,你只需要用新名称创建一个分支,然后删除旧的。你可以使用这篇文章来做到这一点。

https://github.com/blog/1377-create-and-delete-branches

https://github.com/blog/1377-create-and-delete-branches

回答by Wiley

Here is what worked for me:

这是对我有用的:

1.) Create the new branch first: git push github newname :refs/heads/newname

1.) 首先创建新分支: git push github newname :refs/heads/newname

2.) On github site, goto settings and change the Default branch to newname

2.) 在 github 站点上,转到设置并将默认分支更改为 newname

3.) Delete the oldname git push github --delete oldname

3.) 删除旧名称 git push github --delete oldname

回答by SherylHohman

In my case, I needed an additional command
git branch --unset-upstream
to get my renamed branch to push up to origin newname.

就我而言,我需要一个额外的命令 来让我重命名的分支推送到.
git branch --unset-upstream
origin newname

(For ease of typing), I first git checkout oldname.
Then run the following:

(为了方便打字),我先git checkout oldname.
然后运行以下命令:

git branch -m newname
git push origin :oldnameorgit push origin --delete oldname
git branch --unset-upstream
git push -u origin newnameorgit push origin newname

git branch -m newname
git push origin :oldnamegit push origin --delete oldname
git branch --unset-upstream
git push -u origin newnamegit push origin newname

This extra step may only be necessary because I (tend to) setup remote tracking on my branches via git push-uorigin oldname. This way when I have oldnamechecked out, I subsequently only need type git pushrather than git push origin oldname

这个额外的步骤可能只是必要的,因为我(倾向于)通过. 这样当我签出时,我随后只需要输入而不是git push-uorigin oldnameoldnamegit pushgit push origin oldname

If I do NOTuse the command git branch --unset-upstreambefore git push origin newbranch, git re-createsoldbranchand pushes newbranchto origin oldbranch-- defeating my intent.

如果我之前使用该命令,git 将重新创建并推送到-- 违背我的意图。git branch --unset-upstreamgit push origin newbrancholdbranchnewbranchorigin oldbranch

回答by Daniel Kobe

This article shows how to do it real easy.
1. To rename a local Git branch, we can use the Git branch -m command to modify the name:
git branch -m feature1 feature2
2. If you're just looking for the command to rename a remote Git branch, this is it"
git push -u origin feature2:feature3
Check that you have no tags on the branch before you do this. You can do that with git tag.

这篇文章展示了如何轻松做到这一点。
1. 要重命名本地 Git 分支,我们可以使用 Git branch -m 命令修改名称:
git branch -m feature1 feature2
2. 如果您只是在寻找重命名远程 Git 分支的命令,就是这样”
git push -u origin feature2:feature3
检查您是否有在执行此操作之前在分支上添加标签。您可以使用git tag.

回答by Pooja Mane

The following commands rename the branch locally, delete the old branch on the remote location and push the new branch, setting the local branch to track the new remote:

以下命令在本地重命名分支,删除远程位置上的旧分支并推送新分支,设置本地分支跟踪新远程:

git branch -m old_branch new_branch
git push origin :old_branch
git push --set-upstream origin new_branch