6
I’m going to demonstrate merging branches in exactly the same way I did theoretically in section 2.4.
I’ll start by adding another page to the lab-01-website. The master branch still has the latest version of the deployable code (although d-01-intro is more advanced than the master branch, it is under development—new branches should always be based on the latest deployable code, see appendix b).
I’m going to add a new 02-about.htm page and I’m going to do so by creating a new branch from the [25c1410] commit point on the master branch. Switch back to the master branch, create a new branch d-02-about and switch to this new branch.
Now we have Figure 6.56
Time for another file. In the new d-02-about branch add 02-about.html in the root folder (same place as index.html) and add the following code to it:
<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 — About us</title> </head> <body> <h1>A Practical Series Website</h1> <h3>About Us</h3> <p>This page explains who we are and how we came to be doing this.</p> </body> </html>
Code 6.7 d-02-about branch—create new file: 02-about.html |
Save the file, and in the Git pane tick the box to stage the file:
Commit the changes with the commit message:
D: Incremental Build - 02-about page |
---|
02-about.html added. |
Give the new commit point the tag d-P01.02.01. In my case the commit point is [3f15582].
I now have four commits:
The workflow is:
I’m going to do some jumping about between branches here to set up the merge situation. First switch to d-01-intro and change the style.css file:
h1, h2, h3, h4, h5, h6 { /* set standard headings */ font-family: sans-serif; font-weight:normal; font-size: 3rem; padding: 2rem 5rem 2rem 5rem; } h3 { font-size: 2.5rem; color: #4F81BD;}
Code 6.8 style.css modified in d-01-intro |
The only thing I’ve done is to change the colour of the h3 element in line 30; I’ve made it blue by adding a colour declaration (see below):
h3 { font-size: 2.5rem; color: #4F81BD; }
Save the file, tick the commit the change to style.css with the commit message:
andS: Staged - 01-intro page |
---|
Style.css modified <h3> colour changed to blue. |
It makes the page look like this:
My commit point is [f5aeb76]. Tag the commit with the label s-P01.01.P02. This will be the last commit on this branch (hence the tag, see appendix b).
We are now at this point:
The next thing is to merge the two commits on the d-01-intro branch onto the master branch, like Figure 6.62:
This is something new, we haven’t merged a branch with Brackets yet, this is how you do it.
I want to merge the d-01-intro branch back onto the master branch.
The first thing is to switch to the receiving branch. In this case we need to be on the master branch. To do that; click the small arrow next to the d-01-intro branch in the file tree and in the dropdown select master:
Back on the master branch, if you open the history, you will only see the first two commits:
Also, the 01-intro.html file and the 02-about.html files are missing (these don’t exist on the master branch). I’m showing the style.css file in Figure 6.64, you can see here that the master branch version doesn’t have the h3 changes.
Once again:
SWITCHING BRANCHES CHANGES THE FILES |
Ok, now to do the merge.
Click the small arrow next to the master branch in the file tree.
I want to merge in the changes on the d-01-intro branch—to do this, click the merge icon next to d-01-intro in the dropdown menu (Figure 6.65):
This will open the merge branch dialogue box:
The first line master branch when we made the merge.
, can’t be changed; it is set to this because we were on theWhen I make the merge (by clicking master branch. This is a called a merge commit.
), Brackets will create a new commit point on theMerge commits, like every other commit, need a commit message and that is what the next line is. The
box is asking for a one line commit message, enter:P02: Published (merged d-01-intro). |
---|
The pull request message. Pull request is a GitHub thing, I haven’t covered this yet (see § 10.1.2). For the minute don’t push the button—you will just have to delete the text it puts in the box if you do.
button next to the message puts in an automaticThe next two tick boxes:
appendix e.1. Leave this box unticked.
—rebase allows us to effectively re-write history. I don’t like rebase and I recommend you don’t use it. I explain it as a topic and why I don’t like it inTick the final box
. A fast-forward merge is basically a merge with no conflicts. I still want a commit point after the merge, even if there were no problems. Tick this box. That’s it, click . This will generate a merge result dialogue box:Ok, we changed two files (01-intro.html and style.css) and I’m willing to bet there were 19 additions and one deletion. Click .
The master branch is still active; it now has a new commit, the merge commit, [abf1121] in my case. Give this the tag P02 and open the commit history:
The master branch now has five commits on it. The last one being the merge commit. The workflow now looks like this:
At this point, everything on the d-01-intro branch has been merged on to the master branch—d-01-intro still exists and it still has the two commit points on it (you can see this if you change to the branch and view the commit history). However, the branch doesn’t contain anything we need—the commits have been merged on to the master branch.
The best thing to do now is delete the d-01-intro branch. It’s served its purpose and we don’t need it anymore (we can always recreate it if we need to work on 01-intro again).
This is easy; make sure that the branch you want to delete is not active. In my case I’m on the master branch and I want to delete d-01-intro so this is ok.
Again click the small arrow next to the master branch in the file tree.
This time I want to delete the d-01-intro branch—to do this, click the delete icon next to d-01-intro (on the left) in the dropdown menu (Figure 6.70):
This will open an
dialogue box, click and the branch is gone.This gives the workflow of Figure 6.71.
Last bit.
I’m going to create a conflict on the d-02-about branch and then try to merge it back into the master branch (just like the theoretical arrangement in § 2.4.1).
Switch back to the d-02-about branch. I’m going to modify exactly the same line in the style.css as I did with the last change to d-01-intro. But this time, we’ll make the h3 element red.
h1, h2, h3, h4, h5, h6 { /* set standard headings */ font-family: sans-serif; font-weight:normal; font-size: 3rem; padding: 2rem 5rem 2rem 5rem; } h3 { font-size: 2.5rem; color: #c0504d;}
Code 6.9 style.css modified in d-02-about |
Again this is a modification to line 30 (below):
h3 { font-size: 2.5rem; color: #c0504d; }
This will conflict with line 30 on the master branch, this has:
h3 { font-size: 2.5rem; color: #4F81BD; }
The d-02-about modification makes the 02-about page look like this:
Save the file, tick the commit the change to style.css with the commit message:
andS: Staged - 02-about page |
---|
Style.css modified <h3> colour changed to red. |
My commit point is [28a335c]. Tag the commit with the label s-P01.02.P03. This will be the last commit on this branch.
We now have:
Now switch to the master branch and merge in the changes on d-02-about (in just the same way as § 6.7.1). Click the merge icon next to d-02-about in the dropdown menu (Figure 6.74):
Again we have the merge branch dialogue box:
This time enter the merge message:
P03: Published (merged d-02-about). |
---|
And click
.This bit is confusing—bear with me.
This opens an empty Merge result box:
It’s empty because the merge hasn’t happened (yet). Just close the box, (you can’t enter anything on the line—as much as it looks as if you should be able to). Click . What we want is behind it
That’s the confusing bit over—just the strange merge result box.
Things look a bit different now. This is the result:
First thing is we now have an Figure 6.73).
button in the Git pane. Click that and we go back to where we were before we tried to merge the files (i.e. where we were inBrackets is trying to tell us there is a conflict in the style.css file. It’s opened the file in the editor and is showing the lines with a problem. It looks like this:
<<<<<<< HEAD h3 { font-size: 2.5rem; color: #4F81BD; } ======= h3 { font-size: 2.5rem; color: #c0504d;} >>>>>>> d-02-about
Code 6.10 style.css with a conflict |
It starts with a line of less than signs:
<<<<<<< HEAD
the HEAD tells us that what follows is from the current head (which is on the master branch).
The line that follows is the code as it is on the master branch:
h3 { font-size: 2.5rem; color: #4F81BD; }
Then we have a row of equal signs, this is just a divider:
=======
The last two lines show the code as it is on the merging branch: d-02-about:
h3 { font-size: 2.5rem; color: #c0504d;} >>>>>>> d-02-about
The greater than signs identify the merging branch (d-02-about).
Brackets is telling us it can’t decide what to do and it wants us to manually change the file to the correct version.
I could select either version and just delete the other one, or I could enter something completely different. In this case, I’m going to put the file back to how it was at the start (without specifying any colour).
Make the modifications:
h1, h2, h3, h4, h5, h6 { /* set standard headings */ font-family: sans-serif; font-weight:normal; font-size: 3rem; padding: 2rem 5rem 2rem 5rem; } h3 { font-size: 2.5rem; } .cover-fig { /* holder for cover image */
Code 6.10 style.css with a conflict |
Mine looks like this after the modifications, Figure 6.78:
Save style.css and make sure it is staged (tick the box in the Git pane).
Now click
.This opens a normal commit dialogue box. Its message will have the first line we entered initially.
P03: Published (merged d-02-about). |
---|
It will also have the information that a conflict occurred in style.css
Mine looks like this:
Just click master branch. In my case the new commit is [07fe437]. Tag the new commit P03 and view the history:
. Brackets will automatically change to theThe workflow is now:
Finally, the last thing to do is delete the redundant branch (d-02-about). Again click the small arrow next to the master branch in the file tree, click the delete icon next to d-02-about in the dropdown menu.
We’re done. The final workflow is all on the master branch, Figure 6.82:
All the commits from the two branches that were created are now merged onto the master branch line.
Ok, this is a bit of a topic in its own right; but really it’s just more of what we’ve been doing in the previous sections.
There are one or two important points about it: how do I know I’ve found all the conflicts? But rather than bore you with the minutiae I’ve separated it into its own appendix where I cover it in excruciating detail.
See it in all its technicolour glory in appendix f.