Working on Parallel Branches and Merging with Git
Tags: opensource, java, beginners, programming
Published: 2023-09-27 02:00:11

For this lab, I decided to improve my [ConvertTxtToHtml](https://github.com/WangGithub0/ConvertTxtToHtml) repository. I chose two features to implement:

Language Attribute Flag: I aimed to add an optional flag, --lang, which would allow users to specify the language for HTML documents. For example, --lang fr would set the language to French, resulting in <html lang="fr">. The default language would be Canadian English (en-CA).

Add support for a horizontal rule in Markdown: The Markdown `---` should get converted to an `<hr>` tag.

Step 1: Issue Creation and Branching
Before diving into coding, I created two GitHub [issues](https://github.com/WangGithub0/ConvertTxtToHtml/issues/9,https://github.com/WangGithub0/ConvertTxtToHtml/issues/10). This step was crucial as it helped me plan and track my progress effectively. The issues contained detailed descriptions of the changes I intended to make. Next, I created two topic branches—one for each feature—off the main branch. These branches would serve as isolated spaces for developing the respective features.

```
$ git checkout main
$ git checkout -b issue-9
$ git checkout -b issue-10
```

Step 2: Feature Implementation
I started implementing both features simultaneously, as they required modifying different parts of my codebase. It's worth noting that I made a conscious effort not to mix up changes between the two branches. Each branch was dedicated to its respective feature.

I diligently used git add and git commit to track my progress, ensuring that all commits went to their corresponding branches.

Step 3: Merging the First Feature
Once I completed the issue-10, it was time to merge it into the main branch. This merge went smoothly since I hadn't made any changes on main while working on the feature branch. Git performed a fast-forward merge, aligning the main branch with the feature branch.

```
$ git checkout main
$ git merge issue-10
```

Step 4: Merging the Second Feature
Merging the issue-9 was a bit more complex. Since I had worked on other branches (including main) while developing this feature, Git couldn't perform a fast-forward merge. It required a three-way recursive merge, and I encountered merge conflicts.

Resolving merge conflicts in `src/ConvertTxtToHtml.java` file can be a bit intimidating, but it's a valuable skill. I carefully examined the conflicted files, made the necessary adjustments to resolve conflicts, and then committed the changes.

```
$ git checkout main
$ git merge error-code-feature
```

Step 5: Testing and Refining
After merging both feature branches, I extensively tested my main branch to ensure that everything still worked as expected. It's crucial to perform thorough testing at this stage to catch any regressions or issues introduced during the merge.

Step 6: Pushing to GitHub and Closing Issues
With a stable and well-tested main branch, I pushed my changes to my GitHub repository:
```
$ git push origin main
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10sv5lima8au0xuhyaao.png)


Finally, I closed the GitHub issues related to the features. In each issue's comment, I provided a link to the merge commit on GitHub that closed the feature.

Conclusion
Working on parallel branches and mastering Git merges is essential for every developer. This exercise allowed me to gain practical experience in managing multiple features simultaneously, resolving merge conflicts, and ensuring the stability of the main branch.


In future projects, I'll continue to apply these Git skills to maintain codebase integrity and collaborate effectively with my teammates.