如何重命名本地 Git 分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6591213/
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
How do I rename a local Git branch?
提问by Forrest
I don't want to rename a remote branch, as described in Rename master branch for both local and remote Git repositories.
我不想重命名远程分支,如重命名本地和远程 Git 存储库的主分支中所述。
How can I rename a local branch which hasn't been pushed to a remote branch?
如何重命名尚未推送到远程分支的本地分支?
In case you need to rename remotebranch as well:
How do I rename both a Git local and remote branch name
如果您还需要重命名远程分支:
如何重命名 Git 本地和远程分支名称
回答by siride
If you want to rename a branch while pointed to any branch, do:
如果要在指向任何分支时重命名分支,请执行以下操作:
git branch -m <oldname> <newname>
If you want to rename the current branch, you can do:
如果要重命名当前分支,可以执行以下操作:
git branch -m <newname>
A way to remember this is -m
is for "move" (or mv
), which is how you rename files. Adding an alias could also help. To do so, run the following:
记住这一点的一种方法-m
是“移动”(或mv
),这是您重命名文件的方式。添加别名也有帮助。为此,请运行以下命令:
git config --global alias.rename 'branch -m'
If you are on Windows or another case-insensitive filesystem, and there are only capitalization changes in the name, you need to use -M
, otherwise, git will throw branch already existserror:
如果您在 Windows 或其他不区分大小写的文件系统上,并且名称中只有大小写更改,则需要使用-M
,否则,git 将抛出branch already exists错误:
git branch -M <newname>
回答by Madhan Ayyasamy
git branch -m old_branch_name new_branch_name
The above command will change your branch name, but you have to be very careful using the renamed branch, because it will still refer to the old upstream branch associated with it, if any.
上面的命令将更改您的分支名称,但是您必须非常小心地使用重命名的分支,因为它仍然会引用与其关联的旧上游分支(如果有)。
If you want to push some changes into master after your local branch is renamed into new_branch_name (example name):
如果您想在本地分支重命名为 new_branch_name(示例名称)后将一些更改推送到 master:
git push origin new_branch_name:master
(now changes will go to master branch but your local branch name is new_branch_name)
git push origin new_branch_name:master
(现在更改将转到主分支,但您的本地分支名称是 new_branch_name)
For more details, see "How to rename your local branch name in Git."
更多详细信息,请参阅“如何在 Git 中重命名本地分支名称”。
回答by Jonathan
To rename your current branch:
要重命名当前分支:
git branch -m <newname>
回答by Milind Anantwar
Here are the steps to rename the branch:
以下是重命名分支的步骤:
- Switch to the branch which needs to be renamed
git branch -m <new_name>
git push origin :<old_name>
git push origin <new_name>:refs/heads/<new_name>
- 切换到需要重命名的分支
git branch -m <new_name>
git push origin :<old_name>
git push origin <new_name>:refs/heads/<new_name>
EDIT (12/01/2017):Make sure you run command git status
and check that the newly created branch is pointing to its own ref and not the older one. If you find the reference to the older branch, you need to unset the upstream using:
编辑(12/01/2017):确保您运行命令git status
并检查新创建的分支是否指向自己的引用而不是旧的引用。如果找到对旧分支的引用,则需要使用以下方法取消上游设置:
git branch --unset-upstream
回答by trungk18
Rename the branch will be useful once your branch is finished. Then new stuff is coming, and you want to develop in the same branch instead of deleting it and create the new one.
分支完成后,重命名分支将很有用。然后新的东西来了,你想在同一个分支中开发而不是删除它并创建新的。
From my experience, to rename a local and remote branch in Git you should do the following steps.
根据我的经验,要在 Git 中重命名本地和远程分支,您应该执行以下步骤。
Quoting from Multiple States - Rename a local and remote branch in 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.为新名称的本地分支重置上游分支
git push origin -u new-name
回答by Vanchev
The answers so far have been correct, but here is some additional information:
到目前为止,答案都是正确的,但这里有一些额外的信息:
One can safely rename a branch with '-m' (move), but one has to be careful with '-M', because it forces the rename, even if there is an existing branch with the same name already. Here is the excerpt from the 'git-branch' man page:
可以安全地使用“-m”(移动)重命名分支,但必须小心使用“-M”,因为它会强制重命名,即使已经存在同名的现有分支。以下是“git-branch”手册页的摘录:
With a -m or -M option,
<oldbranch>
will be renamed to<newbranch>
. If<oldbranch>
had a corresponding reflog, it is renamed to match<newbranch>
, and a reflog entry is created to remember the branch renaming. If<newbranch>
exists, -M must be used to force the rename to happen.
使用 -m 或 -M 选项,
<oldbranch>
将重命名为<newbranch>
. 如果<oldbranch>
有相应的 reflog,则将其重命名为 match<newbranch>
,并创建一个 reflog 条目以记住分支重命名。如果<newbranch>
存在,必须使用 -M 来强制重命名发生。
回答by Oss
1. Rename
1.重命名
If it is your current branch, just do
如果它是您当前的分支,请执行
git branch -m new_name
If it is another branch you want to rename
如果它是您要重命名的另一个分支
git branch -m old_name new_name
2. Track a new remote branch
2. 跟踪一个新的远程分支
- If your branch was pushed, then after renaming you need to delete it from the remote Git repository and ask your new local to track a new remote branch:
- 如果你的分支被推送,那么在重命名后你需要从远程 Git 存储库中删除它并让你的新本地跟踪一个新的远程分支:
git push origin :old_name
git push --set-upstream origin new_name
回答by Samuel Meacham
I foolishly named a branch starting with a hyphen, and then checked out master. I didn't want to deletemy branch, I had work in it.
我愚蠢地以连字符开头命名了一个分支,然后检查了 master。我不想删除我的分支,我在里面工作。
Neither of these worked:
这些都不起作用:
git checkout -dumb-name
git checkout -dumb-name
git checkout -- -dumb-name
git checkout -- -dumb-name
"
s, '
s and \
s didn't help either. git branch -m
doesn't work.
"
s、'
s 和\
s 也没有帮助。 git branch -m
不起作用。
Here's how I finally fixed it. Go into your working copy's .git/refs/heads, find the filename "-dumb-name", get the hash of the branch. Then this will check it out, make a new branch with a sane name, and delete the old one.
这是我最终修复它的方法。进入工作副本的 .git/refs/heads,找到文件名“-dumb-name”,获取分支的哈希值。然后这将检查它,使用合理的名称创建一个新分支,并删除旧分支。
git checkout {hash}
git checkout -b brilliant-name
git branch -d -- -dumb-name
回答by aliasav
To rename a branch locally:
要在本地重命名分支:
git branch -m [old-branch] [new-branch]
Now you'll have to propagate these changes on your remote server as well.
现在,您还必须在远程服务器上传播这些更改。
To push changes of the deleted old branch:
要推送已删除旧分支的更改:
git push origin :[old-branch]
To push changes of creation of new branch:
要推送新分支创建的更改:
git push origin [new-branch]
回答by Harry_pb
Just three steps to replicate change in name on remote
as well as on GitHub:
只需三个步骤即可remote
在 GitHub上和在 GitHub上复制名称更改:
Step 1 git branch -m old_branchname new_branchname
第1步 git branch -m old_branchname new_branchname
Step 2 git push origin :old_branchname new_branchname
第2步 git push origin :old_branchname new_branchname
Step 3 git push --set-upstream origin new_branchname
第 3 步 git push --set-upstream origin new_branchname