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

发布于 2024-08-16 23:58:28 字数 527 浏览 6 评论 0 原文

尝试删除远程分支失败:

$ 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 分支?

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.

How do I properly delete the remotes/origin/bugfix branch both locally and remotely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

千仐 2024-08-23 23:58:28

执行摘要

git push -d <remote_name> <branchname>   # Delete remote
git branch -d <branchname>               # Delete local

注意:在大多数情况下, 将是origin

删除本地分支

要删除 local 分支,请使用以下选项之一:

git branch -d <branch_name>
git branch -D <branch_name>
  • -d 选项是 --delete 的别名,仅当分支已完全合并到其上游分支时才会删除该分支。
  • -D 选项是 --delete --force 的别名,它删除分支“无论其合并状态如何”。 [来源:man git-branch]
  • Git v2.3gitbranch -d(删除)学会了遵守-f(强制)标志。
  • 如果您尝试删除当前选定的分支,您将收到错误消息。

删除远程分支

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

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

更容易记住

$ git push <remote_name> :<branch_name>

它可能比在 Git v1.5.0 “删除远程分支或标签。”

Git v2.8.0 开始,您可以还可以使用 git push-d 选项作为 --delete 的别名。因此,您安装的 Git 版本将决定您是否需要使用更简单或更难的语法。

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

来自 第 3 章Pro Git 作者:Scott Chacon:

删除远程分支

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

$ git Push origin :serverfix
至[电子邮件受保护]:schacon/simplegit.git
 - [删除]服务器修复

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

我运行了 git push origin :bugfix ,效果非常好。 Scott Chacon 是对的——我想要狗耳那个页面(或者实际上是狗耳) -通过在 Stack Overflow 上回答这个问题)。

最后,在其他机器上执行以下命令来传播更改:

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

Executive Summary

git push -d <remote_name> <branchname>   # Delete remote
git branch -d <branchname>               # Delete local

Note: In most cases, <remote_name> will be origin.

Delete Local Branch

To delete the local branch, use one of the following:

git branch -d <branch_name>
git branch -D <branch_name>
  • The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch.
  • The -D option is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch]
  • As of Git v2.3, git branch -d (delete) learned to honor the -f (force) flag.
  • You will receive an error if you try to delete the currently selected branch.

Delete Remote Branch

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

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

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:

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 main 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 serverfix 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].”

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:

# Fetch changes from all remotes and locally delete 
# remote deleted branches/tags etc
# --prune will do the job :-;
git fetch --all --prune
流绪微梦 2024-08-23 23:58:28

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

荒岛晴空 2024-08-23 23:58:28

简短答案

如果您想了解以下命令的更详细说明,请参阅下一节中的长答案。

删除远程分支

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

删除本地分支

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

删除本地远程跟踪分支

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

长答案:需要删除三个不同的分支!

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

  1. 本地分支X
  2. 远程源分支 X
  3. 跟踪远程分支 X 的本地远程跟踪分支 origin/X

三个分支的可视化

原始发布者使用:

git branch -rd origin/bugfix

仅删除了他的本地远程跟踪分支 < code>origin/bugfix,而不是 origin 上的实际远程分支 bugfix

Diagram 2

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

git push origin --delete bugfix

图 3

其他详细信息

以下部分介绍了删除远程和远程跟踪分支时要考虑的其他详细信息。

推送删除远程分支也会删除远程跟踪分支

请注意,使用 git push 从命令行删除远程分支 X 也会删除本地远程分支跟踪分支 origin/X,因此无需使用 git fetch --prunegit fetch - 修剪过时的远程跟踪分支p。不过,无论如何,如果你这样做了,也没什么坏处。

您可以通过运行以下命令来验证远程跟踪分支 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

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

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

删除这些过时的远程跟踪分支(自 Git 版本 1.6.6 起)的典型方法是简单地使用 --prune 或更短的 - 运行 git fetch p请注意,这会删除远程上不再存在的任何远程分支的所有过时的本地远程跟踪分支

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

以下是 1.6.6 发行说明 (强调我的):

“git fetch”学习了 --all--multiple 选项,用于运行 fetch
许多存储库,以及用于删除远程跟踪的 --prune 选项
过时的分支。
这些使得“git 远程更新”和“git
远程修剪”不太必要(没有计划删除“远程
不过,更新”也不是“远程修剪”)。

对于过时的远程跟踪分支的上述自动修剪的替代方案

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

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

另请参阅

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/X that tracks the remote branch X.

Visualization of three branches

The original poster used:

git branch -rd origin/bugfix

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

Diagram 2

To delete that actual remote branch, you need

git push origin --delete bugfix

Diagram 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 X from the command line using a git push will also remove the local remote-tracking branch origin/X, so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune or git 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:

# 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

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 branch origin/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:

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

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

"git fetch" learned --all and --multiple options, to run fetch from
many repositories, and --prune option 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).

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:

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

See Also

浮生面具三千个 2024-08-23 23:58:28

删除分支的步骤:

对于删除远程分支

git push origin --delete <your_branch>

对于删除本地分支,有三种方式

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

说明:< /strong> 好吧,解释一下这里发生了什么!

只需执行 git push origin --delete 即可仅删除您的远程分支,在末尾添加分支名称,这将删除并同时将其推送到远程...

此外,gitbranch -D,它仅<删除本地分支/strong>!...

-D 代表 --delete --force 它将删除分支,即使它没有合并(强制删除),但你也可以使用-d 代表 --delete ,它会抛出与分支合并状态相关的错误...

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

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

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 --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:

Delete a remote and local branch in git

蒲公英的约定 2024-08-23 23:58:28

您还可以使用以下命令删除远程分支,

git push --delete origin serverfix

其功能与此相同,

git push origin :serverfix

但可能更容易记住。

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.

翻了热茶 2024-08-23 23:58:28

很简单:

删除远程分支

git push -d origin <branch-name>

或者

git push origin :<branch-name>

-- 也可以用这种语法删除标签

强制删除本地分支

git branch -D <branch-name>

注意:删除远程分支后,在其他计算机上执行 git fetch --all --prune ,以删除过时的跟踪分支。

示例

删除本地分支

git branch -D my-local-branch

以删除远程分支

git push origin :my-remote-branch

使用新版本的 git,也可以使用

git push origin --delete <branch_name>

提示: 删除分支
如果你想查看所有可用的分支,你可以使用 gitbranch -a ,

而只想查看远程分支,你可以使用 gitbranch -r 。

It's very simple:

To delete the remote branch

git push -d origin <branch-name>

Or

git push origin :<branch-name>

-- You can also delete tags with this syntax

To forcefully delete local branch

git branch -D <branch-name>

Note: do a git fetch --all --prune on other machines after deleting remote branch, to remove obsolete tracking branches.

Example

to remove local branch

git branch -D my-local-branch

to remove remote branch

git push origin :my-remote-branch

With the new version of git, its also possible to remove branch with

git push origin --delete <branch_name>

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

只有影子陪我不离不弃 2024-08-23 23:58:28

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

git branch -d <branchname> # Deletes local branch

,或

git push origin :<branchname> # Deletes remote branch

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

来修剪已删除的分支。

git remote prune <repository>

为了解决这个问题,您的团队成员可以使用这通常是git remote prune origin

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.

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

git remote prune <repository>

This is typically git remote prune origin.

羁绊已千年 2024-08-23 23:58:28

如果要删除分支,请先检出要删除的分支以外的分支。

git checkout other_than_branch_to_be_deleted

删除本地分支:

git branch -D branch_to_be_deleted

删除远程分支:

git push origin --delete branch_to_be_deleted

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

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
谁把谁当真 2024-08-23 23:58:28
git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>
git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>
人心善变 2024-08-23 23:58:28

这很简单:只需运行以下命令:

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

git branch -d example

这里 example 是分支名称。)

然后删除使用此命令的远程分支:

git push origin :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:

git branch -d example

(Here example is the branch name.)

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

git push origin :example
揽月 2024-08-23 23:58:28

另一种方法是:

git push --prune origin

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

git push --mirror

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

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

尽揽少女心 2024-08-23 23:58:28

我在我的 Bash 设置中使用以下内容:

alias git-shoot="git push origin --delete"

然后您可以调用:

git-shoot branchname

I use the following in my Bash settings:

alias git-shoot="git push origin --delete"

Then you can call:

git-shoot branchname
岁月无声 2024-08-23 23:58:28

本地删除:

要删除本地分支,可以使用:

git branch -d <branch_name>

要强制删除分支,请使用-D而不是-d

git branch -D <branch_name>

远程删除:

有两种选择:

git push origin :branchname

git push origin --delete branchname

我建议您使用第二种方式,因为它更直观。

Delete locally:

To delete a local branch, you can use:

git branch -d <branch_name>

To delete a branch forcibly, use -D instead of -d.

git branch -D <branch_name>

Delete remotely:

There are two options:

git push origin :branchname

git push origin --delete branchname

I would suggest you use the second way as it is more intuitive.

千仐 2024-08-23 23:58:28

如果您想使用单个命令完成这两个步骤,您可以通过将以下内容添加到您的 ~/.gitconfig 中来为其创建一个别名:

[alias]
    rmbranch = "!f(){ git branch -d ${1} && git push origin --delete ${1}; };f"

或者,您可以将其添加到您的全局配置中命令行使用

git config --global alias.rmbranch \
'!f(){ git branch -d ${1} && git push origin --delete ${1}; };f'

注意:如果使用-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:

[alias]
    rmbranch = "!f(){ git branch -d ${1} && git push origin --delete ${1}; };f"

Alternatively, you can add this to your global configuration from the command line using

git config --global alias.rmbranch \
'!f(){ git branch -d ${1} && git push origin --delete ${1}; };f'

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

请帮我爱他 2024-08-23 23:58:28

自 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

眼中杀气 2024-08-23 23:58:28

本地和远程删除您的分支

  • 签出到 master 分支 - git checkout master

  • 删除您的远程分支分支 - git Push origin --delete

  • < p>删除本地分支 - 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>

青萝楚歌 2024-08-23 23:58:28

您还可以使用 git remote prune origin 来执行此操作,

$ git remote prune origin
Pruning origin
URL: [email protected]/yourrepo.git
 * [pruned] origin/some-branchs

它会从 gitbranch -r 列表中修剪和删除远程跟踪分支。

You can also do this using git remote prune origin

$ git remote prune origin
Pruning origin
URL: [email protected]/yourrepo.git
 * [pruned] origin/some-branchs

It prunes and deletes remote-tracking branches from a git branch -r listing.

帅气称霸 2024-08-23 23:58:28

除了其他答案之外,我还经常使用 git_remote_branch 工具。这是一个额外的安装,但它为您提供了一种与远程分支交互的便捷方式。在这种情况下,要删除:

grb delete branch

我发现我也经常使用 publishtrack 命令。

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:

grb delete branch

I find that I also use the publish and track commands quite often.

下壹個目標 2024-08-23 23:58:28

用于删除本地和远程的one-liner命令:

D=branch-name; git branch -D $D; git push origin :$D

或者将下面的别名添加到您的~/.gitconfig中。用法:git Kill 分支名称

[alias]
    kill = "!f(){ git branch -D \"$1\";  git push origin --delete \"$1\"; };f"

A one-liner command to delete both local, and remote:

D=branch-name; git branch -D $D; git push origin :$D

Or add the alias below to your ~/.gitconfig. Usage: git kill branch-name

[alias]
    kill = "!f(){ git branch -D \"$1\";  git push origin --delete \"$1\"; };f"
明媚殇 2024-08-23 23:58:28

删除分支

假设我们在分支“contact-form”上的工作已经完成,并且我们已经将其集成到“master”中。由于我们不再需要它,我们可以将其删除(本地):

$ gitbranch -d 联系表单

对于删除远程分支:

git push origin --delete contact-form

Deleting Branches

Let's assume our work on branch "contact-form" is done and we've already integrated it into "master". Since we don't need it anymore, we can delete it (locally):

$ git branch -d contact-form

And for deleting the remote branch:

git push origin --delete contact-form
囚我心虐我身 2024-08-23 23:58:28

本地删除 -(正常)

git branch -d my_branch

如果您的分支处于变基/合并过程中并且未正确完成,则意味着您将收到错误,Rebase /合并正在进行中,因此在这种情况下,您将无法删除您的分支。

所以要么你需要解决变基/合并问题。否则,您可以使用以下方式强制删除

git branch -D my_branch

要在远程中删除:

git push --delete origin my_branch

您可以使用以下方式执行相同操作:

git push origin :my_branch   # Easy to remember both will do the same.

图形表示:

在此处输入图像描述

To delete locally - (normal)

git branch -d my_branch

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,

git branch -D my_branch

To delete in remote:

git push --delete origin my_branch

You can do the same using:

git push origin :my_branch   # Easy to remember both will do the same.

Graphical representation:

Enter image description here

酒废 2024-08-23 23:58:28

删除远程分支

git push origin :

删除本地分支

gitbranch -D

删除本地分支步骤:

  1. 检出到另一个分支
  2. 删除本地分支

Delete remote branch

git push origin :<branchname>

Delete local branch

git branch -D <branchname>

Delete local branch steps:

  1. checkout to another branch
  2. delete local branch
寒尘 2024-08-23 23:58:28

简单地说:

git branch -d <branch-name>
git push origin :<branch-name>

Simply say:

git branch -d <branch-name>
git push origin :<branch-name>
绝情姑娘 2024-08-23 23:58:28

现在,您可以使用 GitHub Desktop 应用程序来完成此操作。

启动应用程序后,

  1. 单击包含分支的项目
  2. 切换到您要删除的分支

    切换分支

  3. 从“分支”菜单中,选择“取消发布...”以删除分支来自 GitHub 服务器。

    取消发布分支

  4. 从“分支”菜单中,选择“删除“branch_name”...”,以从本地计算机(也称为您所在的计算机)中删除分支目前正在研究)

    删除本地分支

Now you can do it with the GitHub Desktop application.

After launching the application

  1. Click on the project containing the branch
  2. Switch to the branch you would like to delete

    Switching branch

  3. From the "Branch" menu, select, "Unpublish...", to have the branch deleted from the GitHub servers.

    Unpublish branch

  4. 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)

    Delete local branch

乱世争霸 2024-08-23 23:58:28
git push origin --delete <branch Name>

比更容易记住

git push origin :branchName
git push origin --delete <branch Name>

is easier to remember than

git push origin :branchName
阪姬 2024-08-23 23:58:28

如果您有一个与远程分支同名的标签,则此操作将不起作用:

$ git push origin :branch-or-tag-name
error: dst refspec branch-or-tag-name matches more than one.
error: failed to push some refs to '[email protected]:SomeName/some-repo.git'

在这种情况下,您需要指定要删除该分支,不是标签:

git push origin :refs/heads/branch-or-tag-name

同样,要删除标签而不是分支,您可以使用:

git push origin :refs/tags/branch-or-tag-name

This won't work if you have a tag with the same name as the branch on the remote:

$ git push origin :branch-or-tag-name
error: dst refspec branch-or-tag-name matches more than one.
error: failed to push some refs to '[email protected]:SomeName/some-repo.git'

In that case you need to specify that you want to delete the branch, not the tag:

git push origin :refs/heads/branch-or-tag-name

Similarly, to delete the tag instead of the branch you would use:

git push origin :refs/tags/branch-or-tag-name
注定孤独终老 2024-08-23 23:58:28

许多其他答案都会导致错误/警告。这种方法相对简单,但例如,如果它没有完全合并到 some_other_branch 中,您可能仍然需要 gitbranch -Dbranch_to_delete

git checkout some_other_branch
git push origin :branch_to_delete
git branch -d branch_to_delete

如果删除了远程分支,则不需要远程修剪。它仅用于获取您正在跟踪的存储库上可用的最新遥控器。我观察到 git fetch 会添加遥控器,而不是删除它们。以下是 git Remote prune origin 何时实际执行某些操作的示例:

用户 A 执行上述步骤。用户 B 将运行以下命令来查看最新的远程分支:

git fetch
git remote prune origin
git branch -r

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 into some_other_branch, for example.

git checkout some_other_branch
git push origin :branch_to_delete
git branch -d branch_to_delete

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 when git 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:

git fetch
git remote prune origin
git branch -r
有深☉意 2024-08-23 23:58:28

根据使用终端的最新文档我们可以通过以下方式删除。

本地删除:

git branch -D usermanagement

远程删除:

git push --delete origin usermanagement

According to the latest document using a terminal we can delete in the following way.

Delete in local:

git branch -D usermanagement

Delete in remote location:

git push --delete origin usermanagement
維他命╮ 2024-08-23 23:58:28

我厌倦了在谷歌上搜索这个答案,所以我对 crizCraig 之前发布的答案采取了类似的方法。

我将以下内容添加到我的 Bash 配置文件中:

function gitdelete(){
    git push origin --delete $1
    git branch -D $1
}

然后每次完成分支(例如合并到 master 中)时,我都会在终端中运行以下命令:

gitdelete my-branch-name

...然后删除 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:

function gitdelete(){
    git push origin --delete $1
    git branch -D $1
}

Then every time I'm done with a branch (merged into master, for example) I run the following in my terminal:

gitdelete my-branch-name

...which then deletes my-branch-name from origin as as well as locally.

纵山崖 2024-08-23 23:58:28

使用:

git push origin :bugfix  # Deletes remote branch
git branch -d bugfix     # Must delete local branch manually

如果您确定要删除它,请运行

git branch -D bugfix

立即清理已删除的远程分支 run

git remote prune origin

Use:

git push origin :bugfix  # Deletes remote branch
git branch -d bugfix     # Must delete local branch manually

If you are sure you want to delete it, run

git branch -D bugfix

Now to clean up deleted remote branches run

git remote prune origin
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文