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.
Click New pull request to create a PR using the convert-os-to-platform branch.
GitHub brings you to 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.
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.
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.
Next, add a comment to the PR to make it easy for those who will review the PR to understand its contents.
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.
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.
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.
Delete a merged branch
At this point, you can delete convert-os-to-platform since it has been merged into main.
Select Delete 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.
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.
Select Contribute. If there are mergeable changes, GitHub will notify you, as shown below. Click Open pull request to begin the process.
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.
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.
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.