9
GitHub provides facilities for creating, merging and deleting branches. It also has the capability of resolving conflicts that occur in the merging process.
Just to clarify a point, the merging process on GitHub is started by triggering a pull request. You may have seen pull requests mentioned on the GitHub website (and in virtually every piece of documentation that goes with it); there is a button and status for it on the repository home page.
The pull request originates in the communal working environment and it can be interpreted as a request to pull modifications into the repository. I cover it in terms of merging branches in this section (§ 9.5.3) and I cover it as a communal coding activity in section 10.
Where people refer to a pull request, they are generally referring to a process of merging modifications back into the repository.
First things:
By way of example, I will add a new legal page to the website (04-legal.html) in much the same way I did with other pages in previous examples. I will do this by creating a new branch adding the page to the new branch and then merging it back into the master branch.
The easiest way to create a new branch is from the repository home page. Open the lab-01-website home page:
Click the branch button to open the branch dropdown box:
In the d-04-legal.
box start typing the name of the branch to be created, in this case it isIf the branch already exists, GitHub will try to switch to it (hence the find), in our case the branch does not exist and GitHub is telling us it will create a new branch called d-04-legal from the latest commit on the master branch (the blue bit at the bottom, Figure 9.51). Hit to create the branch; GitHub will open the branch page for d-04-legal:
The branch button is now telling us we are on a new branch called d-04-legal. The commit number has not changed (we are still pointing to the latest commit on the master branch, see Figure 9.49).
It will catch you out (at least it does me) and you will find yourself on the wrong branch. It is possible to change the default branch, see § 9.5.6.
That’s it; we’ve created a new branch and switched to it.
From the d-04-legal branch page, click the button.
Call the new file 04-legal.html and add the following code:
<html lang="en"> <!-- Declare language --> <head> <!-- Start of head section --> <meta charset="utf-8"> <!-- Use Unicode character set --> <link rel="stylesheet" type="text/css" href="11-resources/01-css/style.css"> <title>PracticalSeries: Git Lab — legal</title> </head> <body> <h1>A Practical Series Website</h1> <h3> Legal Declaration </h3> <p>This page contains the legal information for the website.</p> </body> </html>
Code 9.2 d-04-legal branch—create new file: 04-legal.html |
And give it the commit message:
D: Incremental Build - 04-legal page |
---|
04-legal.html added. |
It should all look like this:
Click Figure 9.54.
to add the file to the repository. This takes us back to the branch pageThe new commit point is, in my case [9c7954b]; the new file 04-leagl.html is also visible.
The yellow bar informs us that the d-04-legal branch has just been pushed to the repository—technically it hasn’t been pushed, we did in GitHub directly in the remote repository
There is also a message just above the blue commit line; it says this “branch is 1 commit ahead of the master”, this all makes sense.
Wherever you look there are also prompts for
. Let’s look at that next.GitHub uses (by and large) public repositories—all the repositories we’ve looked at so far have been public repositories. This means that anyone can see them and can copy them.
People can even copy them with the intention of developing them further (this process has the rather vulgar name of forking†1 and I discuss how it works in § 10.1). Now, it is generally difficult to stop people doing this and GitHub (I think) actively encourages it (it is possible to block users, but this is intended only for abusive users and not just to stop people looking—it would be difficult to block every user of GitHub).
Clearly, though we don’t want every Tom, Dick or Harry modifying our code—hence the pull request. This allows some other user to take your code, modify it in their own account, and, if they think it’s good enough, send you a pull request with their changes.
The pull request alerts you to the fact that someone has made a modification and they are requesting that you pull their modification in to the repository. Only the owner (or those users given write permission to the repository) can grant this pull request and merge the modification into the repository.
Now in our example here, I (the owner of the repository) made a change to a particular branch; there has been no forking of the repository, no one has copied it and there is no communal working—just me adding a file. So why, you may wonder, is it banging on about pull requests?
The answer is it uses the same process no matter where the change originated (be it some other user who had copied, forked, the repository or me, the owner of the repository, making a change). So we use a pull request to merge the files.
Let’s do it.
†1 | I generally don’t like the names that Git and GitHub use, I think them all coarse, crude and vulgar; but then I’m English and we’re taught good manners from an early age. |
From the branch page click either the
button in the yellow bar or the button next to the button. They both do exactly the same:This takes us to the open pull request page Figure 9.56 (I haven’t shown all of the 04-legal.html file, the stuff in green—there was too much of it):
The important bit is at the top:
The base branch (master) is the branch to which the changes will be merged (the receiving branch as it were), the compare branch (d-04-legal) is the branch we are trying to merge from.
The bit following this, the ✔ Able to merge message is important, it tells us that there are no conflicts between the two branches (the branches can merge without issue).
There is not really anything else to add, the bit underneath, next to the user logo allows a message to be entered, by default it uses the commit message that was entered when the change was committed—add whatever additional information you require (in a pull request from another user, this would be where they explain what the change is and why it would be useful).
I’m just going to leave it with the default content. Click
:After creating a pull request, we’re taken to the pull request status page:
At this point absolutely nothing has happened.
Pull requests |
---|
Pull requests are just that, a request absolutely nothing happens to the contents of the repository when a pull request is generated. |
A pull request has been generated, but nothing in the repository has changed.
Making a pull request takes us to the pull request status page (Figure 9.58).The first thing to note is at the top (highlighted), we are in the pull request tab (we haven’t been here before). This is where all the open pull requests (wherever they were generated) are listed.
We just have one (hence the one in the tab), the one we generated above.
There are three things we can do with a pull request:
Reject it (hit the
button)Ask a question (enter text in the box and hit the
button)Accept it (hit the
button)Looking at these in turn:
To reject a pull request click the 1 in Figure 9.58. Closing a pull request does not delete it, it will be listed under closed pull requests, but it will stop it bothering you and it will not require any further interaction.
button, this is listed as pointA note of caution: closing pull requests can upset some people, some people don’t handle rejection well—best to let them down gently—as someone once said:
“never argue with strangers on the internet”
Next, the conversation. If you notice, we are actually in a sub-tab called
, it’s just under the green open image ().If you’re not sure about the pull request you can ask questions or make a statement “good idea, I’ll implement later”—or in my case “who are you? Stop bothering me. Go away”—type what you want in the box at the bottom and click the button (both labelled point 2 in Figure 9.58) and the message will be added to the pull request listing.
The final option is to accept the changes. Click the 3 in Figure 9.58.
button pointThe
button has various options:We want the default option,
. This is in keeping with my idea of the best practice for merging branches. Generally, the top option is the correct one (and it certainly won’t do any harm).Clicking
automatically generates a merge commit message (you can alter it if you want):I’m going with the default, click
.This still leaves us on the pull request status page:
We can now see the merged pull request in the pull request chain, it is telling us (next to the purple merge icon ) that the new commit is [6907e46]. At this stage the commit has been made and is in the repository.
However, GitHub (unlike Git and Brackets) has an undo button, the Figure 9.58). With GitHub we get to change our mind.
button will undo the change and take us back to the previous state (We’ve merged a branch by using a pull request (merged the d-04-legal branch on to the master branch), we can see this if we open the repository home page; it shows the latest version:
The master branch is now selected, the new 04-legal.html page is there and the latest commit is [6907e46]. There are still two branches ( tab), this is because we haven’t deleted d-04-legal. The tab is also showing no requests.
Click the
tab to open the pull request page:It is showing that there are no open commit requests and one closed request, the one we just made.
Clicking the closed tab (highlighted) will show the current status of the closed pull requests:
If you click the pull request itself (where it says D: Incremental Build) it will show you the details of the pull request, this is the same as Figure 9.61. The option is still there if you want to undo the pull request.
The d-04-legal branch still exists, go back to the repository home page (Figure 9.62), click the branch button and select from the drop down to switch to that branch.
Open the 11-resources folder and then the 01-css folder and finally open the style.css file:
Click the pencil symbol ( ) to edit the file and modify line 30 as follows
h3 { font-size: 2.5rem; color: #404030; }
It gives the h3 heading a grey colour.
The edited file looks like this, enter the commit message as shown:
Click d-04-legal branch page. It will once again inform us that d-04-legal is one commit ahead of master:
and then go back to theThe next thing is to modify the master branch in just the same way to make a conflict (i.e. change the style.css file).
Go back to the repository home page and make sure you are on the master branch.
Drill down to the style.css file open it and edit it (in just the same way we did above for the d-04-legal branch above).
This time make line 30 read:
h3 { font-size: 2.5rem; color: #000000; }
The h3 element is completely black, it looks like this:
Enter the commit message and
.Go back to the repository home page:
Now we’ve change style.css on both branches and they have conflicting changes.
Let’s try to merge them together by creating a pull request. Click either the
button or the button (they do the same thing). This time we get:Not looking so good this time.
The top bit is the same; we’re merging from d-04-legal to the master branch.
It’s the bit after that that is the problem ✖ Can’t automatically merge. But it does go on to say that we can still create the pull request; it just means we have to sort it out later.
The bit at the bottom shows the conflict (in case it can be fixed and the pull request redone). In our case I want to leave it, I want the conflict situation. Click the
button and let’s see what happens:Again we go to the pull request status page and this time we have:
The
button is greyed out, but now we have a button.As before, we could if we wanted, just reject the pull request by clicking the
button. We could also ask questions with the button (just like last time).What we’re really interested in is resolving the conflict, click the does it look familiar?
button—It wants us to make the correction (just like § 6.7.3 & § 8.5). This time I’m going to keep the first one (grey). Modify the file to make it look like this:
Click the
button.Now we get:
Click the commit changes button and it takes us back to the pull request status page but this time it looks like it did in § 9.5.3, we can merge the changes:
We do exactly the same as we did with the original merge, click
and in the message box, accept the default message by clicking .And that’s it, it’s committed. It will leave you on the pull request status page showing the closed pull request. All done.
The final thing with branches. Deleting the branch
Go to the repository home screen and click the branches tab (highlighted):
It opens the branches page:
To delete the branch just click the rubbish bin (highlighted), it will do it straight away (there is no “are you sure?” box). It does however give you the option to restore it afterwards.