21 October 2014

If you use GitHub or any Git-related tool that supports pull requests, you probably find yourself constantly deleting, pruning and merging branches to get your feature code into your main development branch. After doing this over and over, I decided there must be a simple way using bash to put together a function that would automate these steps:

  • Check out the master branch (or whatever your mainline development branch is)

  • Fetch all remote branches

  • Pull updates to the master branch

  • Prune remote branches that have been deleted

  • Delete my local copy of the feature branch that has been merged to master

I started with adding Git status information to my shell by following these instructions:

function parse_git_dirty {
  [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo " *"
}

function parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/ (\1$(parse_git_dirty))/"
}

Next, I added a new function to use the current branch (the one being merged/deleted/pruned) to turn the steps mentioned above into a command:

function prune_merged_branch () {
    prunedBranch=`echo "$(parse_git_branch)" | sed -e 's/(//g' | sed -e 's/)//g' | tr -d ' '`
    git checkout master;git fetch --all;git pull;git remote prune origin;git branch -D "$prunedBranch"
}

Now, I can run prune_merged_branch from the command line to do all of these steps in one swoop:

$ prune_merged_branch
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
Fetching origin
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (1/1), done.
From https://github.com/<username>/<repo>
   a29de3b..b10695c  master     -> origin/master
First, rewinding head to replay your work on top of it...
Fast-forwarded master to b10695cdd910f064964d10e4f6d3d9ac78e3c1c6.
Pruning origin
URL: https://github.com/<username>/<repo>.git
 * [pruned] origin/some-branch
Deleted branch some-branch (was 27da761).

comments powered by Disqus