💻
How to Use Project Development Tools in a Remote C
  • How to Use Project Development Tools in a Remote Coding Team
  • Trello
    • Introduction to Trello
    • How to use Trello
      • What is Trello?
      • Starting up a Trello Board
      • Lists
      • Cards
  • Visual Studio Code
    • Introduction to Live Share in Visual Studio Code
    • How to use Live Share
      • How to Install
      • How to Use
      • Examples of Use Cases
  • Git & Github - Basics
    • Introduction to Git & GitHub
    • How To Use Git
      • Setting up Git
      • Creating GitHub Repositories
      • Managing Repository Access
      • Syncing with a Local Workspace
  • How to Manage a Codebase with Git
    • Introduction to Branching
    • How To Use Branches
      • What are Branches?
      • Working in Branches
      • Merging Branches
      • Best Practices For Branching
    • Git Codebase Management Cheat Sheet
  • Conclusion
  • Team Biography
  • References
Powered by GitBook
On this page
  • Merging Locally With Command Line
  • Merging With GitHub
  • Recap

Was this helpful?

Export as PDF
  1. How to Manage a Codebase with Git
  2. How To Use Branches

Merging Branches

PreviousWorking in BranchesNextBest Practices For Branching

Last updated 4 years ago

Was this helpful?

When we wish to combine the changes in two branches. We do this by merging them.

Merging Locally With Command Line

To merge, switch branches to the desired destination branch (we will use main, continuing with our example as before) and run

git merge <source branch name>

If there are so-called conflicts, conflict markers (determined by <<<<<<<, representing the code in current branch, or >>>>>>>, representing the new, conflicting code. They are separated by =======) will be in the files, and the user must resolve all conflicts (make sure to remove the markers, too), making necessary changes to ensure the code from both branches works. Conflicts occur when two branches make changes to the same piece of code, which makes it unclear which to keep or how to combine them.

When this is done, commit your changes. Then you should run:

git merge --continue

and then push your changes to the remote repository. To abort a merge, run:

git merge --abort

In scenarios where there are no conflicts, git merge should work immediately.

Merging With GitHub

Manually resolving conflicts like this is important to know how to do, but can be difficult and time-consuming. GitHub provides an interface for doing this, with an automatic merging algorithm. It will also tell you when things are too complicated for it to figure out, and to complete the merge manually as we have shown above in cases where you absolutely must. But usually, a merge completely on the GitHub UI is possible. In any case, it is always worth creating the so-called "pull request" to merge two branches on GitHub for three reasons:

  1. The request will be on GitHub for other coworkers to see, letting them to comment, review the code, and agree with you to merge it before moving forward. Merging is as much a communication tool as it is a code management tool, and leaving a paper trail when you do it is important for the same reasons as using revision control in the first place

  2. The aforementioned automatic merging feature is far easier to use than merging manually and locally

  3. If you still need or want to merge manually and locally, it is still possible to do so

To execute a merge on GitHub, click on the "Pull Request" button, which will be in a bar under the main navigation bar with the branches dropdown.

This opens the pull request window, where GitHub will notify you if an automatic merge is possible, and for you to name the request and leave a description/comments before submitting with the button on the bottom right:

In our example case, we see that the automatic merge can not be completed. In the pull request window you will be routed to after creating it (or accessible by navigating to the pull requests tab), there will be a button to resolve conflicts, among the other features of the merging UI on GitHub such as commenting, and - ultimately - approval of the request.

To resolve conflicts in GitHub, clicking that button will lead you to a window showing you the conflicts and letting you decide what to do, much like in the manual case. Other hosts for Git (ie. GitLab) may offer a user interface that lets you pick and chose what code from what branch to keep.

In our example here we can resolve the conflict by including the contents of both files:

With no more conflicts, we use the "Mark as Resolved" button on the top right and the "Commit Merge" button that pops up.

It is worth noting that at this point the merge has not been executed, just - whether a commit was created to resolve conflicts or not - that it is ready to be merged. To actually execute the merge, use the "Merge Pull Request" button that should appear once there are no conflicts.

With the merging complete, we may now view how our two example branches have "converged" as our new branch was integrated into main through the merge:

The additional commit on the example branch was the commit where we resolved the merge conflicts.

Recap

In summary, you now know how to combine code in different branches back together, once they are done being worked on. Specifically, we covered:

  • Starting the merge process in the command line

  • What merge conflicts are

  • How to resolve conflicts

  • How to complete or abort a merge in the command line

  • How to request merges (called "Pull Requests") on GitHub

  • How to resolve conflicts that GitHub cannot automatically resolve within the web interface

  • How to approve merges on GitHub, thereby executing the desired merge

  • What merged branches look like in the revision tree

A merge conflict in the example file. Conflict markers indicate which copy of the text is from which branch
A notification that a branch is ahead of "main", with option for pull request
The pull request interface on GitHub
The merge conflict in GitHub's interface
Resolving the conflict on GitHub
GitHub merge confirmation popup
The revision tree after conflict resolution and merge