使用Git管理代碼工程,著實方便了很多,但是當(dāng)做完feature分支或者完成hotfix之后,總是忘記刪除這些無用的分支,一個一個地刪除著實麻煩,重復(fù)手工勞動不符合程序員的風(fēng)格,于是寫了一個簡單的腳本。一鍵刪除那些不需要的分支,讓多余的干擾信息離開視線。
刪除哪些分支?
刪除的為Merge(合并)操作的源分支。如果工程正在處于分支A(HEAD為A分支),分支B已經(jīng)合并到了分支A,即A分支包含了B分支的內(nèi)容,則會刪除B分支。
代碼
復(fù)制代碼 代碼如下:
#!/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
使用方法
復(fù)制代碼 代碼如下:
ruby removeMergedBranches.rb your_git_project
執(zhí)行結(jié)果
執(zhí)行結(jié)果類似如下,注意如果沒有進(jìn)行合并,則會提示警告或者錯誤,這些可以忽略。
復(fù)制代碼 代碼如下:
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'.
您可能感興趣的文章:- idea+git合并分支解決沖突及詳解步驟
- Python如何使用Gitlab API實現(xiàn)批量的合并分支
- Git分支合并沖突解決的方法實現(xiàn)
- 詳解git的分支與合并的兩種方法
- git分支的創(chuàng)建、切換、合并及刪除操作小結(jié)
- Git創(chuàng)建子分支,合并分支并提交