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

$ 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 -d <remote_name> <branchname>
git branch -d <branchname>

注:在大多数情况下,<remote_name>origin.

删除本地分处

删除当地当地使用下列分支之一:

git branch -d <branch_name>
git branch -D <branch_name>
  • 缩略-d的别名选项--delete,只有在该分支已经完全并入其上游分支的情况下,该分支才删除。
  • 缩略-D的别名选项--delete --force,删除分支“不管其合并地位如何”。 [资料来源:man git-branch]
  • 截至截至Git v2.3 吉特 v2.3, git branch -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章普罗吉特Scott Chacon写道:

正在删除远程分支

假设你已经用一个远程分支完成了,比如,你和你的合作者完成了一个功能,并将其合并到您的远程主要分支(或您稳定的代码线中的任何分支)。您可以用相当模糊的语法删除一个远程分支。git push [remotename] :[branch]。如果您想要从服务器中删除服务器上的服务器固定分支,您将运行以下操作:

$ git push origin :serverfix
To git@github.com:schacon/simplegit.git
 - [deleted]         serverfix

boom 。 在您的服务器上不再有分支。 您可能想要在这个页面上粘住树枝 , 因为您需要这个命令, 您可能会忘记语法 。 记住这个命令的一个方法就是提醒git push [remotename] [localbranch]:[remotebranch]之前我们讨论过的语法 如果你离开[localbranch]你基本上在说,“不要拿我这边的任何东西,让它成为[remotebranch].”

印本印发 印发git push origin: bugfix斯科特·查孔是对的狗狗耳朵该页(或几乎是狗耳朵,在Stack overflow上回答这个)。

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

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

以传播变化。

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

git push --delete origin serverfix

git push origin :serverfix

但它可能更容易记住。

除了其他答案之外,我经常使用git_ remote_ 分支工具。这是一个额外的安装,但它能方便地与远程分支进行互动。在这种情况下,删除:

grb delete branch

我发现我也使用publishtrack命令非常常见 。

马修的回答可用于删除偏远我也感谢这一解释,但要简单区分两个命令:

  • 要删除地方分支来自您的机器 :git branch -d {local_branch}(使用-D而不是强制删除分支,而不检查合并状态);

  • 要删除远程分支来自服务器的 :git push origin -d {remote_branch}.

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

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

git branch -d <branchname> # Deletes local branch

git push origin :<branchname> # Deletes remote branch

仅删除引用。 即使该分支实际上在远程删除, 但它的引用仍然存在于您的团队成员所在的本地存储库中 。 这意味着对于其他团队成员来说, 被删除的分支在做一个git branch -a.

为了解决这个问题,你的团队成员可以使用

git remote prune <repository>

这是典型的git remote prune origin.

另一种办法是:

git push --prune origin

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

git push --mirror

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

如果您想要用一个单命令来完成这两个步骤, 您可以在您的~/.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(lowercase d) , 分支分支只有在合并时才被删除 。 要强制删除, 您需要使用-D(多例D)。

您也可以使用git remote prune origin

$ git remote prune origin
Pruning origin
URL: git@example.com/yourrepo.git
 * [pruned] origin/some-branchs

它利用和删除了远程跟踪分支和远程跟踪分支。git branch -r列表。

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

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

然后您可以拨打:

git-shoot branchname

自2013年1月以来,GitHub包括删除分支按钮旁边的“Branches”页面中的每个分支。

相关博客文章:创建和删除分支

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

这是所有其它答案的混结。 它需要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

许多其他答案都会导致错误/警告。git branch -D branch_to_delete如果未完全合并到some_other_branch例如。

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

缩略短答案

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

正在删除远程分支

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

另见

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

$ 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 checkout other_than_branch_to_be_deleted

删除本地分支 :

git branch -D branch_to_be_deleted

删除远程分支 :

git push origin --delete branch_to_be_deleted

很简单: 只要运行以下命令 :

要删除本地和远程的 Git 分支, 请使用此命令先删除本地分支 :

git branch -d example

(此处example是分支名称。 )

在此之后, 使用此命令删除远程分支 :

git push origin :example

简单说一句:

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

较容易记住

git push origin :branchName

本地删除 :

要删除本地分支,请使用:

git branch -d <branch_name>

强行删除分支,使用-D代替-d.

git branch -D <branch_name>

远程删除 :

有两个选项:

git push origin :branchname

git push origin --delete branchname

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

现在,你可以做它与GitHub 桌面桌面应用程序。

启动申请程序后

  1. 点击含有分支的工程
  2. 切换到您想要删除的分支

    Switching branch

  3. 从“ Branch” 菜单中选择“ 不出版... ” , 将分支从 GitHub 服务器中删除 。

    Unpublish branch

  4. 从“ 空白” 菜单中选择“ 删除”分支组名称"""",""",""",""","""","""",""""","""","""""","""""","""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

    Delete local branch

删除分行

我们假设我们的分支“联系形式”的工作已经完成,我们已经将其纳入“主人”中。由于我们不再需要它,我们可以删除它(就地):

$ git branch -d contact-form

要删除远程分支 :

git push origin --delete contact-form

删除远程分支

git push origin :<branchname>

删除本地分支

git branch -D <branchname>

删除本地分支步骤 :

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

本地和远程删除您的分支

  • 取出至母版分支 -git checkout master

  • 删除您的远程分支 -git push origin --delete <branch-name>

  • 删除您的本地分支 -git branch --delete <branch-name>

删除远程分支的命令行的替代选项是GitHub 分支页面.

例如,见:https://github.com/angular/angular.js/branches

发现于Code -> BranchesGitHub 库页面。

我一般都更喜欢指挥线 但这个GitHub 页面显示更多信息,树的枝枝的枝枝,例如最新更新日期和用户, 和未来和后承诺数在与大量分支机构打交道时非常有用。

我厌倦了勾勾勾勾勾勾勾勾勾勾勾的答案, 所以我采取了类似的办法,克雷格所发回的答案早些时候。

我在我的巴什档案中增加了以下内容:

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

每当我完成一个分支(合并成master例如)我在我的终端运行以下功能:

gitdelete my-branch-name

...然后删除my-branch-name调自origin以及当地。

使用 :

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

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

git branch -D bugfix

现在清理已删除的远程分支, 运行

git remote prune origin

执行前执行前

git branch --delete <branch>

确保您首先确定精确远程分支的名称是执行 :

git ls-remote

这将告诉您要进入什么精确用于<branch>值 。 () (branch案件敏感! )

答案是好的, 但是, 如果您有一大堆分支, 逐个删除本地和远程的分支, 将会是一个无聊的任务 。 您可以使用此脚本将这些任务自动化 。

branch_not_delete=( "master" "develop" "our-branch-1" "our-branch-2")

for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do

    # Delete prefix remotes/origin/ from branch name
    branch_name="$(awk '{gsub("remotes/origin/", "");print}' <<< $branch)"

    if ! [[ " ${branch_not_delete[*]} " == *" $branch_name "* ]]; then
        # Delete branch remotly and locally
        git push origin :$branch_name
    fi
done
  • 列出您不想删除的分支
  • 绕过遥控器的分支 如果它们不在我们的“保护名单”里 就会删除它们

资料来源:立即删除 Git 分支

A A A一班班要删除的命令本地和偏远地区:

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

或者将下面的别名添加到您的~ /. git config ~ /. git config用法:git kill branch-name

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

使用吉特巴什您可以执行以下操作:

git branch --delete <branch>

-

从 GitHub 桌面应用程序中删除当地当地通过分支通过分处菜单条 :

Enter image description here

如果你(如果)不属于使用 GitHub 桌面应用程序, 并正在使用像视觉工作室那样的 IDE 来控制本地源码, 您只需要快速的一步 :

  1. 检查您要删除的分支以外的分支 。
  2. 右键单击您想要删除的分支 。
  3. 选择删除删除删除从上下文菜单中选择。

然后,一旦登录到您的 GitHub 账户, 一旦登录到您的 GitHub 在线账户, 转到存储库并单击所有各处选项卡。从此选项卡中,单击您想要删除的分支名称右侧的小垃圾桶图标。

Enter image description here

无需试图从你的网上存储库删除。

之后我加了以下别名.gitconfig此选项允许我删除有分支或没有指定分支名称的分支。 如果没有通过参数, 分支名称默认为当前分支 。

[alias]
    branch-name = rev-parse --abbrev-ref HEAD     

    rm-remote-branch = !"f() { branch=${1-$(git branch-name)}; git push origin :$branch; }; f"
    rm-local-branch = !"f() { branch=${1-$(git branch-name)}; git checkout master; git branch -d $branch; }; f"
    rm-branch-fully = !"f() { branch=${1-$(git branch-name)}; git rm-local-branch $branch; git rm-remote-branch $branch; }; f"

要删除本地本地- (正常)

git branch -d my_branch

如果分行进展迟缓,而且未适当完成,那表示你们是误入歧途的。Rebase/Merge in progress,在这种情况下,你将无法删除你的分支。

所以要么你要么你需要解决 重新定位/合并的问题。 否则,你可以用武力删除删除通过使用,

git branch -D my_branch

删除偏远:

git push --delete origin my_branch

您可以使用 :

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

图形代表:

Enter image description here

删除分支的步骤 :

删除远程分支 :

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

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

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

还有git branch -D,它简单地删除了本地分支仅仅!...

-D代表--delete --force将会删除分支, 即使它没有合并( 强制删除) , 但是您也可以使用-d代表--delete将分支合并状态的错误丢弃为相应的错误...

我还创造了下方图像以显示步骤:

Delete a remote and local branch in git

我在我的.bash_alians 文件中创建了以下方便功能 :

git-delete-branch() 
{ 
    if [[ -n $1 ]]; then
        git checkout master > /dev/null;
        branch_name="$1";
        echo "Deleting local $branch_name branch...";
        git branch -D "$branch_name";
        echo "Deleting remote $branch_name branch...";
        git push origin --delete "$branch_name";
        git remote prune origin;
        echo "Your current branches are:";
        git branch -a;
    else
        echo "Usage: git-delete-branch <branch_name>";
    fi
}

根据最新文件,使用终端,我们可以以以下方式删除。

删除本地语 :

git branch -D usermanagement

在远程位置删除 :

git push --delete origin usermanagement

很简单:

删除远程分支

git push -d origin <branch-name>

git push origin :<branch-name>

- 也可以用此语法删除标记

强制删除本地分支

git branch -D <branch-name>

注:do 做 agit fetch --all --prune删除远程分支后, 在其它机器上删除已过时的跟踪分支 。

示例示例示例示例

要删除本地分支

git branch -D my-local-branch

要删除远程分支

git push origin :my-remote-branch

随着新版本的Git, 也有可能删除分支

git push origin --delete <branch_name>

TIP :如果您想要查看所有可用的分支, 您可以使用git branch -a,

并且只看到边远的树枝, 你可以使用git branch -r

两者酷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

最灵活的方式是使用自定义 Git 命令例如,在您的某处创建以下 Python 脚本$PATH名称下git-rmbranch并使它可执行 :

#!/usr/bin/env python3

import argparse
import subprocess
import sys

def rmbranch(branch_name, remote, force):
    try:
        print(subprocess.run(['git', 'branch', '-D' if force else '-d', branch_name],
                             capture_output=True, check=True, encoding='utf-8').stdout, end='')
    except subprocess.CalledProcessError as exc:
        print(exc.stderr.replace(f'git branch -D {branch_name}', f'git rmbranch -f {branch_name}'), end='')
        return exc.returncode

    return subprocess.run(['git', 'push', remote, '--delete', branch_name]).returncode    

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Delete a Git branch locally and remotely.')
    parser.add_argument('-r', '--remote', default='origin', help="The remote name (defaults to 'origin')")
    parser.add_argument('-f', '--force', action='store_true', help='Force deletion of not fully merged branches')
    parser.add_argument('branch_name', help='The branch name')
    args = parser.parse_args()

    sys.exit(rmbranch(args.branch_name, args.remote, args.force))

然后git rmbranch -h将会显示您使用信息 :

usage: git-rmbranch [-h] [-r REMOTE] [-f] branch_name

Delete a Git branch locally and remotely.

positional arguments:
  branch_name           The branch name

optional arguments:
  -h, --help            show this help message and exit
  -r REMOTE, --remote REMOTE
                        The remote name (defaults to 'origin')
  -f, --force           Force deletion of not fully merged branches

请注意git push origin --delete <branch_name>并删除本地远程跟踪分支(origin/<branch_name>))(因此不必在意这一点。

P. S. 您可以找到这个 Git 命令的最新版本在这里欢迎提出意见和建议。

最初的几种方法对我行不通。

假设你有以下分支和远程分支,

Local : Test_Branch
Remote: remotes/origin/feature/Test_FE

正确设置上方为您本地分支以跟踪您想要删除的远程分支 。

git branch --set-upstream-to=remotes/origin/feature/Test_FE Test_Branch

然后删除远程分支执行此任务

git push origin --delete Test_Branch

然后删除本地分支,按照命令执行

git branch -D Test_Branch

就是这样。

您可以在此删除与 Glob 或任何分支名称相对应的远程分支 :

git branch -r --list "origin/*" | xargs git branch -r -D