如何本地和远程删除 Git 分支?
尝试删除远程分支失败:
$ git branch -d remotes/origin/bugfix
error: branch 'remotes/origin/bugfix' not found.
$ git branch -d origin/bugfix
error: branch 'origin/bugfix' not found.
$ git branch -rd origin/bugfix
Deleted remote branch origin/bugfix (was 2a14ef7).
$ git push
Everything up-to-date
$ git pull
From github.com:gituser/gitproject
* [new branch] bugfix -> origin/bugfix
Already up-to-date.
如何在本地和远程正确删除 remotes/origin/bugfix
分支?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
执行摘要
注意:在大多数情况下,
将是origin
。删除本地分支
要删除 local 分支,请使用以下选项之一:
-d
选项是--delete 的别名
,仅当分支已完全合并到其上游分支时才会删除该分支。-D
选项是--delete --force
的别名,它删除分支“无论其合并状态如何”。 [来源:man git-branch
]gitbranch -d
(删除)学会了遵守-f
(强制)标志。删除远程分支
自 Git v1.7.0 起,您可以删除远程分支,使用
更容易记住
它可能比在 Git v1.5.0 “删除远程分支或标签。”
从 Git v2.8.0 开始,您可以还可以使用
git push
和-d
选项作为--delete
的别名。因此,您安装的 Git 版本将决定您是否需要使用更简单或更难的语法。删除远程分支 [2010 年 1 月 5 日的原始答案]
来自 第 3 章Pro Git 作者:Scott Chacon:
我运行了 git push origin :bugfix ,效果非常好。 Scott Chacon 是对的——我想要狗耳那个页面(或者实际上是狗耳) -通过在 Stack Overflow 上回答这个问题)。
最后,在其他机器上执行以下命令来传播更改:
Executive Summary
Note: In most cases,
<remote_name>
will beorigin
.Delete Local Branch
To delete the local branch, use one of the following:
-d
option is an alias for--delete
, which only deletes the branch if it has already been fully merged in its upstream branch.-D
option is an alias for--delete --force
, which deletes the branch "irrespective of its merged status." [Source:man git-branch
]git branch -d
(delete) learned to honor the-f
(force) flag.Delete Remote Branch
As of Git v1.7.0, you can delete a remote branch using
which might be easier to remember than
which was added in Git v1.5.0 "to delete a remote branch or a tag."
Starting with Git v2.8.0, you can also use
git push
with the-d
option as an alias for--delete
. Therefore, the version of Git you have installed will dictate whether you need to use the easier or harder syntax.Delete Remote Branch [Original Answer from 5-Jan-2010]
From Chapter 3 of Pro Git by Scott Chacon:
I ran
git push origin :bugfix
, and it worked beautifully. Scott Chacon was right—I will want to dog-ear that page (or virtually dog ear-by answering this on Stack Overflow).Finally, execute the following on other machines to propagate changes:
Matthew 的回答对于删除远程分支非常有用,我也很欣赏这个解释,但是要做一个两个命令之间的简单区别:
从计算机中删除本地分支:
gitbranch -d {local_branch}
(使用< code>-D 而不是强制删除分支而不检查合并状态);从服务器删除远程分支:
git push origin -d {remote_branch}
。参考: Git:删除分支(本地或远程)。
Matthew’s answer is great for removing remote branches and I also appreciate the explanation, but to make a simple distinction between the two commands:
to remove a local branch from your machine:
git branch -d {local_branch}
(use-D
instead to force deleting the branch without checking merged status);to remove a remote branch from the server:
git push origin -d {remote_branch}
.Reference: Git: Delete a branch (local or remote).
简短答案
如果您想了解以下命令的更详细说明,请参阅下一节中的长答案。
删除远程分支
删除本地分支
删除本地远程跟踪分支
长答案:需要删除三个不同的分支!
当您处理本地和远程删除分支时,请记住涉及三个不同的分支:
X
。X
。X
的本地远程跟踪分支origin/X
。原始发布者使用:
仅删除了他的本地远程跟踪分支 < code>origin/bugfix,而不是
origin
上的实际远程分支bugfix
。要删除实际的远程分支,您需要
其他详细信息
以下部分介绍了删除远程和远程跟踪分支时要考虑的其他详细信息。
推送删除远程分支也会删除远程跟踪分支
请注意,使用 git push 从命令行删除远程分支 X 也会删除本地远程分支跟踪分支
origin/X
,因此无需使用git fetch --prune
或git fetch - 修剪过时的远程跟踪分支p
。不过,无论如何,如果你这样做了,也没什么坏处。您可以通过运行以下命令来验证远程跟踪分支
origin/X
是否也被删除:修剪过时的本地远程跟踪分支 origin/X
如果您没有删除远程分支
X
从命令行(如上),那么您的本地存储库仍将包含(现已废弃的)远程跟踪分支origin/X
。例如,如果您直接通过 GitHub 的 Web 界面删除远程分支,则可能会发生这种情况。删除这些过时的远程跟踪分支(自 Git 版本 1.6.6 起)的典型方法是简单地使用
--prune
或更短的- 运行
。 请注意,这会删除远程上不再存在的任何远程分支的所有过时的本地远程跟踪分支:git fetch
p以下是 1.6.6 发行说明 (强调我的):
对于过时的远程跟踪分支的上述自动修剪的替代方案
或者,不通过 git fetch -p 修剪过时的本地远程跟踪分支,您可以通过使用
--remotes
或-r
标志手动删除分支来避免进行额外的网络操作:另请参阅
The short answers
If you want more detailed explanations of the following commands, then see the long answers in the next section.
Deleting a remote branch
Deleting a local branch
Deleting a local remote-tracking branch
The long answer: there are three different branches to delete!
When you're dealing with deleting branches both locally and remotely, keep in mind that there are three different branches involved:
X
.X
.origin/X
that tracks the remote branchX
.The original poster used:
Which only deleted his local remote-tracking branch
origin/bugfix
, and not the actual remote branchbugfix
onorigin
.To delete that actual remote branch, you need
Additional details
The following sections describe additional details to consider when deleting your remote and remote-tracking branches.
Pushing to delete remote branches also removes remote-tracking branches
Note that deleting the remote branch
X
from the command line using agit push
will also remove the local remote-tracking branchorigin/X
, so it is not necessary to prune the obsolete remote-tracking branch withgit fetch --prune
orgit fetch -p
. However, it wouldn't hurt if you did it anyway.You can verify that the remote-tracking branch
origin/X
was also deleted by running the following:Pruning the obsolete local remote-tracking branch origin/X
If you didn't delete your remote branch
X
from the command line (like above), then your local repository will still contain (a now obsolete) remote-tracking branchorigin/X
. This can happen if you deleted a remote branch directly through GitHub's web interface, for example.A typical way to remove these obsolete remote-tracking branches (since Git version 1.6.6) is to simply run
git fetch
with the--prune
or shorter-p
. Note that this removes all obsolete local remote-tracking branches for any remote branches that no longer exist on the remote:Here is the relevant quote from the 1.6.6 release notes (emphasis mine):
Alternative to above automatic pruning for obsolete remote-tracking branches
Alternatively, instead of pruning your obsolete local remote-tracking branches through
git fetch -p
, you can avoid making the extra network operation by just manually removing the branch(es) with the--remotes
or-r
flags:See Also
删除分支的步骤:
对于删除远程分支:
对于删除本地分支,有三种方式:
说明:< /strong> 好吧,解释一下这里发生了什么!
只需执行
git push origin --delete
即可仅删除您的远程分支,在末尾添加分支名称,这将删除并同时将其推送到远程...此外,
gitbranch -D
,它仅仅<删除本地分支/strong>!...-D
代表--delete --force
它将删除分支,即使它没有合并(强制删除),但你也可以使用-d
代表--delete
,它会抛出与分支合并状态相关的错误...我还创建了下面的图像来显示步骤:
Steps for deleting a branch:
For deleting the remote branch:
For deleting the local branch, you have three ways:
Explain: OK, just explain what's going on here!
Simply do
git push origin --delete
to delete your remote branch only, add the name of the branch at the end and this will delete and push it to remote at the same time...Also,
git branch -D
, which simply delete the local branch only!...-D
stands for--delete --force
which will delete the branch even it's not merged (force delete), but you can also use-d
which stands for--delete
which throw an error respective of the branch merge status...I also create the image below to show the steps:
您还可以使用以下命令删除远程分支,
其功能与此相同,
但可能更容易记住。
You can also use the following to delete the remote branch
Which does the same thing as
but it may be easier to remember.
很简单:
删除远程分支
或者
-- 也可以用这种语法删除标签
强制删除本地分支
注意:删除远程分支后,在其他计算机上执行 git fetch --all --prune ,以删除过时的跟踪分支。
示例
删除本地分支
以删除远程分支
使用新版本的 git,也可以使用
提示: 删除分支
如果你想查看所有可用的分支,你可以使用 gitbranch -a ,
而只想查看远程分支,你可以使用 gitbranch -r 。
It's very simple:
To delete the remote branch
Or
-- You can also delete tags with this syntax
To forcefully delete local branch
Note: do a
git fetch --all --prune
on other machines after deleting remote branch, to remove obsolete tracking branches.Example
to remove local branch
to remove remote branch
With the new version of git, its also possible to remove branch with
TIP:
if you want to see all available branches you can use
git branch -a
,and to see just remote branches, you can use
git branch -r
提示:当您使用删除分支时
,或
仅删除引用。即使分支实际上在远程被删除,对它的引用仍然存在于团队成员的本地存储库中。这意味着对于其他团队成员来说,当他们执行 gitbranch -a 时,已删除的分支仍然可见。
来修剪已删除的分支。
为了解决这个问题,您的团队成员可以使用这通常是
git remote prune origin
Tip: When you delete branches using
or
only the references are deleted. Even though the branch is actually removed on the remote, the references to it still exists in the local repositories of your team members. This means that for other team members the deleted branches are still visible when they do a
git branch -a
.To solve this, your team members can prune the deleted branches with
This is typically
git remote prune origin
.如果要删除分支,请先检出要删除的分支以外的分支。
删除本地分支:
删除远程分支:
If you want to delete a branch, first checkout to the branch other than the branch to be deleted.
Deleting the local branch:
Deleting the remote branch:
这很简单:只需运行以下命令:
要在本地和远程删除 Git 分支,首先使用以下命令删除本地分支:(
这里
example
是分支名称。)然后删除使用此命令的远程分支:
This is simple: Just run the following command:
To delete a Git branch both locally and remotely, first delete the local branch using this command:
(Here
example
is the branch name.)And after that, delete the remote branch using this command:
另一种方法是:
警告: 这将删除本地不存在的所有远程分支。或更全面地说,
将有效地使远程存储库看起来就像存储库的本地副本(本地头、远程和标签镜像到远程)。
Another approach is:
WARNING: This will delete all remote branches that do not exist locally. Or more comprehensively,
will effectively make the remote repository look like the local copy of the repository (local heads, remotes and tags are mirrored on remote).
我在我的 Bash 设置中使用以下内容:
然后您可以调用:
I use the following in my Bash settings:
Then you can call:
本地删除:
要删除本地分支,可以使用:
要强制删除分支,请使用
-D
而不是-d
。远程删除:
有两种选择:
我建议您使用第二种方式,因为它更直观。
Delete locally:
To delete a local branch, you can use:
To delete a branch forcibly, use
-D
instead of-d
.Delete remotely:
There are two options:
I would suggest you use the second way as it is more intuitive.
如果您想使用单个命令完成这两个步骤,您可以通过将以下内容添加到您的
~/.gitconfig
中来为其创建一个别名:或者,您可以将其添加到您的全局配置中命令行使用
注意:如果使用
-d
(小写d),则只有在合并后分支才会被删除。要强制删除,您需要使用-D
(大写 D)。If you want to complete both these steps with a single command, you can make an alias for it by adding the below to your
~/.gitconfig
:Alternatively, you can add this to your global configuration from the command line using
NOTE: If using
-d
(lowercase d), the branch will only be deleted if it has been merged. To force the delete to happen, you will need to use-D
(uppercase D).自 2013 年 1 月起,GitHub 在“分支”页面中每个分支旁边包含一个删除分支按钮。
相关博客文章:创建和删除分支
Since January 2013, GitHub included a Delete branch button next to each branch in your "Branches" page.
Relevant blog post: Create and delete branches
本地和远程删除您的分支
签出到 master 分支 -
git checkout master
删除您的远程分支分支 -
git Push origin --delete
gitbranch --delete
To delete your branch locally and remotely
Checkout to master branch -
git checkout master
Delete your remote branch -
git push origin --delete <branch-name>
Delete your local branch -
git branch --delete <branch-name>
您还可以使用 git remote prune origin 来执行此操作,
它会从 gitbranch -r 列表中修剪和删除远程跟踪分支。
You can also do this using
git remote prune origin
It prunes and deletes remote-tracking branches from a
git branch -r
listing.除了其他答案之外,我还经常使用 git_remote_branch 工具。这是一个额外的安装,但它为您提供了一种与远程分支交互的便捷方式。在这种情况下,要删除:
我发现我也经常使用
publish
和track
命令。In addition to the other answers, I often use the git_remote_branch tool. It's an extra install, but it gets you a convenient way to interact with remote branches. In this case, to delete:
I find that I also use the
publish
andtrack
commands quite often.用于删除本地和远程的one-liner命令:
或者将下面的别名添加到您的~/.gitconfig中。用法:
git Kill 分支名称
A one-liner command to delete both local, and remote:
Or add the alias below to your ~/.gitconfig. Usage:
git kill branch-name
删除分支
对于删除远程分支:
Deleting Branches
And for deleting the remote branch:
本地删除 -(正常)
如果您的分支处于变基/合并过程中并且未正确完成,则意味着您将收到错误,
Rebase /合并正在进行中
,因此在这种情况下,您将无法删除您的分支。所以要么你需要解决变基/合并问题。否则,您可以使用以下方式强制删除,
要在远程中删除:
您可以使用以下方式执行相同操作:
图形表示:
To delete locally - (normal)
If your branch is in a rebasing/merging progress and that was not done properly, it means you will get an error,
Rebase/Merge in progress
, so in that case, you won't be able to delete your branch.So either you need to solve the rebasing/merging. Otherwise, you can do force delete by using,
To delete in remote:
You can do the same using:
Graphical representation:
删除远程分支
git push origin :
删除本地分支
gitbranch -D
删除本地分支步骤:
Delete remote branch
git push origin :<branchname>
Delete local branch
git branch -D <branchname>
Delete local branch steps:
简单地说:
Simply say:
现在,您可以使用 GitHub Desktop 应用程序来完成此操作。
启动应用程序后,
切换到您要删除的分支
从“分支”菜单中,选择“取消发布...”以删除分支来自 GitHub 服务器。
从“分支”菜单中,选择“删除“branch_name”...”,以从本地计算机(也称为您所在的计算机)中删除分支目前正在研究)
Now you can do it with the GitHub Desktop application.
After launching the application
Switch to the branch you would like to delete
From the "Branch" menu, select, "Unpublish...", to have the branch deleted from the GitHub servers.
From the "Branch" menu, select, 'Delete "branch_name"...', to have the branch deleted off of your local machine (AKA the machine you are currently working on)
比更容易记住
is easier to remember than
如果您有一个与远程分支同名的标签,则此操作将不起作用:
在这种情况下,您需要指定要删除该分支,不是标签:
同样,要删除标签而不是分支,您可以使用:
This won't work if you have a tag with the same name as the branch on the remote:
In that case you need to specify that you want to delete the branch, not the tag:
Similarly, to delete the tag instead of the branch you would use:
许多其他答案都会导致错误/警告。这种方法相对简单,但例如,如果它没有完全合并到
some_other_branch
中,您可能仍然需要gitbranch -Dbranch_to_delete
。如果删除了远程分支,则不需要远程修剪。它仅用于获取您正在跟踪的存储库上可用的最新遥控器。我观察到 git fetch 会添加遥控器,而不是删除它们。以下是 git Remote prune origin 何时实际执行某些操作的示例:
用户 A 执行上述步骤。用户 B 将运行以下命令来查看最新的远程分支:
Many of the other answers will lead to errors/warnings. This approach is relatively fool proof although you may still need
git branch -D branch_to_delete
if it's not fully merged intosome_other_branch
, for example.Remote pruning isn't needed if you deleted the remote branch. It's only used to get the most up-to-date remotes available on a repository you're tracking. I've observed
git fetch
will add remotes, not remove them. Here's an example of whengit remote prune origin
will actually do something:User A does the steps above. User B would run the following commands to see the most up-to-date remote branches:
根据使用终端的最新文档我们可以通过以下方式删除。
本地删除:
远程删除:
According to the latest document using a terminal we can delete in the following way.
Delete in local:
Delete in remote location:
我厌倦了在谷歌上搜索这个答案,所以我对 crizCraig 之前发布的答案采取了类似的方法。
我将以下内容添加到我的 Bash 配置文件中:
然后每次完成分支(例如合并到
master
中)时,我都会在终端中运行以下命令:...然后删除
my-branch-name
来自origin
以及本地。I got sick of googling for this answer, so I took a similar approach to the answer that crizCraig posted earlier.
I added the following to my Bash profile:
Then every time I'm done with a branch (merged into
master
, for example) I run the following in my terminal:...which then deletes
my-branch-name
fromorigin
as as well as locally.使用:
如果您确定要删除它,请运行
立即清理已删除的远程分支 run
Use:
If you are sure you want to delete it, run
Now to clean up deleted remote branches run