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.
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.
Branches provide several benefits for collaborative development:
Now, let's explore the process of merging branches.
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 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.
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.
You can find the code from the examples above in one file here:
https://github.com/CodingInGreen/git-branches-and-merge