使用Git管理代码工程,着实方便了很多,但是当做完feature分支或者完成hotfix之后,总是忘记删除这些无用的分支,一个一个地删除着实麻烦,重复手工劳动不符合程序员的风格,于是写了一个简单的脚本。一键删除那些不需要的分支,让多余的干扰信息离开视线。
删除哪些分支?
删除的为Merge(合并)操作的源分支。如果工程正在处于分支A(HEAD为A分支),分支B已经合并到了分支A,即A分支包含了B分支的内容,则会删除B分支。
代码
复制代码 代码如下:
#!/usr/bin/env ruby
# encoding: utf-8
exceptBranches = ['master', 'pre', 'develop']
for branch in `cd #{ARGV[0]} && git branch -l`.split(' ') - ['*']
next if exceptBranches.include? branch
system("git branch -d #{branch}")
end
使用方法
复制代码 代码如下:
ruby removeMergedBranches.rb your_git_project
执行结果
执行结果类似如下,注意如果没有进行合并,则会提示警告或者错误,这些可以忽略。
复制代码 代码如下:
warning: deleting branch 'custom' that has been merged to
'refs/remotes/origin/custom', but not yet merged to HEAD.
Deleted branch custom (was b63ab7d).
Deleted branch hotfix (was 340cca0).
Deleted branch mgit (was 86b4004).
error: The branch 'develop_rtl' is not fully merged.
If you are sure you want to delete it, run 'git branch -D develop_rtl'.