Skip to main content
  1. Learn
  2. Software Development
  3. Guides
  4. Git tutorial
  5. Git collaboration
  6. Integrating branches
  7. Merging branches
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Merging branches

You can integrate several branches by using the git merge command.

Consider the situation below. There are two branches: a bugfix branch with a few commits coming off the main branch.

Branch

In this case, merging bugfix back into main is not much of an issue. That’s because main has not changed since bugfix was created. Git will merge this by moving the main position to the latest position of bugfix. This merge is called a fast-forward.

Fast-forward merge

In the example below, however, main has been updated several times since bugfix was branched out. The changes from bugfix and main must be combined when a merge is executed on these two branches.

It has advanced more than when a branch is divided

For this sort of merge, a merge commit is created, and the main position is updated to the newly created merge commit.

Merge commit incorporating both changes

Even when a fast-forward merge is possible, you could still explicitly force it to merge without a fast-forward merge.

Non fast-forward merge

As shown above, a non-fast-forward merge leaves the bugfix branch as it is. This gives you a clearer picture of the feature branch bugfix. You can easily find where the feature branch starts or ends and track the changes made to the feature branch.

Best practices for merging branches

When it comes to merging branches in Git, following best practices ensures a smooth and efficient integration process. Here are some best practices to consider when merging branches:

  • Keep the target branch up to date: Before merging a branch, ensure that the target branch (e.g., main, develop) is up to date with the latest changes from the remote repository. Use git pull or git fetch followed by git merge to update the target branch before initiating the merge.
  • Review and test changes: It's important to review the changes made on the branch you want to merge. Reviewing the code helps catch any potential issues or bugs. Also, consider running relevant tests on the branch to verify that the changes work as intended.
  • Resolve conflicts: If there are conflicting changes between the source and target branch, Git will prompt you to resolve the conflicts manually. Take the time to carefully resolve conflicts by understanding the conflicting changes and selecting the appropriate code or modifications.
  • Merge with the pull request workflow: If you are using Git hosting platforms like Backlog, it is highly recommended to leverage the pull request workflow. Pull requests provide a platform for collaboration, code reviews, and discussions before merging. This ensures that the changes are thoroughly reviewed and approved by other team members.
  • Use appropriate merge strategy: Git offers different merge strategies, such as the default "recursive" strategy (git merge), the "ours" strategy (git merge -s ours), or the "theirs" strategy (git merge -s theirs). Choose the most suitable strategy based on the requirements of the merge and potential conflicts.
  • Monitor build and test results: After merging, make sure to monitor the build and test results to ensure that the integration did not introduce any regressions or issues. This step helps maintain the stability and quality of the project.
  • Delete merged branches: Once a branch has been successfully merged into the target branch, it is a good practice to delete the merged branch. This keeps the repository clean and reduces clutter.
  • Communicate changes: After merging, inform the team about the merged changes and updates. This ensures that everyone is aware of the modifications made to the codebase.

By following these best practices, you can streamline the merging process, maintain code quality, and promote effective collaboration within your team.

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life