如何删除已合并的分支?我可以一次删除所有分支,而不是逐个删除每个分支吗?
当前回答
如果您使用的是HubFlow或GitFlow等分支模型,则可以使用此命令删除合并的功能分支:
git branch --merged | grep feature.* | grep -v "\*" | xargs -n 1 git branch -d
其他回答
$ git config --global alias.cleanup
'!git branch --merged origin/master | egrep -v "(^\*|master|staging|dev)" | xargs git branch -d'
(为便于阅读,拆分为多行)
调用“gitcleanup”将删除已合并到origin/master中的本地分支。它跳过master、staging和dev,因为我们不想在正常情况下删除它们。
将其分解为以下内容:
git-config--全局别名清理这将创建一个名为“cleanup”的全局别名(在所有回购中)这个在命令的开头,我们将使用一些非git命令作为别名的一部分,因此我们需要在这里实际运行bash命令git branch—合并的原始/主此命令返回已合并到原始/master的分支名称列表egrep-v“(^\*|master|staging|dev)”这将从已合并的分支列表中删除master、staging和dev分支。我们不想删除这些分支,因为它们不是功能。xargs git分支-d这将为每个未合并的分支运行gitbranch-d xxxxx命令。这将逐个删除本地分支。
git从git额外回购中删除合并的分支。
https://github.com/tj/git-extras/blob/master/Commands.md#git-删除合并的分支
我的Bash脚本贡献大致基于mmrobin的答案。
它需要一些有用的参数来指定include和exclude,或者只检查/删除本地或远程分支,而不是两者。
#!/bin/bash
# exclude branches regex, configure as "(branch1|branch2|etc)$"
excludes_default="(master|next|ag/doc-updates)$"
excludes="__NOTHING__"
includes=
merged="--merged"
local=1
remote=1
while [ $# -gt 0 ]; do
case "$1" in
-i) shift; includes="$includes $1" ;;
-e) shift; excludes="$1" ;;
--no-local) local=0 ;;
--no-remote) remote=0 ;;
--all) merged= ;;
*) echo "Unknown argument $1"; exit 1 ;;
esac
shift # next option
done
if [ "$includes" == "" ]; then
includes=".*"
else
includes="($(echo $includes | sed -e 's/ /|/g'))"
fi
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 -e "Fetching branches...\n"
git remote update --prune
remote_branches=$(git branch -r $merged | grep -v "/$current_branch$" | grep -v -E "$excludes" | grep -v -E "$excludes_default" | grep -E "$includes")
local_branches=$(git branch $merged | grep -v "$current_branch$" | grep -v -E "$excludes" | grep -v -E "$excludes_default" | grep -E "$includes")
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 [ "$remote" == 1 -a -n "$remote_branches" ]; then
echo "$remote_branches"
fi
if [ "$local" == 1 -a -n "$local_branches" ]; then
echo "$local_branches"
fi
read -p "Continue? (y/n): " -n 1 choice
echo
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
if [ "$remote" == 1 ]; then
remotes=$(git remote)
# Remove remote branches
for remote in $remotes
do
branches=$(echo "$remote_branches" | grep "$remote/" | sed "s/$remote\/\(.*\)/:\1 /g" | tr -d '\n')
git push $remote $branches
done
fi
if [ "$local" == 1 ]; then
# Remove local branches
locals=$(echo "$local_branches" | sed 's/origin\///g' | tr -d '\n')
if [ -z "$locals" ]; then
echo "No branches removed."
else
git branch -d $(echo "$locals" | tr -d '\n')
fi
fi
fi
fi
要删除已合并到主分支的本地分支,我使用以下别名(git-config-e--global):
cleanup = "!git branch --merged master | grep -v '^*\\|master' | xargs -n 1 git branch -D"
我使用gitbranch-D来避免错误:分支“somebranch”未完全合并。消息,而我的当前签出与主分支不同。
公认的解决方案很好,但有一个问题,它还删除了尚未合并到远程的本地分支。
如果你查看的输出,你会看到
$ git branch --merged master -v
api_doc 3a05427 [gone] Start of describing the Java API
bla 52e080a Update wording.
branch-1.0 32f1a72 [maven-release-plugin] prepare release 1.0.1
initial_proposal 6e59fb0 [gone] Original proposal, converted to AsciiDoc.
issue_248 be2ba3c Skip unit-for-type checking. This needs more work. (#254)
master be2ba3c Skip unit-for-type checking. This needs more work. (#254)
分支bla和issue_248是将被默默删除的本地分支。
但您也可以看到单词[gone],它表示已被推到远程(现在已不存在)的分支,因此表示可以删除分支。
因此,原始答案可以更改为(拆分为多行以缩短行长度)
git branch --merged master -v | \
grep "\\[gone\\]" | \
sed -e 's/^..//' -e 's/\S* .*//' | \
xargs git branch -d
以保护尚未合并的分支。此外,也不需要为master提供保护,因为它在源位置有一个远程,不会显示为已删除。