askvity

How to Merge Branches in GitHub?

Published in GitHub Version Control 5 mins read

Merging branches in GitHub brings changes from one branch into another, integrating different lines of development. This is a fundamental process in version control workflows, allowing teams or individuals to combine completed features, bug fixes, or experiments into a main branch like main or master.

Understanding Branch Merging

At its core, merging takes the commits from one branch and applies them to another. GitHub facilitates this process primarily through Pull Requests (PRs), which offer a review stage before merging, but you can also merge directly using the command line or the GitHub Desktop application.

Ways to Merge Branches

There are several common methods to merge branches when working with GitHub.

1. Merging via a Pull Request (GitHub Web UI)

This is the most common and recommended method in a collaborative environment as it allows for code review and discussion before merging.

  • Create a Pull Request: Navigate to your repository on GitHub.com. If you recently pushed a branch, you'll often see a prompt to create a pull request. Alternatively, go to the "Pull requests" tab and click "New pull request".
  • Select Branches: Choose the base branch (where you want the changes to go, e.g., main) and the compare branch (the branch containing the changes you want to merge, e.g., feature-branch).
  • Review Changes: GitHub shows you a diff of the changes.
  • Create Pull Request: Add a title and description, then click "Create pull request".
  • Discuss and Review: Team members can review the code, leave comments, and suggest changes.
  • Merge Pull Request: Once approved and status checks pass, click the "Merge pull request" button. You'll typically have options like "Create a merge commit," "Squash and merge," or "Rebase and merge."

2. Merging Using the Command Line

If you prefer working locally or need more control, you can use the Git command line interface.

  • Checkout the Target Branch: Switch to the branch where you want to merge the changes into.
    git checkout main
  • Merge the Source Branch: Run the merge command, specifying the branch you want to merge from.
    git merge feature-branch

    Git will attempt to merge the changes. If there are no conflicts, the merge is completed automatically. If conflicts occur, you'll need to resolve them manually before committing.

  • Push Changes: After a successful merge (and resolving conflicts if necessary), push the updated branch to GitHub.
    git push origin main

3. Merging Using GitHub Desktop

GitHub Desktop provides a user-friendly graphical interface for Git operations, including merging.

As described in the reference:

  1. In GitHub Desktop, click Current Branch. This button is usually located at the top of the application window.
  2. Click Choose a branch to merge into BRANCH. This option appears in the branch dropdown menu. BRANCH here refers to the currently selected branch.
  3. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. The second BRANCH refers to the branch you selected in the previous step.
  4. If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button. You will need to resolve these conflicts before the merge can be finalized.

Once the merge is complete in GitHub Desktop, you will usually need to Push origin to update the branch on GitHub.com.

Handling Merge Conflicts

Merge conflicts happen when Git cannot automatically determine how to combine changes from two branches because the same lines of code (or nearby lines) were modified differently in both branches.

  • Identify Conflicts: Git (or GitHub Desktop) will notify you of conflicts.
  • Resolve Manually: Open the conflicting files in your code editor. You will see conflict markers (like <<<<<<<, =======, >>>>>>>). Manually edit the file to combine the desired changes from both branches.
  • Mark as Resolved: After editing the file, mark the conflict as resolved. In the command line, you git add the file. In GitHub Desktop, there are specific options for resolving conflicts.
  • Commit the Merge: Once all conflicts are resolved, commit the changes to finalize the merge.

Choosing the right method depends on your workflow and collaboration needs. Pull requests are excellent for team review, while command line and GitHub Desktop offer direct merging capabilities.

Related Articles