Skip to content

Instantly share code, notes, and snippets.

@jeroenvisser101
Last active December 19, 2018 16:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenvisser101/8ec0cca627ae5301311f to your computer and use it in GitHub Desktop.
Save jeroenvisser101/8ec0cca627ae5301311f to your computer and use it in GitHub Desktop.
Merge a branch using --ff-only, check status checks, and remove branches locally and on remote.
#!/usr/bin/env ruby
def notice(text)
puts "\e[33m#{text}\e[0m"
end
def success(text)
puts "\e[32m#{text}\e[0m"
end
def error(text)
puts "\e[31m#{text}\e[0m"
exit 1
end
# Update the branch, and if it fails, throw error
unless system('git fetch 2> /dev/null')
error "You can only finish a branch if you're in a git repository"
end
# Only merge local branches
current_branch = `git rev-parse --abbrev-ref HEAD`.chomp
if current_branch == "HEAD"
error "You should only finish local branches."
end
# Don't finish the master branch
if current_branch == "master"
error "You cannot finish the master branch."
end
# Make sure ci is passing
ci_status = `hub ci-status`.chomp
unless ci_status == "success"
error "You should only finish branches that are passing ci. (current status: #{ci_status})"
end
# Make sure branch is not behind origin/master
commits_behind = `git rev-list --left-only --count origin/master...#{current_branch}`.chomp.to_i
if commits_behind > 0
error "Your branch is behind origin/master, first rebase onto origin/master and run again."
end
# Checkout master
notice "Checking out master..."
puts `git checkout master`
# Do a fast-forward merge (just move the tag)
notice "Merging branch (fast-forward)..."
puts `git merge --ff-only #{current_branch}`
# Push changes
notice "Pushing changes to remote..."
puts `git push`
# Delete local and remote branch
notice "Deleting branches (local and remote)..."
puts `git branch -d #{current_branch}`
puts `git push origin --delete #{current_branch}`
# Finish program
success "Finished branch '#{current_branch}'."
@jeroenvisser101
Copy link
Author

Requirements:

  • hub
  • git
  • ruby

Installing:

wget https://gist.githubusercontent.com/jeroenvisser101/8ec0cca627ae5301311f/raw/git-finish
mv git-finish /usr/local/bin/git-finish
chmod +x /usr/local/bin/git-finish

@MauricioKruijer
Copy link

xwbdbym

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