A fundamental aspect of working on open-source projects is separating bug fixes, feature developments, and similar tasks into separate branches. Doing so lets developers experiment with code solutions free of the worry of messing things up in the main repository.

However, these offshoot branches must be merged into the main project when the developer’s done working on them.

To do this, you perform a git merge.

What is git merge?

Git merge is Git’s way of combining two separate development histories into one. In other words, you can merge code from a separate branch into the main branch to integrate the changes.

How does git merge work?

Git merge combines several sequences of commits into a single history. In most cases, that means merging two branches—most often a feature branch and the master branch.

In this case, Git will take the commits from the branch tips and try to find a common base commit between them. If it does, it’ll create a merge commit representing the blending of the merging branch with the receiving branch.

How to git merge successfully

Let’s run through how to use git merge the right way.

Preparing to merge

To prepare to merge, first, check that your HEAD is pointing at the right receiving branch. You can use git checkout to switch to the receiving branch.

Next up, make sure both the merging and receiving branches are completely up to date with remote changes. To do this, execute git fetch—this will pull in all the latest remote commits for you to look at. If everything looks good, run git pull to update your master branch.

Merging branches

Now that we’ve adequately prepared to merge branches, the merging process itself should be quite simple.

Simply execute a git merge X, where X is the branch you are merging into the main branch.

Doing this creates the new merge commit to bring things together.

At this point, if you’re done working on whatever you were working on in the branch being merged, you can delete that branch safely.

Merge conflicts

It’s always nice when merges happened seamlessly, but on occasion, you could run into merge conflicts.

Merge conflicts happen when both branches you’re trying to merge change some part of the same file. Sometimes, Git is able to figure things out here.

However, Git often isn’t able to decide which version it should use if multiple people made changes to the same line in a file (or something similar), so it gives up right before creating the merge commit. That way, you can solve the problem.

Solving merge conflicts

First, you need to understand what caused the merge conflict. Did someone delete a file you made changes to? Or maybe you added a file with the same name as an existing file.

Regardless, Git will inform you that you have “unmerged paths,” meaning conflicts stopped your branches from merging.

Fortunately, Git uses visual markers (<<<<<<<, =======, and >>>>>>>) to help you find the problem area easily.

The equal signs separate the two branches being merged. The branch below the equal signs is the branch you’re merging, whereas the branch above the equals signs is the receiving branch.

Once you find these conflicting sections, you can edit them until they work well. You may have to collaborate with the contributors/team members involved to make sure everything looks right.

After fixing the sections, it’s time to finish the merge. Execute git add on the conflicting file(s) to let Git know you’ve fixed everything. Then, execute a git commit command to create the merge commit and finish up.

No more manual merges

With Mergify, you no longer have to worry about manually executing git merge commands every time you need to merge a feature branch. The Mergify bot automates all of that.

You can simply set your rules for what can be merged in, then watch the bot go to work. It keeps you informed about when criteria match for a merge, and which criteria those are. Thanks to Mergify’s priorities feature in the merge queue, you can ensure only your most crucial pull requests get merged in first as well.

Mergify is free for open-source, and it only takes one click to add Mergify to your GitHub. There’s no risk, so sign up for Mergify today.