DEV Community

Ian Jacobs
Ian Jacobs

Posted on

An Example of Merging Parallel Branches in GitHub

This week in my open-source course I am practicing creating and merging branches in my textml 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 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.
  1. 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:

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
Enter fullscreen mode Exit fullscreen mode

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 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
Enter fullscreen mode Exit fullscreen mode

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 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(-)
Enter fullscreen mode Exit fullscreen mode

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:

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.

Top comments (0)