删除远程分支的尝试失败 :

$ 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本地和远程分支?


当前回答

您也可以使用以下内容删除远程分支

git push --delete origin serverfix

git push origin :serverfix

但它可能更容易记住。

其他回答

如果您有一个标签, 标签名称与遥控器上的分支相同, 这行不通 :

$ 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 'git@github.com:SomeName/some-repo.git'

在此情况下, 您需要指定要删除分支, 而不是标记 :

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

类似地, 要删除标签, 而不是您要使用的分支 :

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

缩略短答案

如果您想要对以下命令进行更详细的解释,请在下一节中看到长的答案。

正在删除远程分支

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. 当地远程跟踪处origin/X跟踪远程分支X.

Visualization of three branches

最初使用的海报是:

git branch -rd origin/bugfix

只删除了他的当地远程跟踪分支 origin/bugfix,而不是实际的远程分支bugfix上 年 月origin.

Diagram 2

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

git push origin --delete bugfix

Diagram 3

补充详情

以下各节介绍在删除远程和远程跟踪分支时需要考虑的其他细节。

推动删除远程分支还删除远程跟踪分支

注意:删除远程分支X使用 agit push 还将删除当地的远程跟踪分支 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

保护过时的当地远程跟踪分分机/X

如果您没有删除您的远程分支X从命令行(如上)发出命令行(如上),然后您的本地仓库将仍然包含(一个现已过时的)远程跟踪分支origin/X。如果您直接通过 GitHub 的网络界面删除远程分支,这种情况就会发生。

清除这些过时的远程跟踪分支(自Git 1.6.6版以来)的典型方法是简单地运行git fetch和和--prune短或短-p. 请注意,这清除了所有在偏远地区已不复存在的偏远分支的所有陈旧的当地远程跟踪分支。:

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

以下是以下相关引文:1.6.6 释放说明(强调地雷):

学习“ 即时抓取” 学习 --all--multiple 选项,以运行从许多仓库获取的信息,以及--prune选项,可删除过期的远程跟踪分支。更没有必要(虽然没有计划删除“远程更新”或“远程更新”)。

取代对过时的远程跟踪分支自动修剪的替代品

而不是通过git fetch -p, 您可以避免进行额外的网络操作,而仅手动删除与--remotes-r国旗 :

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

另见

我用下面的 在我的巴什设置 :

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

然后您可以拨打:

git-shoot branchname

这是所有其它答案的混结。 它需要Ruby 1.9. 3+, 并经过测试仅仅OS X上写着

调用此文件git-remove,然后使用,例如,git remove temp.

#!/usr/bin/env ruby
require 'io/console'

if __FILE__ == $0
      branch_name = ARGV[0] if (ARGV[0])
      print "Press Y to force delete local and remote branch #{branch_name}..."
    response = STDIN.getch
    if ['Y', 'y', 'yes'].include?(response)
      puts "\nContinuing."
      `git branch -D #{branch_name}`
      `git branch -D -r origin/#{branch_name}`
      `git push origin --delete #{branch_name}`
    else
      puts "\nQuitting."
    end
end

两者酷AJ86号A. 兵器答案非常相似。 我回过头来试图理解支持子模块替换的更好方法。 下面是两者的组合。

首先将 Git Bash 导航到 Git 仓库的根部, 以便分割 。 在我这里的示例中, 就是~/Documents/OriginalRepo (master)

# Move the folder at prefix to a new branch
git subtree split --prefix=SubFolderName/FolderToBeNewRepo --branch=to-be-new-repo

# Create a new repository out of the newly made branch
mkdir ~/Documents/NewRepo
pushd ~/Documents/NewRepo
git init
git pull ~/Documents/OriginalRepo to-be-new-repo

# Upload the new repository to a place that should be referenced for submodules
git remote add origin git@github.com:myUsername/newRepo.git
git push -u origin master
popd

# Replace the folder with a submodule
git rm -rf ./SubFolderName/FolderToBeNewRepo
git submodule add git@github.com:myUsername/newRepo.git SubFolderName/FolderToBeNewRepo
git branch --delete --force to-be-new-repo

下面是上面的复制件, 上面有自定义的可替换名称, 代之以 HTTPS 。 根文件夹是现在的 。~/Documents/_Shawn/UnityProjects/SoProject (master)

# Move the folder at prefix to a new branch
git subtree split --prefix=Assets/SoArchitecture --branch=so-package

# Create a new repository out of the newly made branch
mkdir ~/Documents/_Shawn/UnityProjects/SoArchitecture
pushd ~/Documents/_Shawn/UnityProjects/SoArchitecture
git init
git pull ~/Documents/_Shawn/UnityProjects/SoProject so-package

# Upload the new repository to a place that should be referenced for submodules
git remote add origin https://github.com/Feddas/SoArchitecture.git
git push -u origin master
popd

# Replace the folder with a submodule
git rm -rf ./Assets/SoArchitecture
git submodule add https://github.com/Feddas/SoArchitecture.git
git branch --delete --force so-package