In the previous articles we have seen that it is possible to collaborate more on the product we are creating.
Once, however, that we have made our changes we will need to be able to insert them in the original branch and then be able to apply them to our code. This operation is called a union (trad. Union).
Git obviously has a command dedicated to this operation: git merge.
A---B---C branch_with_changes / ---D---E---F---G master
How then is the merge command used to be able to join the two branches? If admitted to be inside the main (master) branch, it is possible to execute the following command:
git merge branch_with_changes
Git will be responsible for unifying the two branches, combining the changes to our original code. If everything went well, we will get the following situation:
A---B---C branch_with_changes / \ ---D---E---F---G---H master
That is, Git will have created a new commit on the master branch with our changes in it.
All this obviously if the union process has been successful. Or? If the two branches that we are going to merge have changes inside that involve common portions of code, the merge will not be successful and the system will warn you that there is a conflict to be resolved.
here is some content not affected by the conflict <<<<<<< master this is conflicted text from master ======= this is conflicted text from feature branch
Generally the code before the marker ======= it is related to the branch that is receiving the changes, in our master case.
Once you have resolved all conflicts, simply perform a normal git commit to complete the operation.
The case covered by this example is also called fast forward merge: it is the most common case and occurs when a branch contains more recent code than the branch we want to integrate. A typical example of this workflow is when a branch is detached to add a function to the code: when the new functionality is ready, the branch will be merged with the main code.
If during the development of your new function on the secondary branch, you should intervene on the main code, during the merge operation Git will perform what is called “3-way merge”: from your point of view the final result will not change, but Git will perform some additional operations “under the hood” to be able to integrate all the changes. It goes without saying that if a 3-way merge was necessary, it is more likely that the code subject to merge will generate conflicts.
This series of articles can not obviously be exhaustive of the whole Git world or of versioning. I leave you then some references where you can deepen the topics seen in these days:
Official Git Documentation (https://git-scm.com/docs)
Atlassian Bitbucket tutorial (https://it.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud)