DEV Community

Cover image for Merge into Master Often? Automate!
Phil Tenteromano for Vets Who Code

Posted on • Updated on

Merge into Master Often? Automate!

Working on a Repo

My engineering department is decently sized, about 50-60 engineers all pushing to (typically) the same repository. Changes happen all the time that force us to merge an updated master into a working branch. This is due to a number of different reasons. One being our heavy reliance on tests from a number of different frameworks, across a broad codebase.

Recently, I found myself constantly running git fetch and git merge origin/master. This redundancy irked me. I, like most sane developers, love the terminal and the command line. So I thought back to my school days were we played around with bash and making our own custom commands. So I decided to make things a little easier.

Automation

Note: I work on MacOS

This'll be quick! In my ~/.bash_profile I put the following code snippet:

# Load custom bash functions
if [ -r ~/.custom_cmds.sh ]; then
  source ~/.custom_cmds.sh
fi
Enter fullscreen mode Exit fullscreen mode

This simply looks for the ~/.custom_cmds.sh file, and loads it into every new bash session (by default from ~/.bash_profile).

Create the custom commands file and inside of it, simply put:

merge_master() {
  git fetch;
  git merge origin/master;
}
Enter fullscreen mode Exit fullscreen mode

This creates a bash function called merge_master. Of course, you can name it anything you want, but I went with this.

Afterwards, I ran chmod u+x ~/.custom_cmds.sh to ensure execution rights of the file. Now just restart the terminal, or run source ~/.bash_profile and Boom. You have your first custom command line function! While simple, it makes me a little happier - run it from any of your repositories with a simple merge_master.

With this bash syntax, we can now create any automated function we want that will simplify our command-line-lives. Just add it into your ~/.custom_cmds.sh file!

What else do you like to automate? Bash is incredibly powerful - the options are limitless!

Top comments (0)