Last active
September 20, 2017 19:13
-
-
Save edenwaith/8118506 to your computer and use it in GitHub Desktop.
git-branch-delete Delete both a local and remote git branch with one command.Place this file in your /usr/local/bin/ directory.Usage:git branch-delete <name_of_branch>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# git-branch-delete | |
# | |
# Deletes a branch both locally and remotely in the origin. | |
# | |
############################################################################### | |
# Usage | |
############################################################################### | |
usage=<<EOT | |
Usage: | |
git branch-delete <branch-name> | |
Deletes the specified branch both locally and remotely in the origin. | |
PARAMETERS: | |
branch-name: | |
The name of the branch to delete. | |
EOT | |
############################################################################### | |
# Setup | |
############################################################################### | |
require 'getoptlong' | |
GetoptLong.new( | |
['--help', '-h', GetoptLong::NO_ARGUMENT] | |
).each do |opt, arg| | |
if opt == '--help' | |
puts usage | |
exit | |
end | |
end | |
############################################################################### | |
# Main Program | |
############################################################################### | |
if ARGV.size < 1 | |
puts usage | |
exit | |
end | |
if ARGV[0] == 'master' | |
puts "\n\nSorry, I can't let you delete 'master'." | |
exit | |
end | |
branch_name = ARGV[0].strip | |
current_branch = `git branch | grep "^\*"`[/^\*\s+(.+)$/,1].strip | |
if branch_name == current_branch | |
puts "\n\nSorry, you can't delete the current working branch." | |
puts "Switch to another branch first." | |
exit | |
end | |
`git branch -D #{branch_name}` | |
`git push origin :#{branch_name}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks a lot like https://github.com/scottwb/dotfiles/blob/master/bin/git-branch-delete ;) (Glad you found it useful...I still use it to this day!)