如何在本地和远程删除 Git 分支?

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

How do I delete a Git branch locally and remotely?

gitversion-controlgit-branchgit-pushgit-remote

提问by Matthew Rankin

I want to delete a branch both locally and remotely.

我想在本地和远程删除一个分支。

Failed Attempts to Delete a Remote Branch

尝试删除远程分支失败

$ 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.

What should I do differently to successfully delete the remotes/origin/bugfixbranch both locally and remotely?

我应该怎么做才能remotes/origin/bugfix在本地和远程成功删除分支?

回答by Matthew Rankin

Executive Summary

执行摘要

$ git push -d <remote_name> <branch_name>
$ git branch -d <branch_name>

Note that in most cases the remote name is origin. In such a case you'll have to use the command like so.

请注意,在大多数情况下,远程名称是origin. 在这种情况下,您必须像这样使用命令。

$ git push -d origin <branch_name>

Delete Local Branch

删除本地分支

To delete the localbranch use one of the following:

要删除本地分支,请使用以下方法之一:

$ git branch -d branch_name
$ git branch -D branch_name

Note:The -doption is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch]

注意:-d选项是 的别名--delete,只有在其上游分支中已经完全合并的情况下才会删除该分支。您还可以使用-D,它是 的别名--delete --force,它删除“不管其合并状态如何”的分支。[来源:man git-branch]

Delete Remote Branch [Updated on 8-Sep-2017]

删除远程分支 [2017 年 9 月 8 日更新]

As of Git v1.7.0, you can delete a remotebranch using

Git v1.7.0 开始,您可以使用删除远程分支

$ git push <remote_name> --delete <branch_name>

which might be easier to remember than

这可能比

$ git push <remote_name> :<branch_name>

which was added in Git v1.5.0"to delete a remote branch or a tag."

这是在Git v1.5.0中添加的“删除远程分支或标签”。

Starting on Git v2.8.0you can also use git pushwith the -doption as an alias for --delete.

Git v2.8.0开始,您还可以使用git push-d选项作为--delete.

Therefore, the version of Git you have installed will dictate whether you need to use the easier or harder syntax.

因此,您安装的 Git 版本将决定您是否需要使用更简单或更难的语法。

Delete Remote Branch [Original Answer from 5-Jan-2010]

删除远程分支 [2010 年 1 月 5 日的原始答案]

From Chapter 3 of Pro Gitby Scott Chacon:

来自Scott Chacon的Pro Git第 3 章:

Deleting Remote Branches

Suppose you're done with a remote branch — say, you and your collaborators are finished with a feature and have merged it into your remote's master branch (or whatever branch your stable code-line is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]. If you want to delete your server-fix branch from the server, you run the following:

$ git push origin :serverfix
To [email protected]:schacon/simplegit.git
 - [deleted]         serverfix

Boom. No more branches on your server. You may want to dog-ear this page, because you'll need that command, and you'll likely forget the syntax. A way to remember this command is by recalling the git push [remotename] [localbranch]:[remotebranch]syntax that we went over a bit earlier. If you leave off the [localbranch]portion, then you're basically saying, “Take nothing on my side and make it be [remotebranch].”

删除远程分支

假设你已经完成了一个远程分支——比如说,你和你的合作者完成了一个功能并将它合并到你远程的主分支(或者你的稳定代码行所在的任何分支)。您可以使用相当笨拙的语法删除远程分支git push [remotename] :[branch]。如果要从服务器中删除 server-fix 分支,请运行以下命令:

$ git push origin :serverfix
To [email protected]:schacon/simplegit.git
 - [deleted]         serverfix

繁荣。您的服务器上不再有分支。您可能想仔细阅读此页面,因为您将需要该命令,而且您可能会忘记语法。记住这个命令的一种方法是回忆git push [remotename] [localbranch]:[remotebranch]我们之前讨论过的语法。如果你不吃那[localbranch]部分,那么你基本上是在说,“不要站在我这边,让它成为[remotebranch]。”

I issued git push origin: bugfixand it worked beautifully. Scott Chacon was right—I will want to dog earthat page (or virtually dog ear by answering this on Stack Overflow).

我发布了git push origin: bugfix,它运行得很好。Scott Chacon 是对的——我想听听那个页面(或者通过在 Stack Overflow 上回答这个问题实际上听听)。

Then you should execute this on other machines

那么你应该在其他机器上执行这个

# Fetch changes from all remotes and locally delete 
# remote deleted branches/tags etc
# --prune will do the job :-;
git fetch --all --prune

to propagate changes.

传播变化。

回答by Eric Brotto

Matthew's answeris great for removing remotebranches and I also appreciate the explanation, but to make a simple distinction between the two commands:

马修的回答非常适合删除远程分支,我也很欣赏解释,但要对这两个命令进行简单区分:

To remove a local branchfrom your machine:

从你的机器上删除本地分支

git branch -d {the_local_branch}(use -Dinstead to force deleting the branch without checking merged status)

git branch -d {the_local_branch}-D改为使用强制删除分支而不检查合并状态)

To remove a remote branchfrom the server:

从服务器删除远程分支

git push origin --delete {the_remote_branch}

git push origin --delete {the_remote_branch}

Reference: Git: Delete a branch (local or remote)

参考:Git:删除一个分支(本地或远程)

回答by Eric Brotto

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

删除远程分支

git push origin --delete <branch>  # Git version 1.7.0 or newer
git push origin -d <branch>        # Shorter version (Git 1.7.0 or newer)
git push origin :<branch>          # Git versions older than 1.7.0

Deleting a local branch

删除本地分支

git branch --delete <branch>
git branch -d <branch> # Shorter version
git branch -D <branch> # Force-delete un-merged branches

Deleting a local remote-tracking branch

删除本地远程跟踪分支

git branch --delete --remotes <remote>/<branch>
git branch -dr <remote>/<branch> # Shorter

git fetch <remote> --prune # Delete multiple obsolete remote-tracking branches
git fetch <remote> -p      # Shorter


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:

当您在本地和远程处理删除分支时,请记住涉及三个不同的分支

  1. The local branch X.
  2. The remote origin branch X.
  3. The local remote-tracking branch origin/Xthat tracks the remote branch X.
  1. 当地分公司X
  2. 远程源分支X
  3. 跟踪origin/X远程分支的本地远程跟踪分支X

Visualization of three branches

三个分支的可视化

The original poster used:

原海报使用:

git branch -rd origin/bugfix

Which only deleted his local remote-tracking branchorigin/bugfix, and not the actual remote branch bugfixon origin.

其中仅删除了本地远程跟踪分支origin/bugfix,而不是实际的远程分支bugfixorigin

Diagram 2

图二

To delete that actual remote branch, you need

要删除该实际远程分支,您需要

git push origin --delete bugfix

Diagram 3

图3

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 Xfrom the command line using a git pushwill also remove the local remote-tracking branchorigin/X, so it is not necessary to prune the obsolete remote-tracking branch with git fetch --pruneor git fetch -p. However, it wouldn't hurt if you did it anyway.

请注意,X使用 a 从命令行删除远程分支git push也将删除本地远程跟踪分支origin/X,因此没有必要使用git fetch --prune或修剪过时的远程跟踪分支git fetch -p。但是,无论如何,如果您这样做了,也不会受到伤害。

You can verify that the remote-tracking branch origin/Xwas also deleted by running the following:

您可以origin/X通过运行以下命令来验证远程跟踪分支是否也已删除:

# View just remote-tracking branches
git branch --remotes
git branch -r

# View both strictly local as well as remote-tracking branches
git branch --all
git branch -a

Pruning the obsolete local remote-tracking branch origin/X

修剪过时的本地远程跟踪分支 origin/X

If you didn't delete your remote branch Xfrom the command line (like above), then your local repository will still contain (a now obsolete) remote-tracking branch origin/X. This can happen if you deleted a remote branch directly through GitHub's web interface, for example.

如果您没有X从命令行删除远程分支(如上),那么您的本地存储库仍将包含(现已过时的)远程跟踪分支origin/X。例如,如果您直接通过 GitHub 的 Web 界面删除远程分支,就会发生这种情况。

A typical way to remove these obsolete remote-tracking branches (since Git version 1.6.6) is to simply run git fetchwith the --pruneor shorter -p. Note that this removes all obsolete local remote-tracking branches for any remote branches that no longer exist on the remote:

除去这些过时的远程跟踪分支的典型方式(因为GIT中版本1.6.6)是简单地运行git fetch--prune或更短-p请注意,这将删除远程上不再存在的任何远程分支的所有过时的本地远程跟踪分支

git fetch origin --prune
git fetch origin -p # Shorter

Here is the relevant quote from the 1.6.6 release notes(emphasis mine):

以下是1.6.6 发行说明中的相关引用(重点是我的):

"git fetch" learned--alland --multipleoptions, to run fetch from many repositories, and --pruneoption to remove remote tracking branches that went stale.These make "git remote update" and "git remote prune" less necessary (there is no plan to remove "remote update" nor "remote prune", though).

“git fetch”学习--all--multiple选项,从许多存储库运行提取,以及--prune删除过时的远程跟踪分支的选项。这些使得“git remote update”和“git remote prune”不再必要(不过,没有计划删除“remote update”或“remote prune”)。

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 operationby just manually removing the branch(es) with the --remoteor -rflags:

或者,不是通过 修剪过时的本地远程跟踪分支git fetch -p您可以通过手动删除带有--remote-r标志的分支来避免进行额外的网络操作

git branch --delete --remotes origin/X
git branch -dr origin/X # Shorter

See Also

也可以看看

回答by Alireza

Steps for deleting a branch:

删除分支的步骤:

For deleting the remote branch:

删除远程分支:

git push origin --delete <your_branch>

For deleting the local branch, you have three ways:

要删除本地分支,您有三种方法

1: git branch -D <branch_name>

2: git branch --delete --force <branch_name>  # Same as -D

3: git branch --delete  <branch_name>         # Error on unmerge

Explain:OK, just explain what's going on here!

解释:好的,解释一下这里发生了什么!

Simply do git push origin --deleteto delete your remote branch only, add the name of the branch at the end and this will delete and push it to remoteat the same time...

简单地做git push origin --delete删除远程分支在结尾处添加分支的名称,这将删除,并推到远程的同时...

Also, git branch -D, which simply delete the local branch only!...

此外,git branch -D它只是删除本地分支而已!...

-Dstands for --delete --forcewhich will delete the branch even it's not merged (force delete), but you can also use -dwhich stands for --deletewhich throw an error respective of the branch merge status...

-D代表--delete --forcewhich 将删除分支,即使它没有合并(强制删除),但您也可以使用-dwhich 代表--deletewhich 抛出分支合并状态的相应错误...

I also create the image belowto show the steps:

我还创建了下面图像来显示步骤:

Delete a remote and local branch in git

在 git 中删除远程和本地分支

回答by pagetribe

You can also use the following to delete the remote branch

也可以使用下面的删除远程分支

git push --delete origin serverfix

Which does the same thing as

做同样的事情

git push origin :serverfix

but it may be easier to remember.

但可能更容易记住。

回答by pfrenssen

Tip: When you delete branches using

提示:当您使用删除分支时

git branch -d <branchname> # Deletes local branch

or

或者

git push origin :<branchname> # Deletes remote branch

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.

仅删除引用。即使分支实际上已在远程删除,对它的引用仍然存在于团队成员的本地存储库中。这意味着对于其他团队成员,当他们执行git branch -a.

To solve this, your team members can prune the deleted branches with

为了解决这个问题,您的团队成员可以使用以下命令修剪已删除的分支

git remote prune <repository>

This is typically git remote prune origin.

这通常是git remote prune origin.

回答by Praveen Hiremath

If you want to delete a branch, first checkout to the branch other than the branch to be deleted.

如果要删除分支,首先要checkout到要删除的分支以外的分支。

git checkout other_than_branch_to_be_deleted

Deleting the local branch:

删除本地分支:

git branch -D branch_to_be_deleted

Deleting the remote branch:

删除远程分支:

git push origin --delete branch_to_be_deleted

回答by Felipe

git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>

回答by Syeful Islam

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:

要在本地和远程删除 Git 分支,首先使用以下命令删除本地分支:

git branch -d example

(Here exampleis the branch name.)

(这example是分支名称。)

And after that, delete the remote branch using this command:

之后,使用以下命令删除远程分支:

git push origin :example

回答by imanuelcostigan

Another approach is:

另一种方法是:

git push --prune origin

WARNING:This will delete all remote branches that do not exist locally.Or more comprehensively,

警告:这将删除本地不存在的所有远程分支。或者更全面地,

git push --mirror

will effectively make the remote repository look like the local copy of the repository (local heads, remotes and tags are mirrored on remote).

将有效地使远程存储库看起来像存储库的本地副本(本地头、远程和标签在远程镜像)。