如何删除已合并的分支?我可以一次删除所有分支,而不是逐个删除每个分支吗?
当前回答
如果您使用的是HubFlow或GitFlow等分支模型,则可以使用此命令删除合并的功能分支:
git branch --merged | grep feature.* | grep -v "\*" | xargs -n 1 git branch -d
其他回答
如果您使用的是HubFlow或GitFlow等分支模型,则可以使用此命令删除合并的功能分支:
git branch --merged | grep feature.* | grep -v "\*" | xargs -n 1 git branch -d
我的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 --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-config-e--global):
cleanup = "!git branch --merged master | grep -v '^*\\|master' | xargs -n 1 git branch -D"
我使用gitbranch-D来避免错误:分支“somebranch”未完全合并。消息,而我的当前签出与主分支不同。
编写一个脚本,Git将检查所有已合并到master的分支。
然后做git结账大师。
最后,删除合并的分支。
for k in $(git branch -ra --merged | egrep -v "(^\*|master)"); do
branchnew=$(echo $k | sed -e "s/origin\///" | sed -e "s/remotes\///")
echo branch-name: $branchnew
git checkout $branchnew
done
git checkout master
for k in $(git branch -ra --merged | egrep -v "(^\*|master)"); do
branchnew=$(echo $k | sed -e "s/origin\///" | sed -e "s/remotes\///")
echo branch-name: $branchnew
git push origin --delete $branchnew
done
推荐文章
- 当我试图推到原点时,为什么Git告诉我“没有这样的远程‘原点’”?
- 如何从远程分支中挑选?
- 如何查看一个分支中的哪些提交不在另一个分支中?
- 如何取消在github上的拉请求?
- HEAD和master的区别
- GIT克隆在windows中跨本地文件系统回购
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?
- 跟踪所有远程git分支作为本地分支
- 为什么要把Gradle Wrapper提交给VCS?
- 自定义SSH端口上的Git
- git如何显示不存在于.gitignore中的未跟踪文件