Git: Automated Merge Function (Bash/Zsh)

I have noticed that I had a permanently repeating git workflow:

  1. Do some stuff and commit
  2. Checkout some deployed branch (i.e. ‘staging’)
  3. Update this branch (I am working in a team so this branch is also updated by other people)
  4. Merge my feature branch into staging branch
  5. Push staging branch
  6. Switch back to my feature branch

So I wrote a little function to automate┬ásteps 2 – 6. Here is the result: a little function which I called mcb (Merge Current Branch).


It takes one argument the target branch name to merge the current branch into.

$ mcb staging


Here is the script:

function mcb() {
 if [ "$1" != "" ]
   CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
   git checkout $1 && git pull && git merge $CURRENT_BRANCH && git push && git checkout $CURRENT_BRANCH
   echo "Error - Missing merge target branch - Doing nothing"
   echo "Usage: mcb <target branch>"

Simply put it into your ~/.bash_profile or ~/.zprofile and don’t forget to source it afterwards:

$ source ~/.zprofile
comments powered by Disqus