Merging branches

As mentioned previously, you might branch your code so that you can maintain a working version while testing out new features that could break your code or analysis. However, once you finish developing the new features, you may be ready to consolidate the two branches.

The main branch has a second branch for developing a new feature. The second branch has reached a commit labeled 'Finished new feature'.

This will ensure that your new features are incorporated into future analyses and will help keep your repository clean and easy to navigate. Combining two branches into one branch is known as merging.

A line now joins the last commit of the new feature branch to the main branch. A commit at the end of this line is labeled 'Merge new feature'.

Note that when you perform a merge, the histories of the two branches are also combined.

The commits on the new feature branch have been added to the main branch after merging.

Automatic Merges

In many cases, merging is simple. As long as two branches do not alter the same lines of a file, the file can be merged seamlessly. For example, say this is the ingredients list at a branch point:

8 Tomatillos, peeled and washed
2 Jalapenos, seeds and stems removed
1/4 white onion
2 garlic cloves, peeled
1 bunch fresh cilantro, roughly chopped
1/2 t salt

Then, the following two ingredients lists:

8 Tomatillos, peeled and washed
3 Jalapenos, seeds and stems removed
1/4 white onion
3 garlic cloves, peeled
1 bunch fresh cilantro, roughly chopped
1/2 t salt
8 Tomatillos, peeled washed and cut in half
2 Jalapenos, seeds and stems removed
1/4 white onion
2 garlic cloves, peeled
1 bunch fresh cilantro, roughly chopped
1/2 t salt

can be merged automatically because they alter different lines of the file. The left file changes the amount of jalapenos and garlic, while the right file alters the preparation instructions for the tomatillos. The final merged file would be:

8 Tomatillos, peeled washed and cut in half
3 Jalapenos, seeds and stems removed
1/4 white onion
3 garlic cloves, peeled
1 bunch fresh cilantro, roughly chopped
1/2 t salt

Merge conflicts

However, sometimes files cannot be merged automatically. This occurs when two branches alter the same lines of a file in different ways. When this happens, git doesn’t know which version of the line to use and cannot proceed; this situation is known as a merge conflict. For example, if the following file is the ingredient list at the branch point:

8 Tomatillos, peeled and washed
2 Jalapenos, seeds and stems removed
1/4 white onion
2 garlic cloves, peeled
1 bunch fresh cilantro, roughly chopped
1/2 t salt

Then the following two files:

8 Tomatillos, peeled and washed
3 Jalapenos, seeds and stems removed
1/4 white onion
2 garlic cloves, peeled
1 bunch fresh cilantro, roughly chopped
1/2 t salt
8 Tomatillos, peeled and washed
4 Jalapenos, seeds and stems removed
1/4 white onion
2 garlic cloves, peeled
1 bunch fresh cilantro, roughly chopped
1/2 t salt

would cause a merge conflict because they both edit the jalapeno line in different ways.

To resolve a merge conflict, choose a desired version of the line and copy it into the other branch.

New Vocabulary

In the next exercise, we will practice merging branches and resolving a merge conflict.

PreviousNext