A major reason for Git’s popularity is its ability to easily create and merge branches. Programmers can focus on separate branches at a modular level and then merge the branches at the end of the cycle.
Let’s say that you’ve been working on a new feature, feature_1_beta, in a separate branch for quite some time. The feature has been fully developed, tested, and now you need to integrate this feature into the overall application. This would require merging your feature_1_beta branch with the master.
First, you need to switch to master using the git checkout
command, as follows:
git checkout master
You can now execute the git merge
command to merge the new feature into the master branch:
git merge feature_1_beta
If this step is completed successfully, your feature_1_beta branch will be fully integrated with the master branch.
However, there is a chance that Git won’t be able to automatically resolve some conflicts, and you’ll have to resolve them manually. This normally happens when two branches have different parts of the same file and Git isn’t able to figure out which part to use. This case is shown in the terminal below:
When you open the conflict file in a text editor, you will see the conflicted part, like this:
/* code unaffected by conflict */
<<<<<<< HEAD
/* code from master that caused conflict */
=======
/* code from feature that caused conflict */
When Git encounters a conflict, it adds <<<<<<<
and =======
to highlight the parts that caused the conflict and need to be resolved. Once you have decided which part of the code to keep in the final master branch and have removed the irrelevant code (along with the conflict indicators), run git add
and git commit
commands on the conflicted files to generate the merge commit.