Git - Branches and Merge

May 11, 2023

In the world of software development, version control systems play a crucial role in managing codebases effectively. Among these, Git has emerged as the most popular and powerful tool.

 

In my last blog post, I covered some of the basics of Git and how to connect a local computer's repository to a remote repository on GitHub. The end result was we successfully pushed a file to the remote on GitHub using Git commands in the MacOS terminal.

Now we move on to understanding Git branches and learning the basics of merging branches. This  is essential for seamless collaboration within a team of developers.

 

What are Git Branches?

 

Git branches are lightweight pointers to specific commits within a repository. They allow developers to work on multiple versions of a project simultaneously, making it possible to isolate and manage changes separately from the main codebase. Branches serve as independent workspaces where developers can experiment, implement new features, fix bugs, or make improvements without affecting the main codebase.

 

Why are Git Branches important?

 

Branches provide several benefits for collaborative development:

 

  • Isolation and Parallel     Development: With branches, team members can work on different features or     bug fixes simultaneously without interfering with each other's progress.     Each branch represents an isolated environment for development, ensuring     that changes can be made independently.

 

  • Experimentation and Testing:     Branches enable developers to experiment with new ideas, implement new     features, or apply fixes without disturbing the stability of the main     codebase. It allows for thorough testing and validation before merging     changes into the main branch.

 

  • Risk Mitigation: By keeping the     main branch (often called the "master" or "main"     branch) stable and deployable, branches mitigate the risk of introducing     bugs or breaking the production codebase. Changes are reviewed, tested,     and merged only when they are deemed ready.

 

Best Practices for Branching in Team Environments:

 

  • Descriptive Branch Names: Use     clear and descriptive branch names that reflect the purpose of the     changes. This helps other team members understand the context and purpose     of each branch.

 

  • Short-Lived Branches: Create     branches for specific tasks or features, and aim to merge them back into     the main branch promptly. Long-lived branches can lead to conflicts and     make the codebase difficult to manage.

 

  • Code Reviews: Encourage code     reviews for all branches before merging them into the main branch. This     ensures quality control, improves codebase consistency, and identifies     potential issues.

 

  • Merge Strategies: Choose an     appropriate merge strategy based on the team's preferences and project     requirements. Document and communicate these strategies to maintain     consistency.

 

Merging Branches

 

Now, let's explore the process of merging branches.

 

Example

 

Simple Branch Merge: Consider a scenario where we want to create a branch named "experiment" to test code in order to implement a new feature. Here is the code to create that new branch.

Now that the branch has been created, we can switch to that branch by using checkout to write and test out new code.

  

Smaller incremental changes can now be tested using the "experiment" branch without affecting the master branch. After we have reviewed, tested, and received approval to merge the "experiment" feature into "master".

Merge Conflicts

 

Merge conflicts occur when Git encounters conflicting changes in different branches during the merging process. Git will mark the conflicting sections in the affected files and prompt you to manually resolve the conflicts. It's essential to carefully analyze the conflicting changes and choose the desired outcome before finalizing the merge.

 

Example -  Conflict in a File

 

Let's consider a scenario where two branches, "experiment" and"master" and the developer has made conflicting changes to the same file script.js.

 

We can attempt to merge the "experiment" branch by switching to the master branch and executing the merge command. This will be unsuccessful.

  

Here Git has notified us of the conflict. To resolve the conflict, you need to manually edit the files, choosing which version of the code to keep or combining the changes. Here you can see in VS Code that Git has marked up the script.js file where the conflict is so we can clean it up.

Open the conflicted file in your editor and resolve the conflicts by choosing the desired changes. Remove the conflict markers (<<<<<<<,=======, >>>>>>>) once you have resolved the conflicts. Then run the following commands to complete the merge.

 

By manually resolving the conflicts and carefully selecting the desired changes, you can ensure that the merged codebase remains coherent and functional.

Remember, conflict resolution may vary depending on the specific changes and files involved. It's important to thoroughly review the conflicting sections, understand the intended changes, and make informed decisions to resolve the conflicts successfully.

 

Grab the Code

You can find the code from the examples above in one file here:

https://github.com/CodingInGreen/git-branches-and-merge