git批量删除本地分支 - git拉取远程分支
如何在本地和远程删除Git分支? (20)
执行摘要
$ git push --delete <remote_name> <branch_name>
$ git branch -d <branch_name>
请注意,在大多数情况下,远程名称是origin
。
删除本地分支
要删除本地分支,请使用以下某个选项:
$ git branch -d branch_name
$ git branch -D branch_name
注意: -d
选项是--delete
的别名,只有在已经在其上游分支中完全合并的情况下才会删除该分支。 您还可以使用-D
,它是--delete --force
的别名,它会删除分支“无论其合并状态如何”。 [来源: man git-branch
]
删除远程分支[2017年9月8日更新]
从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日]
来自Scott Chacon的Pro Git第3章:
删除远程分支
假设您已经完成了远程分支 - 比如说,您和您的协作者已经完成了一项功能并将其合并到您的远程主分支(或您的稳定代码行所在的任何分支)。 您可以使用相当钝的语法
git push [remotename] :[branch]
删除远程分支。 如果要从服务器中删除serverfix分支,请运行以下命令:$ git push origin :serverfix To [email protected]:schacon/simplegit.git - [deleted] serverfix
繁荣。 您的服务器上没有更多分支。 您可能想要熟悉此页面,因为您需要该命令,并且您可能会忘记语法。 记住这个命令的一种方法是回忆一下我们之前讨论过的
git push [remotename] [localbranch]:[remotebranch]
语法。 如果你放弃[localbranch]
部分,那么你基本上就是说:“不要采取任何行动,让它成为[remotebranch]
。”
我发布了git push origin :bugfix
,它运行得很漂亮。 斯科特查康是对的 - 我会想要听到那个页面(或者通过在Stack Overflow上回答这个问题,实际上是狗耳朵)。
然后你应该在其他机器上执行它
git fetch --all --prune
传播变化。
我想删除本地和我在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.
在本地和GitHub上成功删除remotes/origin/bugfix
分支需要做些什么?
简短的答案
如果您想要更详细地解释以下命令,请参阅下一节中的长答案。
删除远程分支:
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个不同的分支 :
- 本地分支
X
- 远程原点分支
X
- 跟踪远程分支
X
的本地远程跟踪分支origin/X
使用的原始海报
git branch -rd origin/bugfix
它只删除了他的本地远程跟踪分支 origin/bugfix
,而不是origin
远程分支bugfix
。
要删除该实际的远程分支 ,您需要
git push origin --delete bugfix
额外细节
以下部分介绍了删除远程和远程跟踪分支时要考虑的其他详细信息。
推送删除远程分支也会删除远程跟踪分支
请注意,使用git push
从命令行删除远程分支X
也将删除本地远程跟踪分支 origin/X
,因此不必使用git fetch --prune
或git fetch -p
修剪过时的远程跟踪分支。 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开始)的典型方法是使用--prune
或更短的-p
运行git fetch
。 请注意,这将删除远程不再存在的任何远程分支的所有过时的本地远程跟踪分支 :
git fetch origin --prune
git fetch origin -p # Shorter
以下是1.6.6发行说明中的相关引用(强调我的):
“git fetch”学习了
--all
和--multiple
选项,用于从许多存储库运行fetch,以及--prune
选项用于删除过时的远程跟踪分支。 这些使得“git remote update”和“git remote prune”不那么必要(虽然没有计划删除“远程更新”或“远程修剪”)。
替代上述自动修剪过时的远程跟踪分支
或者,不是通过git fetch -p
修剪过时的本地远程跟踪分支,而是通过使用--remote
或-r
标志手动删除分支来避免进行额外的网络操作 :
git branch --delete --remotes origin/X
git branch -dr origin/X # Shorter
也可以看看
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 : https://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote
让我们假设我们在分支“联系表单”上的工作已经完成,我们已经将它集成到“master”中。 由于我们不再需要它,我们可以删除它(本地):
$ git branch -d contact-form
并删除远程分支:
git push origin --delete contact-form
在本地删除:
要删除本地分支,您可以使用:
git branch -d branch_name
要强制删除分支,请使用-D
而不是-d
。
git branch -D branch_name
远程删除:
有两种选择:
git push origin :branchname
git push origin --delete branchname
我建议你使用第二种方式,因为它更直观。
删除远程分支
git push origin :<branchname>
删除本地分支
git branch -D <branchname>
删除本地分支步骤:
- 结账到另一个分行
- 删除本地分支
另一种方法是
git push --prune origin
警告: 这将删除本地不存在的所有远程分支。 或者更全面,
git push --mirror
将有效地使远程存储库看起来像存储库的本地副本(本地磁头,远程和标签在远程镜像)。
在本地和远程删除分支
结帐到主分支 -
git checkout master
删除远程分支 -
git push origin --delete <branch-name>
删除本地分支 -
git branch --delete <branch-name>
如果您的标签与远程分支的名称相同,则无效:
$ 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
如果要删除分支,请首先签出除要删除的分支以外的分支。
git checkout other_than_branch_to_be_deleted
删除本地分支:
git branch -D branch_to_be_deleted
删除远程分支:
git push origin --delete branch_to_be_deleted
您还可以使用以下命令删除远程分支。
git push --delete origin serverfix
这与做同样的事情
git push origin :serverfix
但它可能更容易记住。
所有其他答案的混搭。 需要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
现在,您可以使用GitHub桌面应用程序。
启动应用程序后
简单地说:
git branch -d <branch-name>
git push origin :<branch-name>
许多其他答案将导致错误/警告。 这种方法相对简单,虽然你可能仍然需要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分支,首先使用以下命令删除本地分支:
git branch -d example
(这里的example
是分支名称)
然后使用命令删除远程分支:
git push origin :example
git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>
git push origin --delete branchName
更容易记住
git push origin :branchName