Skip to content

Instantly share code, notes, and snippets.

@StuPig
Created November 7, 2014 08:35
Show Gist options
  • Save StuPig/06736fbdeede11001aff to your computer and use it in GitHub Desktop.
Save StuPig/06736fbdeede11001aff to your computer and use it in GitHub Desktop.
git分支清理脚本 script to clean git remote branches
#!/bin/bash
echo '***********************************';
echo '*';
echo -e '*\t分支清理';
echo '*';
echo '***********************************';
echo '';
# 切到主干分支develop,更新远程分支
git checkout develop && git remote update --prune;
# 清理本地分支
read -p $"是否清理远程已经合并的本地分支?[y/n]" clean_local;
if [[ $clean_local =~ ^[Yy]$ ]]; then
# 清理本地已merge的分支
git branch --merged | grep -v "master\\|develop" | xargs -n 1 git branch --delete;
echo "本地清理完毕。。。。";
echo '';
fi
# 清理远程已经合并分支
read -p $"是否清理远程已经合并的分支?[y/n]" clean_remote_merged;
if [[ $clean_remote_merged =~ ^[Yy]$ ]]; then
# 获取远程已经merge的分支
remote_merged_branches=`git branch -r --merged | grep -v "master\\|develop"`;
echo -e '\t\t-------已合并分支------'
for branch_info in $remote_merged_branches; do
echo $branch_info;
echo -e `git log -1 $branch_info --pretty=format:'%C(yellow)%h%Creset -%C(yellow)%d%Creset %Cgreen(%cr)%Creset %C(red)%ci %C(bold blue)<%an>%Creset %C(white)%s' --abbrev-commit --date=short`;
read -p $"[已合并分支] -是否删除该远程分支 $branch_info ?[y/n]" remove_merged
if [[ $remove_merged =~ ^[Yy]$ ]]; then
read -p $"删除前是否查看分支 $branch_info 提交历史?[y/n]" check_log;
if [[ $check_log =~ ^[Yy]$ ]]; then
git log $branch_info --graph --pretty=format:'%C(yellow)%h%Creset \
-%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' \
--abbrev-commit --date=short
fi
read -p $"确认删除分支 $branch_info? [y/n]" confirmed;
if [[ $confirmed =~ ^[Yy]$ ]]; then
echo $branch_info | sed -e 's/origin\//:/' | xargs git push origin;
# echo $branch_info | sed -e 's/origin\//:/' | xargs echo;
fi
fi
echo '';
done
echo "远程已合并分支清理完毕。。。。";
echo '';
fi
# 清理远程未合并分支
read -p $"是否清理远程未合并的分支?[y/n]" clean_remote_merged;
if [[ $clean_remote_merged =~ ^[Yy]$ ]]; then
# 获取远程未merge的分支
remote_no_merged_branches=`git branch -r --no-merged | grep -v "master\\|develop"`;
echo -e '\t\t-------未合并分支------'
for branch_info in $remote_no_merged_branches; do
echo $branch_info;
echo -e `git log -1 $branch_info --pretty=format:'%C(yellow)%h%Creset -%C(yellow)%d%Creset %Cgreen(%cr)%Creset %C(red)%ci %C(bold blue)<%an>%Creset %C(white)%s' --abbrev-commit --date=short`;
read -p $"[未合并分支] -是否删除该远程分支 $branch_info ?[y/n]" remove_merged
if [[ $remove_merged =~ ^[Yy]$ ]]; then
read -p $"删除前是否查看分支 $branch_info 提交历史?[y/n]" check_log;
if [[ $check_log =~ ^[Yy]$ ]]; then
git log $branch_info --graph --pretty=format:'%C(yellow)%h%Creset \
-%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' \
--abbrev-commit --date=short
fi
read -p $"确认删除分支 $branch_info? [y/n]" confirmed;
if [[ $confirmed =~ ^[Yy]$ ]]; then
echo $branch_info | sed -e 's/origin\//:/' | xargs git push origin;
# echo $branch_info | sed -e 's/origin\//:/' | xargs echo;
fi
fi
echo '';
done
echo "远程未合并分支清理完毕。。。。";
echo '';
fi
echo "分支清理完毕!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment