Merging branches in Git is a way to incorporate changes from one branch into another branch. In the previous article in this series, we looked at the basics of branching in Git and GitHub, as a means of achieving isolated lines of development for parallel work and feature separation. Once the code on a branch has been judged production-ready, it can be merged into the production-ready line.
Avatar
Latest posts by Edem Afenyo (see all)

This article is a continuation of our discussion on branches and picks up where the previous article left off. To follow along, it is important that you check out the previous article for context, code samples, and the required setup of your environment.

Git and GitHub both allow you to merge branches, essentially bringing in changes from one branch to the other. In GitHub, merges are performed via pull requests (PR). A PR is a mechanism for reviewing and taking action on changes submitted to a branch. The changes can either be accepted (via merging) or rejected.

Between collaborators, PRs become an entry point for discussions on changes and can trigger a series of commits to bring the branch to an acceptable state before merging. They also allow control over what code is merged into a codebase.

In this section, you will perform a PR to merge the feature branch, convert-os-to-platform, into main on GitHub. A feature branch is a branch created to develop and test a feature before it is introduced to the production-ready version of an application (usually the main branch).

Create a pull request

Open GitHub, and then click the repo that you created in the tutorial. Click the branches tab, as in the screenshot below, to view a list of branches for your repo on GitHub. Here, we see main and convert-os-to-platform, which we pushed to GitHub in the previous article.

Creating a pull request

Creating a pull request

Click New pull request to create a PR using the convert-os-to-platform branch.

Creating a pull request 1

Creating a pull request 1

GitHub brings you to the Open a pull request page.

Editing the Open a pull request page

Editing the Open a pull request page

First, as part of comparing branches for the PR, set the base repository to your fork using the dropdown menu. You can identify your fork by your GitHub username (Gboom345, in my case) and the name you gave the fork (branches in this guide). You will see other forks from other GitHub users in the dropdown menu. The base repo is the repository that receives changes in a merge operation.

Changing the base repo

Changing the base repo

Once that is set, the comparison is simplified to just the branches of your fork. The base branch is also main since it receives changes from convert-os-to-platform.

Comparing changes across branches of the same repo

Comparing changes across branches of the same repo

On the same page, compare the changes between the two branches being merged before you create the PR. These are the changes that we committed in the previous article to the convert-os-to-platform branch.

As shown in the screenshot below, compared to the main branch, lines that were removed from check-system.py are marked with a minus (-), and those that were added are marked with a plus (+). This format is used by Git-based version control systems to show differences between two versions of a file.

Viewing changes to files

Viewing changes to files

Next, add a comment to the PR to make it easy for those who will review the PR to understand its contents.

Providing comments for a pull request

Providing comments for a pull request

You can directly assign the PR to specific collaborators for their attention. Collaborators are contributors to a project that you or other collaborators invited. In this scenario, since you are the sole collaborator, you can assign it to yourself and apply a label for easy cataloging of PRs. Finally, click Create pull request to create the PR.

Creating a pull request assigning and labelling

Creating a pull request assigning and labelling

Merge a pull request

After clicking Create pull request, you're directed to the Pull Request Review page, where you can review changes, check merge conflicts, and finalize or modify pull request details before official submission. As you can see in the screenshot below, no conflicts exist between the base branch and the pull request branch. The pull request branch is the branch from which you originated the pull request. Conflicts typically occur when changes in the pull request branch and the base branch affect the same lines or sections of a file differently, and Git can't determine which change to prioritize. I will cover this topic in my next post.

Merging a pull request

Merging a pull request

When you click Merge pull request, you're taken to a confirmation page, and only after clicking Confirm merge are the changes from the feature branch (convert-os-to-platform) integrated into the target branch (main), creating a new merge commit.

Confirming pull request merge

Confirming pull request merge

Delete a merged branch

At this point, you can delete convert-os-to-platform since it has been merged into main.

Select Delete branch.

Deleting a merged branch

Deleting a merged branch

You should receive a notice that the branch has been deleted. That being said, you can restore the branch if you need it back for any reason.

Confirming a branch has been deleted

Confirming a branch has been deleted

Contribute to a branch

You just merged two branches in the fork you created from my repo. I will now explain how to contribute code from the new main branch in your fork to my original repo. Thus, you will set up a merge between the main branch of your repo and the main branch of my repo.

Click Code to go back to the Code tab to initiate the PR creation process.

Navigating to the code tab

Navigating to the code tab

Select Contribute. If there are mergeable changes, GitHub will notify you, as shown below. Click Open pull request to begin the process.

Contributing upstream to a forked repository

Contributing upstream to a forked repository

For the base repository, select my original repo oddjobsincorporated/branches. Then, choose the main branch as the base. The head repository is your forked repo, and the branch you want to contribute is your main branch.

When done, select Create pull request to create the PR.

Supplying details for a pull request

Supplying details for a pull request

Note that, unlike in the screenshot, on your GitHub page, you will see your own head repository and branch.

At this point, you will receive confirmation that a PR has been opened. You now have to wait for the maintainers (in this case, I am the maintainer) to review the changes and make a decision on the inclusion of your contributions. You will be notified once your code has been reviewed.

If you decide against contributing before your changes are merged or rejected, you can close the PR by clicking the Close pull request button in the screenshot below.

Viewing an open pull request

Viewing an open pull request

Conclusion

In this second post of the Git/GitHub beginner's guide, you learned how to merge branches in your repository and contribute to the repository of a different owner by merging your main branch with the main branch of my repository.

Subscribe to 4sysops newsletter!

I used GitHub to merge branches. Of course, you can also merge branches using the Git command line, which I will cover in my next post.

Articles in seriesA Git/GitHub beginner's guide
0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account