16. Merging: Integrating Branches

The merge process in Git is one of the fundamental operations that allows the integration of changes from one branch to another. In software projects, where several lines of development occur in parallel, merging is essential to combine different features or bug fixes into a single version history. In this chapter, we'll explore the concepts and best practices for performing efficient and secure merges with Git and GitHub.

Understanding Merge in Git

merge is used to join the history of two or more branches. When you perform a merge, Git automatically tries to combine code changes from the branches involved. If Git can do this without user intervention, it's called merge fast-forward. However, when there are conflicting changes to the same pieces of code between branches, Git will not be able to merge automatically, resulting in a merge conflict, which must be resolved manually by the developer.

Merge Types

There are basically two types of merges in Git:

  • Fast-forward merge: Occurs when there are no conflicting changes between branches. Git simply moves the pointer from the branch being merged to the point in the branch being merged.
  • Three-way merge: When there are divergent changes in the branches, Git creates a new commit that represents the merger of these changes. This commit has two parents, representing each of the branches that were merged.

Performing a Merge

To perform a merge, you need to be on the branch that will receive the changes. For example, if you want to merge the feature branch into the main branch, you must first checkout the main branch:

git checkout main
git merge feature

If the merge is a fast-forward, Git simply moves the main branch pointer forward. If a three-way merge is necessary, Git will start the merge process and, if there are no conflicts, create a new merge commit.

Resolving Merge Conflicts

When Git encounters conflicts that it cannot resolve automatically, it stops the merge process and marks the conflicting files. It is the developer's responsibility to resolve these conflicts manually. To do this, you must edit the marked files, choosing the changes from each branch that should be kept or combined. After resolving the conflicts, you must add the files to the staging area and finish the merge with a commit.

git add <resolved-file>
git commit -m "Resolve merge conflicts between feature and main"

Good Merge Practices

To avoid complex merge issues, here are some best practices:

  • Keep branches up to date: Regularly merge the main branch (usually main or master) into feature branches to minimize divergence code.
  • Frequent small commits: Small, descriptive commits make it easier to understand changes and resolve conflicts.
  • Communicate with the team: Before carrying out significant merges, it is important to communicate with the team to avoid surprise merges that can cause major conflicts.
  • Test before merging: Ensure that the code in the feature branch is working correctly before merging into the main branch.

Using GitHub for Merge

GitHub offers a graphical interface for performing merges through Pull Requests (PRs). PRs are a great way to review code before it is merged into the main branch. They also provide a space for discussion about proposed changes.

To create a PR on GitHub:

  1. Push the feature branch to the remote repository.
  2. Go to the repository page on GitHub and click "Pull Request."
  3. Select the base branch (where the code will be merged) and the compare branch (the one containing the changes).
  4. Fill in the PR title and description and create the Pull Request.

After code review and necessary discussion, the PR can be merged directly into the GitHub interface, as long as there are no conflicts that need to be resolved locally.

Conclusion

Merging is a powerful tool that allows the integration of different lines of work in a software project. Understanding how to perform efficient merges and resolve conflicts is crucial to maintaining a smooth and collaborative development workflow. By following best practices and using recbears like Pull Requests on GitHub, teams can maximize efficiency and minimize risks associated with the merge process.

Now answer the exercise about the content:

Which of the following statements about the merge process in Git is true?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Resolving merge conflicts 17

Next page of the Free Ebook:

Resolving merge conflicts

Estimated reading time: 5 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks