An Example of Merging Parallel Branches in GitHub
Tags: github, opensource, beginners, osdc
Published: 2023-09-29 18:51:04

This week in my open-source course I am practicing creating and merging branches in my [textml](https://dev.to/ijacobscpa/textml-2gic) text-to-HTML converter located on my GitHub at https://github.com/ijacobs-cpa/textml. The program takes a `.txt` or `.md` file and converts it to a usable HTML file with proper tags. 

To practice creating and merging branches for my program. I decided to add 2 new features to my program:

1. Adding support for an optional `-l`, `--lang` language argument that specifies the <HTML lang=...> attribute in the root `<html>` element in the final converted HTML file.

- For example, `./textml.py french.txt --lang fr` would set `<html lang="fr">` the language to french in the root `<html>` element.

2. Adding the conversion of `---` in markdown files to the `<hr>` horizontal rule element in HTML.
 
For each of these new features, I created an issue on my projects repository:

- [Language Support Issue](https://github.com/ijacobs-cpa/textml/issues/10)

- [Horizontal rule Issue](https://github.com/ijacobs-cpa/textml/issues/11)

Once this was done I created a new branch for each issue on my local repository:

```
$ git checkout -b issue-10
$ git checkout -b issue-11
```

I then started to implement the first new feature into my code on its respective branch making sure not to add any changes to the main branch. 

After this was done I moved to the other branch to implement the second new feature:

`$ git checkout issue-11`

During this process, I found moments when I was testing the feature that I was working on and tried to run the program with the other already implemented feature from the other branch as an optional argument only to see it not work. This shows that if you are working on multiple unmerged branches make sure you understand the base limitations of your code. 

## Merging the Branches

After completing and testing the current issue branch's new feature I was ready to merge the parallel branches into the main branch and I started with the second one:

```git
$ git checkout main
...
$ git merge issue-11
Updating 5f96795..23098af
Fast-forward
 convertUtils.py          |  4 +++-
 examples/mdTest/test3.md | 11 +++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 examples/mdTest/test3.md
```

As can be seen above git uses a "Fast-forward" strategy merge as the unchanged main branch has no changes so no conflicts come up as the branches are merged.

I then went and merged the issue-10 branch with the main branch:

```git
$ git merge issue-10
Auto-merging convertUtils.py
Merge made by the 'recursive' strategy.
 README.md       |  6 +++++-
 convertUtils.py | 12 ++++++------
 textml.py       | 13 +++++++++----
 3 files changed, 20 insertions(+), 11 deletions(-)
```

This time when merging it used the recursive strategy as we are trying to merge the issue-10 branch into the main branch which has been merged already with the issue-11 branch essentially making this a 3-way recursive merge.

Thankfully, there were no merge conflicts and the issue branches were merged into the main branch successfully. I then pushed the changes to GitHub to finish up.

Here are links to the merge commits on GitHub:

- [Issue 10 Merge (recursive)](https://github.com/ijacobs-cpa/textml/commit/c1e4bff3aa7c9be4f370bd1ef5c2ea5991bb8a86)

- [Issue 11 Merge (fast-forward)](https://github.com/ijacobs-cpa/textml/commit/ed50a9dd372576254b56967892bac1511af24c7b)

## Final Thoughts

Working on branches seems like a very good way to go about adding changes to a project on GitHub as it provides a good way to modify and test your code without removing the original and makes a project compatible with the open-source community when wanting to work with others simultaneously.