Skip to content

Commit 3d61829

Browse files
committed
Add exercise on git merge conflicts
1 parent 75490d8 commit 3d61829

File tree

5 files changed

+164
-1
lines changed

5 files changed

+164
-1
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"13": "live-server --open=week-2/13-order",
2121
"14": "live-server --open=week-2/14-align-self",
2222
"15": "live-server --open=week-2/15-git-merge",
23+
"20": "live-server --open=week-3/20-git-conflict",
2324
"serve": "live-server"
2425
},
2526
"devDependencies": {

week-2/15-git-merge/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Git Merging with Pull Requests
22

3-
In [exercise 8](/week-1/8-git-branch/readme.md) you created two branches. The links were red in one branch was green in the other, so that you could quickly show your boss both options. She has decided she doesn't like either colour. Instead, she wants the links in a dark orange.
3+
In [exercise 8](/week-1/8-git-branch) you created two branches. The links were red in one branch and green in the other, so that you could quickly show your boss both options. She has decided she doesn't like either colour. Instead, she wants the links in a dark orange.
44

55
In this exercise you will learn how to apply changes you have made in one git branch to another branch. This is called "merging". Follow the instructions below to create a branch, make changes, and merge them to the `master` branch.
66

week-3/20-git-conflict/index.html

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>20. Git Merge Conflict - HTML, CSS and Git Exercises</title>
7+
<link rel="stylesheet" href="/css/normalize.css">
8+
<link rel="stylesheet" href="/css/week1.css">
9+
<link rel="stylesheet" href="/css/message.css">
10+
<link rel="stylesheet" href="styles.css">
11+
</head>
12+
13+
<body>
14+
<div class="site-wrapper">
15+
<div class="site-header">
16+
<div class="site-header__title">Messages</div>
17+
</div>
18+
<div class="messages">
19+
<div class="message">
20+
<div class="message__author">Won</div>
21+
<p class="message__content">
22+
Where should we meet tomorrrow?
23+
</p>
24+
<time class="message__time message__time--old">Yesterday, 7:25pm</time>
25+
</div>
26+
<div class="message">
27+
<div class="message__author">Luke</div>
28+
<p class="message__content">
29+
Let's meet at the iCafe in Merchant City.
30+
<a href="https://goo.gl/maps/aza4h9nUBhn" class="link link--map">
31+
https://goo.gl/maps/aza4h9nUBhn
32+
</a>
33+
</p>
34+
<time class="message__time">7:35pm</time>
35+
</div>
36+
<div class="message message--unread">
37+
<div class="message__author">Won</div>
38+
<p class="message__content">
39+
Ok!
40+
<a href="https://media.giphy.com/media/l41K4KlVE8dgozf8I/giphy.gif" class="link link--gif">https://media.giphy.com/media/l41K4KlVE8dgozf8I/giphy.gif</a>
41+
</p>
42+
<time class="message__time">7:38pm</time>
43+
</div>
44+
</div>
45+
</div>
46+
</body>
47+
48+
</html>

week-3/20-git-conflict/readme.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Git Merge Conflict
2+
3+
In this exercise you will learn how to work with two branches that have "diverged". Branches diverge when one branch has a commit that doesn't exist in the other branch. In some cases, git can not merge them and you have a "merge conflict".
4+
5+
Follow the instructions below to create a merge conflict and then learn how to solve the problem.
6+
7+
> _Hint: There are many ways this exercise can cause problems if you don't follow the steps precisely. If you are confused at any time, ask a mentor for help._
8+
9+
In [exercise 15](/week-2/15-git-merge) you created and merged a pull request to change the links to `orangered`. But your boss is still not happy. She wants to try a purple colour (`fuchsia`).
10+
11+
First, make sure you are on the `master` branch:
12+
13+
```
14+
git checkout master
15+
git branch
16+
```
17+
18+
You should see the following printed to the terminal, which tells you that you are currently on the `master` branch:
19+
20+
```
21+
green_links
22+
* master
23+
orange_links
24+
red_links
25+
```
26+
27+
Create a new branch for changing the link colour to `fuchsia`:
28+
29+
```
30+
git checkout -b purple_links
31+
```
32+
33+
Now edit the `style.css` file and change the colour of the links in your messages to `fuchsia`. Check to see if the links have changed to a purple colour. If they have, then add and commit your changes.
34+
35+
Before you can show your boss the purple colour, she calls you into her office. Customers of your messaging service are complaining about the orange links. Some say it is too bright and hard to read. She wants you to put aside your work on the purple links and set the links back to the original colour (`#4491db`) in the `master` branch immediately.
36+
37+
Make sure you are on the `master` branch:
38+
39+
```
40+
git checkout master
41+
```
42+
43+
> _Hint: Run `git branch` in the terminal to make sure you're on the correct branch._
44+
45+
Edit the `style.css` file and change the colour of the links in your messages to `#ff00ea`. Check to see if the links have changed to a purple colour. If they have, then add and commit your changes.
46+
47+
Phew! You have restored the blue links and customers have stopped complaining. The next day your boss decides she wants to launch the purple links. You can do this by merging your `purple_links` branch into master.
48+
49+
In [exercise 15](/week-2/15-git-merge) you merged a branch into `master` using GitHub. This time we'll do the same thing on the command line.
50+
51+
Check out the `master` branch:
52+
53+
```
54+
git checkout master
55+
```
56+
57+
> _Hint: Don't forget to run `git branch` in the terminal to make sure you're on the correct branch._
58+
59+
Now you can merge the `purple_links` branch into `master` with the following command:
60+
61+
```
62+
git merge purple_links
63+
```
64+
65+
When you try to do this, you will get a message telling you that there is a merge conflict.
66+
67+
```
68+
Auto-merging week-3/20-git-conflict/styles.css
69+
CONFLICT (content): Merge conflict in week-3/20-git-conflict/styles.css
70+
Automatic merge failed; fix conflicts and then commit the result.
71+
```
72+
73+
This tells you that there is a merge conflict in the file `week-3/20-git-conflict/styles.css`. If you open the file you will find that lines have been added to the file.
74+
75+
```
76+
/* Add your own CSS code below */
77+
.link {
78+
<<<<<<< HEAD
79+
color: #4491db;
80+
=======
81+
color: fuchsia;
82+
>>>>>>> purple_links
83+
font-weight: 700;
84+
text-decoration: none;
85+
}
86+
```
87+
88+
Why did this happen? When git tried to merge the `purple_links` branch into `master` it found two commits: the commit where you changed the links to blue and the commit where you changed the links to purple.
89+
90+
Since these commits change the same line of code, git doesn't know which one is correct. You have to tell it the correct code. To do this, edit the file to remove the added lines and the blue colour property, so that only the purple colour remains:
91+
92+
```
93+
/* Add your own CSS code below */
94+
.link {
95+
color: fuchsia;
96+
font-weight: 700;
97+
text-decoration: none;
98+
}
99+
```
100+
101+
Test the changes in your browser. If the links have been changed to a purple colour, you've resolved the merge conflict. Now you need to add and commit the files to complete the merge. Run the following commands:
102+
103+
```
104+
git add week-3/20-git-conflict/styles.css
105+
git commit -m "Merge purple_links branch"
106+
```
107+
108+
Way to go! You've now resolved a merge conflict. Merge conflicts are one of the most challenging things you will deal with. If you made it through, give yourself a pat on the back.

week-3/20-git-conflict/styles.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* Add your own CSS code below */
2+
.link {
3+
color: orangered;
4+
font-weight: 700;
5+
text-decoration: none;
6+
}

0 commit comments

Comments
 (0)