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

我想删除本地和我在GitHub上的远程项目分支上的分支.

尝试删除远程分支失败

$ 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在本地和GitHub上成功删除分支我需要做些什么

回答

执行摘要

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

请注意,在大多数情况下,远程名称是origin.

删除本地分支

要删除本地分支,请使用以下某个选项:

$ git push -d origin <branch_name>

注意:-d选项是别名--delete,仅在分支已在其上游分支中完全合并时才删除分支.您还可以使用-D,这是一个别名--delete --force,删除分支"无论其合并状态如何".[来源:man git-branch]

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

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

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

这可能比记住更容易记住

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

这是在Git v1.5.0中添加的"删除远程分支或标记".

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

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

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

来自Scott Chacon 的Pro Git第3章:

删除远程分支

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

$ git push <remote_name> :<branch_name>

繁荣.您的服务器上没有更多分支.您可能想要了解此页面,因为您需要该命令,并且您可能会忘记语法.记住这个命令的一种方法是回忆一下git push [remotename] [localbranch]:[remotebranch]我们之前讨论过的语法.如果你放弃这个[localbranch]部分,那么你基本上就是说:"不要把任何东西放在我身边,让它成为现实[remotebranch]."

我发布了git push origin: bugfix它并且工作得非常好.斯科特查孔是对的 - 我会想要听到那个页面(或者通过在Stack Overflow上回答这个问题,实际上是狗耳朵).

然后你应该在其他机器上执行它

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

传播变化.

  • 在删除服务器上的远程分支后,不要忘记在其他机器上执行`git fetch --all --prune`.||| 用`git branch -d`删除本地分支并用`git push origin -delete`删除远程分支后,其他机器可能仍然有"过时的跟踪分支"(看他们做`git branch -a`).要摆脱这些,请执行`git fetch --all --prune`.
  • 我不得不运行`git branch -D Branch_Name`来摆脱本地分支
  • 除了@ TrevorBoydSmith的`git branch -a`查看所有分支外,你还可以使用`git branch -r`来查看远程分支.另见`git remote show origin` - 来源:http://gitready.com/intermediate/2009/02/13/list-remote-branches.html
  • @KolobCanyon如果分支尚未合并到另一个分支,则只需使用-D.
  • 问题是***“要在本地和GitHub上成功删除remotes / origin / bugfix分支,我需要做些什么不同?” ***在更新后的答案中运行命令后,本地分支仍然存在。如果接受的答案是***完整***的答案,那将是很好的。Git很难完成简单的任务,这绝对令人惊讶。
  • 如果你在分公司不行.需要做一个git checkout master或者之前离开分支的任何东西.

Matthew的答案很适合删除远程分支,我也很欣赏这个解释,但是要简单区分这两个命令:

要从您的计算机中删除本地分支:

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

要从服务器中删除远程分支:

git push origin --delete {the_remote_branch}

参考:https://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote

  • @megido很好`-D`强制删除,`-d`会给你一个警告,如果它还没有被合并.
  • 我建议使用-d而不是-D,因为它更安全.如果-d由于未合并的提交而失败,那么您需要对其进行评估,如果确定可以删除则使用-D.
  • 如果你的本地分支没有与master合并并运行''git branch -d your_branch`那么你就会出错'错误:分支'your_branch'没有完全合并.如果您确定要删除它,请运行'git branch -D your_branch'
  • 其他具有已删除远程分支的存储库克隆的其他人应运行`git remote prune <name>`(例如`git remote prune origin`),以便本地删除远程不再存在的陈旧分支.
  • 我想补充一点,-d如果未与当前HEAD合并,则会发出警告。如果需要澄清,我建议使用此命令`git branch -a --merged origin / master`。它将列出本地和远程的所有分支。已经合并为主人。[此处的其他信息](http://stackoverflow.com/questions/226976/how-can-i-know-in-git-if-a-branch-has-been-already-merged-into-master)

简短的答案

如果您想要更详细地解释以下命令,请参阅下一节中的长答案.

删除远程分支:

git push origin --delete <branch>  # Git version 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 tracking branches
git fetch <remote> -p      # Shorter

长答案:有3个不同的分支要删除!

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

  1. 当地分公司X.
  2. 远程原点分支X.
  3. 跟踪origin/X远程分支的本地远程跟踪分支X.

使用的原始海报

git branch -rd origin/bugfix

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

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

git push origin --delete bugfix

额外细节

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

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

请注意,X使用a从命令行删除远程分支git push 也会删除本地远程跟踪分支 origin/X,因此没有必要用git fetch --prune或修剪过时的远程跟踪分支git 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开始)的典型方法是git fetch使用--prune或更短的运行-p.请注意,这将删除远程不再存在的任何远程分支的所有过时的本地远程跟踪分支:

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

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

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

或者,而不是通过修剪陈旧的本地远程跟踪分支git fetch -p,可以避免额外的网络操作仅通过手动删除与分支(ES)--remote-r标志:

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

也可以看看

  • git-branch(1)手册页.
  • git-fetch(1)手册页.
  • ProGit§3.5Git Branching - 远程分支.
  • 远程跟踪分支+1.当您克隆其他人的分支时,此分支是导致问题的原因.它会继续跟踪您的提交并询问您是否要推送到该人员的分支机构.
  • @huggie说的非常正确.Git中的分支只是附加到提交的书签.所以在上面的图表中,本地克隆(2个分支)中有"X"和"origin/X"书签,然后遥控器上有"X"(3个分支).
  • I've read all the answers down to here and this is for sure the best answer I've read so far!--(and probably the best one on this page, period). This is especially true because it's the only answer which states this REALLY IMPORTANT fact that I never knew before: "**there are 3 different branches to delete!**" I had no idea! This all makes so much more sense now, and it sheds so much light on all the other answers here now too. Thanks!

删除分支的步骤:

删除远程分支:

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

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

根本就git push origin --delete只能删除远程分支,在末尾添加分公司的名称,这将删除,并在同一时间推送到远程...

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

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

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

  • `git branch -a`将显示本地和远程分支.它将为您的图表介绍提供帮助.
  • 请注意,如果要在要删除的分支上进行设置,则在删除本地分支之前,需要签出除需要删除的分支(例如,主分支)以外的分支。
  • 图像令人分心,非常大,没有任何答案。我希望这不会成为SO的趋势。

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

git push --delete origin serverfix

这与做同样的事情

git push origin :serverfix

但它可能更容易记住.


提示:使用时删除分支

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 remote prune`是一种有点过时的方法来删除过时的远程跟踪分支,更新的方法是使用`git fetch --prune`或`git fetch -p`.
  • 您应该澄清上面的`git push`操作删除了本地分支*和*远程分支.

如果要删除分支,请首先签出除要删除的分支以外的分支.

git checkout other_than_branch_to_be_deleted

删除本地分支:

git branch -D branch_to_be_deleted

删除远程分支:

git push origin --delete branch_to_be_deleted

git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>
  • ahahah :)这取决于你:如果你想看到git哭,请使用-d,如果你想哭,请使用-D.
  • 注意`-D`*强制*删除.使用`-d`总是更好,这会提醒你是否需要做一些危险的事情.

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

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

git branch -d example

(这example是分支名称)

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

git push origin :example

另一种方法是: -

git push --prune origin

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

git push --mirror

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


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

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

然后你可以打电话:

git-shoot branchname
  • 我最后只是将别名"shoot"添加到我的.gitconfig shoot = push origin --delete中

自2013年1月起,GitHub Delete branch在"分支"页面的每个分支旁边都添加了一个按钮.

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

  • 我要指出这个.请注意,该按钮不会删除您的本地分支...请参阅此答案以了解如何执行此操作:http://stackoverflow.com/a/10999165/901641
  • 我今年才开始使用Github,所以我想知道为什么这是一个受到高度评价的问题,以及为什么没有最重要的答案建议只是从Github Web界面删除它!有趣的是,这只是最近的补充。

在本地删除:

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

git branch -d <branch_name>

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

git branch -D <branch_name>

远程删除:

有两种选择:

git push origin :branchname
git push origin --delete branchname

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


如果您想使用单个命令完成这两个步骤,可以通过将以下内容添加到以下内容中为其创建别名~/.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).

  • 这就是我想要的.我自己的shell函数别名不起作用(意外的EOF),我无法弄清楚为什么,但这很好用!我做的唯一改变是用`;`替换`&&`,这样即使第一个命令失败,第二个命令仍然会执行(有时只有本地或只有远程存在).

在本地和远程删除分支


你也可以这样做 git remote prune origin

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

它从git branch -r列表中修剪和删除远程跟踪分支.


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

grb delete branch

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


一个衬管命令删除本地和远程:

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

或者将下面的别名添加到〜/ .gitconfig ; 用法:git kill branch-name

[alias]
kill = "!f(){ git branch -D \"$1\";  git push origin --delete \"$1\"; };f"
  • ⚠️在脚本中小心使用`git branch -D`,因为它强制删除分支而不检查它是否已合并.使用`-d`是安全的.

删除分支

$ git branch -d contact-form

并删除远程分支:

git push origin --delete contact-form

删除远程分支

git push origin :<branchname>

删除本地分支

git branch -D <branchname>

删除本地分支步骤:

  1. 结账到另一个分行
  2. 删除本地分支
  • 远程分支删除之后是否需要“ git push”?

简单地说:

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

git push origin --delete <branch Name>

更容易记住

git push origin :branchName

现在,您可以使用GitHub桌面应用程序.

启动应用程序后

  1. 单击包含分支的项目
  2. 切换到要删除的分支
  3. 从"分支"菜单中,选择"取消发布...",以从GitHub服务器中删除分支.
  4. 从"分支"菜单中选择"删除" branch_name "...",将分支从本地计算机(也就是您当前正在处理的计算机)中删除
  • 我没有投票,但我的想法是它没有实质性的帮助.问题显然是要求更多的命令行类型答案,而不必使用外部程序,如果人们点击这里,他们可能不会寻找桌面方式的github.
  • @Daemedeor,我不同意.在OP提出问题的2010年,UI的实现方式并不存在,唯一的选择是命令行.为了表明您只想要一个命令行选项,应该在问题中或使用标签[tag:command-line-interface]说明,在这种情况下,它不存在.
  • 删除远程分支的git命令很烂,我往往会忘记它(新旧的)。幸运的是,有可以选择的GUI工具。Git Gui,TortoiseGit和GitHub Desktop都有它-我希望Git Extensions也具有此功能。无论如何,当我需要删除远程分支时,我记得是从Git Extensions中启动Git Gui。

要删除本地 - (正常),

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.

图示,


如果您的标签与远程分支的名称相同,则无效:

$ 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
  • 好吧,我的情况是我正在将分支转换为标记,并且标记与分支具有相同的名称是有意义的.通过转换I意味着将分支B合并到A并用分支B标记分支B中的最后一次提交,以便在删除分支B之后,通过简单地检查标记B仍然可以容易地恢复它.

许多其他答案都会导致错误/警告.例如,这种方法相对简单,但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

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

在我的Bash配置文件中添加了以下内容:

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

然后每次我完成一个分支(master例如合并)我在终端中运行以下命令:

gitdelete my-branch-name

......然后my-branch-nameorigin本地删除.

  • 扩展这个,` - delete"$ @"`和`-D"$ @"`而不是`$ 1`将为多个分支处理它.
  • 我建议首先运行`git branch -d`(小写'd')以确保已合并更改,然后成功推送(在命令之间放入`&&`)

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区分大小写!)


所有其他答案的混搭.需要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
  • 一个字母得到这个答案的礼貌:http://stackoverflow.com/a/8072675/8047
  • 对不起,但为这种工作安装Ruby?更合乎逻辑的是bash上的实现,它将开箱即用.
  • @Yar此链接不在上下文中,范围更广.我只讲git,并且主题不仅仅源于OSX,选择对其他系统来说很奇怪(例如*UNIX,Windows)

我在.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"
  • 注意de`-D`选项.在批处理中考虑使用较低的`-d`

用于删除远程分支的命令行的备选选项是GitHub分支页面.

例如,请参阅:https://github.com/angular/angular.js/branches

在GitHub存储库的Code- > Branches页面中找到.

我自己通常更喜欢命令行,但是这个GitHub页面向您显示有关分支的更多信息,例如上次更新的日期和用户,以及前后提交的数量.处理大量分支时很有用.


我也有类似的问题,这似乎工作:这删除本地分支.
git branch -d the_local_branch

这将删除远程分支
git push origin :the_remote_branch

资料来源:Makandra Cards


有很好的答案,但是,如果你有大量的分支机构,在本地和远程一个接一个地删除它们将是一项单调乏味的任务.您可以使用此脚本自动执行此任务.

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


使用GitBash,您可以执行以下操作:

git branch --delete <branch>

要么

从GitHub桌面应用程序,当您签出分支时,您可以通过分支菜单条删除本地分支:

如果您没有使用GitHub桌面应用程序,并且使用像Visual Studio这样的IDE来进行本地源代码控制,那么您只需要几个快速步骤:

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

然后,在线登录您的GitHub帐户后,转到存储库并单击All Branches选项卡.从那里,只需点击您要删除的brach名称右侧的小垃圾桶图标.

*请记住 - 如果分支未发布,则无需尝试从在线存储库中删除它.


我在.bash_aliases文件中创建了以下方便的函数:

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
  • I really have no idea why `git` command is so inconsistent and unintuitive to remember. Looks at the deletion, one is `-D`, another one is `-d|--delete`
  • This is the only solution that worked for me +1.

删除远程分支

git push -d origin <branch-name>

要么

git push origin :<branch-name>

删除本地分支

git branch -D <branch-name>
  • 我需要使用 `--delete` 而不是 `-d` 来删除远程分支。
  • `-d` 选项是 `--delete` 的别名,如果 `--delete` 起作用,那么 `-d` 也应该起作用,如果你强行想要删除一个分支,你可以使用 `-D` 代替 `- d` 或 `--delete`。

最灵活的方法是使用自定义 Git 命令。例如,$PATH在名称下的某处创建以下 Python 脚本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>默认情况下),因此无需关心。

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

  • @Mogens Python is already preinstalled in most sane distributions. With git only you can't for example: 1) customize the output (e.g. make it more consistent) 2) combine multiple commands in desired way 3) easily customize the logic. Besides, entering the same commands over and over again is pretty boring.

无论CoolAJ86的和apenwarr的答案都非常相似。我在两者之间来回走动,试图了解支持子模块替换的更好方法。下面是它们的组合。

首先将 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

前几种方法对我不起作用,

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

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

就是这样。干杯。


以上是如何在本地和远程删除Git分支?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>