如何删除已合并的分支?我可以一次删除所有分支,而不是逐个删除每个分支吗?
当前回答
我一直在使用以下方法在一个cmd中删除合并的本地AND远程分支。
我的bashrc文件中有以下内容:
function rmb {
current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ "$current_branch" != "master" ]; then
echo "WARNING: You are on branch $current_branch, NOT master."
fi
echo "Fetching merged branches..."
git remote prune origin
remote_branches=$(git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$")
local_branches=$(git branch --merged | grep -v 'master$' | grep -v "$current_branch$")
if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then
echo "No existing branches have been merged into $current_branch."
else
echo "This will remove the following branches:"
if [ -n "$remote_branches" ]; then
echo "$remote_branches"
fi
if [ -n "$local_branches" ]; then
echo "$local_branches"
fi
read -p "Continue? (y/n): " -n 1 choice
echo
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
# Remove remote branches
git push origin `git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$" | sed 's/origin\//:/g' | tr -d '\n'`
# Remove local branches
git branch -d `git branch --merged | grep -v 'master$' | grep -v "$current_branch$" | sed 's/origin\///g' | tr -d '\n'`
else
echo "No branches removed."
fi
fi
}
原始来源
这不会删除主分支,而是删除合并的本地和远程分支。一旦你在你的rc文件中有了这个,只需运行rmb,你会看到一个合并的分支列表,这些分支将被清理并要求确认操作。您可以修改代码,不要求确认,但最好保留它。
其他回答
tl;dr:git branch--format='%(if:notequals=main)%(refname:short)%(then)%(if:notequals=master)%。
看,妈妈,没有工具!
Git可以在不使用外部工具的情况下过滤分支,这意味着有一种跨平台、相对简单的方法可以从搜索中排除默认分支。要列出合并到“main”分支中的分支,不包括“main”分行本身:git branch--format='%(if:notequals=main)%(refname:short)%(then)%(refname:short,%(end)'--merged=main。对于“master”分支:git branch--format='%(如果:notequals=master)%(refname:short)%
我们还可以将两者结合起来,仅当分支名称既不匹配“main”也不匹配“master”时才打印分支名称:git branch--format='%(if:notequals=main)%(refname:short)%(then)%
最后一个附带一个小警告:您应该首先检查默认分支(在本例中为“main”或“master”),因为--merged没有值意味着“merged into HEAD”,它可能指向您要删除的分支之一。但是尝试删除当前的本地分支无论如何都不会起作用,因此如果您正在这样做,这不会有风险。
这种方法的另一个优点是没有多余的前导空格或星号可以过滤掉。
这些命令将为排除的分支打印空行,而不是不打印任何内容,但xargs会处理这一点。如果要使用xargs以外的其他方法处理行,可能需要对空行进行特殊处理(例如通过sed'/^$/d')
有关--format的更多信息,请参阅每个ref的git帮助。
我发现最简单的方法是只删除本地分支,而不是远程分支:
$git分支--合并|grep-v主|xargs-n 1 git分支-D
此命令将仅删除已合并到主分支中的分支。如果您不想删除其他分支(例如登台),请小心。
Git中没有命令会自动为您执行此操作。但是你可以编写一个脚本,使用Git命令来满足你的需要。这可以通过多种方式实现,具体取决于您使用的分支模型。
如果您需要知道分支是否已合并到master中,如果myTopicBranch已合并,则以下命令将不会产生输出(即,您可以删除它)
$ git rev-list master | grep $(git rev-parse myTopicBranch)
您可以使用Gitbranch命令解析出Bash中的所有分支,并对所有分支执行for循环。在这个循环中,您可以使用上面的命令检查是否可以删除分支。
如何在PowerShell控制台中删除合并的分支
git branch --merged | %{git branch -d $_.Trim()}
如果要排除master或任何其他分支名称,可以使用PowerShell Select String这样进行管道,并将结果传递给git branch-d:
git branch -d $(git branch --merged | Select-String -NotMatch "master" | %{$_.ToString().Trim()})
我一直在使用以下方法在一个cmd中删除合并的本地AND远程分支。
我的bashrc文件中有以下内容:
function rmb {
current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ "$current_branch" != "master" ]; then
echo "WARNING: You are on branch $current_branch, NOT master."
fi
echo "Fetching merged branches..."
git remote prune origin
remote_branches=$(git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$")
local_branches=$(git branch --merged | grep -v 'master$' | grep -v "$current_branch$")
if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then
echo "No existing branches have been merged into $current_branch."
else
echo "This will remove the following branches:"
if [ -n "$remote_branches" ]; then
echo "$remote_branches"
fi
if [ -n "$local_branches" ]; then
echo "$local_branches"
fi
read -p "Continue? (y/n): " -n 1 choice
echo
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
# Remove remote branches
git push origin `git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$" | sed 's/origin\//:/g' | tr -d '\n'`
# Remove local branches
git branch -d `git branch --merged | grep -v 'master$' | grep -v "$current_branch$" | sed 's/origin\///g' | tr -d '\n'`
else
echo "No branches removed."
fi
fi
}
原始来源
这不会删除主分支,而是删除合并的本地和远程分支。一旦你在你的rc文件中有了这个,只需运行rmb,你会看到一个合并的分支列表,这些分支将被清理并要求确认操作。您可以修改代码,不要求确认,但最好保留它。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别