如何删除已合并的分支?我可以一次删除所有分支,而不是逐个删除每个分支吗?
当前回答
要删除已合并到主分支的本地分支,我使用以下别名(git-config-e--global):
cleanup = "!git branch --merged master | grep -v '^*\\|master' | xargs -n 1 git branch -D"
我使用gitbranch-D来避免错误:分支“somebranch”未完全合并。消息,而我的当前签出与主分支不同。
其他回答
对于那些使用Windows并喜欢PowerShell脚本的人,这里有一个删除本地合并分支的脚本:
function Remove-MergedBranches
{
git branch --merged |
ForEach-Object { $_.Trim() } |
Where-Object { $_ -NotMatch "^\*" } |
Where-Object { -not ( $_ -Like "*master" -or $_ -Like "*main" ) } |
ForEach-Object { git branch -d $_ }
}
或者简称:
git branch --merged | %{$_.trim()} | ?{$_ -notmatch 'dev' -and $_ -notmatch 'master' -and $_ -notmatch 'main'} | %{git branch -d $_.trim()}
刚刚为此创建了python脚本:
import sys
from shutil import which
import logging
from subprocess import check_output, call
logger = logging.getLogger(__name__)
if __name__ == '__main__':
if which("git") is None:
logger.error("git is not found!")
sys.exit(-1)
branches = check_output("git branch -r --merged".split()).strip().decode("utf8").splitlines()
current = check_output("git branch --show-current".split()).strip().decode("utf8")
blacklist = ["master", current]
for b in branches:
b = b.split("/")[-1]
if b in blacklist:
continue
else:
if input(f"Do you want to delete branch: '{b}' [y/n]\n").lower() == "y":
call(f"git branch -D {b}".split())
call(f"git push --delete origin {b}".split())
Git Sweep在这方面做得很好。
我最喜欢的简单脚本:
git branch --merged | grep -E -v "(master|main|develop|other)" | xargs git branch -d
如果您使用的是Windows,您可以使用Windows Powershell或Powershell 7以及Out GridView来创建一个漂亮的分支列表,并用鼠标选择要删除的分支:
git branch --format "%(refname:short)" --merged | Out-GridView -PassThru | % { git branch -d $_ }
单击“确定”后,Powershell会将这些分支名称传递给gitbranch-d命令并删除它们
推荐文章
- git pull -rebase和git pull -ff-only之间的区别
- GitHub -致命:无法读取用户名https://github.com':没有这样的文件或目录
- 撤消git平分错误
- 使用.gitconfig配置diff工具
- 如何配置Mac OS X术语,使git有颜色?
- Visual Studio Code: .git文件夹/文件隐藏
- “node_modules”文件夹应该包含在git存储库中吗
- 为什么git-rebase给了我合并冲突,而我所做的只是压缩提交?
- 当我试图推到原点时,为什么Git告诉我“没有这样的远程‘原点’”?
- 如何从远程分支中挑选?
- 如何查看一个分支中的哪些提交不在另一个分支中?
- 如何取消在github上的拉请求?
- HEAD和master的区别
- GIT克隆在windows中跨本地文件系统回购
- RPC失败;卷度传输已关闭,剩余未完成的读取数据