Skip to content

Instantly share code, notes, and snippets.

@edenwaith
Last active September 20, 2017 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edenwaith/8118506 to your computer and use it in GitHub Desktop.
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>
#!/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}`
@scottwb
Copy link

scottwb commented Sep 20, 2017

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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment