Skip to content

Commit 592fb71

Browse files
authored
Merge pull request CodeYourFuture#8 from NateWr/git_exercises
Add git exercises for merging, pull requests and merge conflicts
2 parents 82a4475 + 3d61829 commit 592fb71

File tree

11 files changed

+292
-0
lines changed

11 files changed

+292
-0
lines changed

images/15-pull-request-0.png

57.6 KB
Loading

images/15-pull-request-1.png

62.5 KB
Loading

images/15-pull-request-2.png

77.6 KB
Loading

images/15-pull-request-3.png

83.1 KB
Loading

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"12": "live-server --open=week-2/12-align-items",
2020
"13": "live-server --open=week-2/13-order",
2121
"14": "live-server --open=week-2/14-align-self",
22+
"15": "live-server --open=week-2/15-git-merge",
23+
"20": "live-server --open=week-3/20-git-conflict",
2224
"serve": "live-server"
2325
},
2426
"devDependencies": {

week-2/15-git-merge/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>8. Git Merge - 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-2/15-git-merge/readme.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Git Merging with Pull Requests
2+
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.
4+
5+
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.
6+
7+
> _Hint: If you get confused at any time, ask a mentor for help. Merging can be difficult!_
8+
9+
First, let's build our `dark_orange` branch. You did something similar in exercise 8, but let's practice doing it again.
10+
11+
Open the terminal in your code editor. Check which branch you are currently in by entering the following command into your terminal:
12+
13+
```
14+
git branch
15+
```
16+
17+
You should see the following printed to the terminal, which tells you that you are currently on the `master` branch:
18+
19+
```
20+
green_links
21+
* master
22+
red_links
23+
```
24+
25+
Enter the following command into your terminal to create a new branch:
26+
27+
```
28+
git checkout -b orange_links
29+
```
30+
31+
Enter `git branch` to see if the branch was created. You should see the following:
32+
33+
```
34+
green_links
35+
master
36+
* orange_links
37+
red_links
38+
```
39+
40+
This means that you are currently editing the `orange_links` branch.
41+
42+
Now edit the `style.css` file and change the colour of the links in your messages to `orangered`. Check to see if the links have changed colour. If they have, then add and commit your changes.
43+
44+
Next, you will need to push the `orange_links` branches. When you have a _new_ branch, you have to add extra information to the _first_ `push` command:
45+
46+
```
47+
git push -u origin orange_links
48+
```
49+
50+
When you have successfully pushed your `orange_links` branch, you should see the following in your GitHub repository.
51+
52+
![Screenshot of the new branch pull request prompt in GitHub](/images/15-pull-request-0.png)
53+
54+
GitHub will show you this yellow bar whenever you have recently pushed to a branch. If you pushed your changes more than 20-30 minutes ago, it may not appear.
55+
56+
Now we are ready to merge the changes from our `orange_links` branch into our `master` branch. Click the **Compare & pull request** button indicated below.
57+
58+
![Screenshot of the new branch pull request prompt in GitHub with the pull request button indicated](/images/15-pull-request-1.png)
59+
60+
On the next page, check to make sure the correct branch is showing where the arrow points to `orange_links`. Then scroll down to see a description of the changes that will be applied.
61+
62+
When you're ready to create the pull request, click the **Create Pull Request** button.
63+
64+
![Screenshot of the new pull request screen in GitHub](/images/15-pull-request-2.png)
65+
66+
Now that you've created the pull request, make some final checks and then merge it.
67+
68+
1. It shows each commit you made on the branch. Make sure you _only_ see the commits you expect to see.
69+
2. Click the **Files Changed** link to see a list of all the changes you are about to merge. Make sure you _only_ see the changes you expect to see.
70+
3. When you're ready, click the **Merge Pull Request** button.
71+
72+
![Screenshot of the open pull request screen in GitHub](/images/15-pull-request-3.png)
73+
74+
Congratulations! You have now changed the colour of the links and merged those changes to your `master` branch.

week-2/15-git-merge/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: #4491db;
4+
font-weight: 700;
5+
text-decoration: none;
6+
}

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)