Git tip: A quicker, smarter way to merge fixes with the master
This blogpost will explain how you can speed up the process of merging your fixes with the master branch(es) of your projects.
Current approach at K15t Software
At K15t Software, we currently take the following approach when fixing issues in our Scroll add-ons:
- Don't commit fixes directly to the master.
- Instead, create a bugfix branch with the name of the issue key (e.g. EXP-317).
- Commit all changes to this branch, then merge it with the master using certain parameters.
While this method involves a few extra steps, it does produce a neat history graph like this one:
After committing the fix to the bugfix branch, this means you need to perform all of the following steps:
- Make sure the bugfix branch has no uncommitted changes.
- Return to the master branch.
- Perform the merge (
git merge --no-commit --no-ff <FIX_BRANCH_NAME>).
- Finally, write a commit message and perform the commit.
If you only need to do this occasionally, it’s not a huge problem – as long as you remember the above steps and the merge parameters. But if you have to go through this process many times over, it will quickly get tedious.
For example, making a minor CSS change to the default templates of our six Scroll Exporters involves updating ten different projects (all six exporters plus the corresponding four example templates on Bitbucket).
Speed up your bugfix merges
As this is too time-consuming to perform manually, we came up with the following script. You need to place it somewhere in your $PATH and then invoke it like this (yes, git can do that):
This will merge the fix with the master branch, using a default commit comment:
If you prefer, you can also specify a custom commit message:
The script also carries out the following checks before proceeding:
- Look for uncommitted changes: The script checks that you have no uncommitted changes in your branch.
- Make sure fixes are only merged with the master: If you are still in your bugfix branch, the script automatically switches to the master before performing the merge.
Download and use the script to speed up your own merges
You’d like to use our script to automate your own merges? Be our guest – here’s what you need to do:
- Download the script here: git-merge-fix (make sure you don’t change the name – prefix 'git-' and no extension).
- Place it somewhere in your $PATH.
- Make sure it’s marked as executable.
- You’re done.