Git and GitHub: The Complete Guides - Chapter 6: GitHub Merging
Tags: github, webdev, beginners, git
Published: 2021-09-23 21:50:05

![goran-laying-on-a-scorpion](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnuznn5y7tgjp6e4bw6w.png)

In the previous chapters we talked about how to merge branches, how to solve merge conflicts using different methods, namely <u>fast-forward</u> and <u>three-way</u> merge and we also saw how we can change the _base commit_ using **Git Rebase.**

Until now we have been working locally, but with what we already understand it is time to work on **GitHub**.

####What we will cover here:

<ul>
<li>Merge branches on GitHub</li>
<li>Dealing with merge conflicts on GitHub</li>
</ul>

##Merge branches on GitHub

We will be merging branches on GitHub. Let's login to our repository and recall what we have.

![github-repo-recollection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77mlpmnrd2lbq8hv28ep.gif)

Firstly lets create a new branch, lets call it **new-feature**.

![creating-a-branch-on-github-gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pu2x8vjtg6zsx3ej2yew.gif)

Now lets make two commits on the **new-feature** branch. We will be editing the _style.css_ file.

![github-two-commits-on-feature-branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2badjhfkefk9de34fwn.gif)

After making the commits, our **new-feature** branch is two commits ahead of the **master** branch. Suppose we are happy with the changes, in order to merge them we can do the following.

1. First we switch to the **master** branch.

2. To the right of the screen you should see a green button that has **'compare and pull request'**, click on it.

We are taken to a new page. Lets dive into some key concepts.

3. To merge branches in GitHub we need to create a **pull request**. That is why we see the heading on top that says <u>'Open a pull request'</u>, below that you will see it says <u>'Create a new pull request by comparing changes across two branches'</u>.

4. Below that we see the branches that are being compared, namely **master** and **new-feature**. We see a green check mark next to it that says **'Able to merge'**. <u>'These branches can be automatically merged.'</u> This means we don't have any merging conflicts.

5. Below that we can see two input fields, where we may insert some comments.

![github-pull-request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/upxmt7je9gym75jrg2ys.gif)

If you scroll down below you will see the two commits we made and below that we see where in the file we made those changes.

6. Everything is ready to create a pull request, to do this we click the green button that says **'create pull request'**.

7. We are take to another page, where we see the name of the user who want to merge two commits into **master** from **new-feature**. Also down below we see it says <u>'This branch has no conflicts with the base branch'</u>.

8. We are all good to go! lets click the green button that says **'merge pull request'** and finally **'confirm merge'**.

9. As you can see the pull request has successfully merged and closed, and now we can delete the branch. No worries, you all set, the **new-feature** branch can be safely deleted by clicking the **'delete branch'** button.

![github-merge-pull-request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fi9vwjuz7gb8qmg1hfju.gif)

Now when we check the commits history, we will see three new commits, the first two commits describe the changes made in the _style.css_ file. The last commit says that we have merged the pull request from the **new-feature** branch.

This is the way we can mege branches on GitHub. In this case we were working on the repository from just one account. 

In the real world, when you want to *contribute* to someone else's repository, then you might not have the permission to merge the branches, you need to send a <u>pull request</u> to the **_owner_** of the repository, who will check the changes and then merge the branches. `More on this later...`

For now lets see how we can deal with merge conflicts on GitHub.

##Dealing with merge conflicts on GitHub

As it was the case on the local repository, sometimes we might have some merge conflicts on the remote repository as well. We will simulate a scenario to create a merge conflict and then show how to deal with the conflict.

Firstly lets create a new branch, lets call it **demo-1**.

![github-create-branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6vtxscdczja3g1javte.gif)

Now lets make two commits on the **demo-1** branch. We will be editing the _script.js_ file.

![github-commits-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ppe1bo8dd4pezwqqru7w.gif)

![github-commits-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wynrp19z9h0g0fgtbux9.gif)

After making the commits, our **demo-1** branch is two commits ahead of the **master** branch. Suppose someone else is working on the **master** branch and is making a change on the same line of the _script.js_ file and finally commit their changes.

![github-master-commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5ef8aus8jx8vtygsjej.gif)

>Since we are still working on the **demo-1** branch we have no idea that the _script.js_ file has been edited on the **master** branch yet. 

We are happy with the changes we made to the _script.js_ file on the **demo-1** branch and we are ready to merge them with the **master** branch.

To the right of the screen you should see a green button that has **'compare and pull request'**, click on it.

At first glance, you should already see the difference here, with the red colored text that says **'Can’t automatically merge'**, however next to it says <u>'Don’t worry, you can still create the pull request.'</u>

We are facing a merge conflict, because we tried to edit the same line of code from two different branches. But despite this we can still create the **pull request**.

Lets update the input field and then click the **'create pull request'** button. We are taken to the next page and it lets us know that <u>'This branch has conflicts that must be resolved'</u>.

Below the message we see the files where the conflicts are occuring, in this case it is the _script.js_ file and right next to it we see the button that says **'Resolve conflicts'**. click the button.

![github-merge-conflict-resolved](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1a2v3o1i3ojybsdsxto.gif)

We are then taken to a text editor enviroment, in here its the same as how it was when we ran into this problem locally, we are able to decide which code we want to keep.

Once we have decided, we can click the button **'mark as resolved'**, then click **'commit merge'**.

We are all good to go! lets click the green button that says **'merge pull request'** and finally **'confirm merge'**.

As you can see the pull request has successfully merged and closed, and now we can delete the branch. No worries, you all set, the **demo-1** branch can be safely deleted by clicking the **'delete branch'** button

![github-merge-conflict-resolved](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzue7czli7jxle80j9zn.gif)

If we check the commits history we will see a couple of new commits that were automatically made during the process of merging.

![gihub-merge-conflict-resolved-logs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvp5kza7n52yt5s3bi40.gif)

This is how we can resolve merge conflicts on GitHub. Next we are going to discuss how to copy our remote repo on our GitHub account. How we can clone it to our local computer and how to contribute to someone else's repo.

Thanks you reading and enjoy the rest of your day...

![neo-says-thank-you](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym9w2z7rhxn694aluu9h.gif)