Difference between revisions of "DPS909 & OSD600 Winter 2017 - Lab 4"

From CDOT Wiki
Jump to: navigation, search
Line 70: Line 70:
 
# Try switching back to your <code>fav-food</code> branch, confirm that you don't have a <code>name.txt</code> file.
 
# Try switching back to your <code>fav-food</code> branch, confirm that you don't have a <code>name.txt</code> file.
  
 +
==4. Merge Conflicts==
  
 +
Git can automatically merge most things without any help.  In the previous case, we added two separate files, and git had no trouble combining them.  What if we had made changes to the same file?
  
 +
Let's try an experiment that will simulate two separate lines of development on the same file:
  
 +
# Create a new branch called <code>animals</code> that points to the same commit as <code>master</code>.
 +
# Create a new file called <code>animals.txt</code> and add the names of three farm animals, one per line.  For example, your <code>animals.txt</code> might look like this:
 +
horse
 +
cow
 +
chicken
  
 +
When you're done, commit the new <code>animals.txt</code> file to your <code>animals</code> branch.  Now let's start two separate changes that both work on <code>animals.txt</code>:
  
 +
# Create a new branch called <code>water-animals</code> that points to the same commit as <code>animals</code>.
 +
# On the <code>water-animals</code> branch, edit <code>animals.txt</code> to add 3 water animals. When you're done, commit your changes to the <code>water-animals</code> branch. For example, your <code>animals.txt</code> might look like this:
 +
horse
 +
cow
 +
chicken
 +
whale
 +
seahorse
 +
dolphin
  
 +
Let's repeat this process, but this time we'll focus on animals that live in jungles:
 +
 +
# Create a new branch called <code>jungle-animals</code> that points to the same commit as <code>animals</code>.
 +
# On the <code>jungle-animals</code> branch, edit <code>animals.txt</code> to add 3 jungle animals.  When you're done, commit your changes to the <code>water-animals</code> branch. For example, your <code>animals.txt</code> might look like this:
 +
horse
 +
cow
 +
chicken
 +
monkey
 +
python
 +
bird of paradise
  
  

Revision as of 13:18, 2 February 2017

Git Branches

In this lab you will review working with branches, and learn how to use the special gh-pages branch on Github to host static web content.

1. Review: creating branches

Every git repo begins with a single branch named master. When you commit, the master branch is automatically updated to point to the most recent commit.

Recall from class that we can create a new branch by doing the following:

git checkout -b new-branch

The -b flag indicates that you want to first create the branch if it does not already exist, then switch to it and check it out to your working directory. You could also have written it like this:

git checkout -b new-branch master

In this case, we are explicitly specifying that we want new-branch to be created and point to the same commit to which master is pointing. Because we're already on the master branch, we don't have to do this--git will assume you want to use the current commit. We could also have written it like this:

git checkout -b new-branch HEAD

Here we explicitly specify that we want to use the latest commit on the current branch, also known as HEAD.

If we wanted to have new-branch get created pointing to something other than the latest commit, we can specify that commit instead. For example, if we want it to point back to two commits from the current one (i.e., go back in time), we'd do this:

git checkout -b new-branch HEAD~2

As you switch between branches, if you ever get confused about which branch you're on, use git status or git branch, both of which will indicate the current branch.

2. Resetting a Branch

Sometimes it's necessary to switch the commit to which a branch is pointing. For example, if you accidentally commit things on master vs. a new branch, and need to move master back to a commit in the same history as a remote repository (i.e., if you want to git pull upstream master to get new updates).

Let's walk through an example:

  1. On Github, fork the Spoon-Knife repository at https://github.com/octocat/Spoon-Knife
  2. Clone this repository to your local computer
  3. Confirm that you are on the master branch (i.e., use git branch)
  4. Create a new file called name.txt with your first name in it
  5. Use git to add and commit the new name.txt file.
  6. Confirm that you have 1 new commit on the master branch (i.e., use git log)

At this point we have a new commit on the master branch, but decide that we should have done this work on a new branch. Let's fix things so master is back where it should be, and we instead have a new branch:

  1. Create a new branch called name by doing git checkout -b name
  2. Confirm that you are now on the name branch
  3. Confirm that name and master both point to the same commit (i.e., use git show name and git show master)

We can now move our master branch back one commit, and leave our name branch where it is. We'll do that using the -B flag (note the capital), which will create or reset a branch:

  1. git checkout -B master HEAD~1
  2. Confirm that name and master both point to different commits (i.e., use git show name and git show master)

3. Merging

Next, let's experiment with merging. We'll start by creating a third branch:

git checkout -b fav-food master

This will create a fav-food branch which is pointing to the same commit as master. Confirm that you now have 3 branches (master, name, fav-food) using git branch.

On the fav-food branch, add a new file food.txt with a list of your favourite foods. When you're done, add and commit this file to the fav-food branch.

Let's now try combining our name and fav-food branches. If you're ever unsure about a merge, you can always try doing it on a new branch and see how it goes. Let's try doing our merge on a branch called name-and-food:

  1. Create a fourth branch that points to the same commit as name: git checkout -b name-and-food name
  2. Merge the fav-food branch into name-and-food. When you merge, you always switch to (i.e., checkout) the branch into which you want to merge first, then merge the other branch in: git merge fav-food. Because we are merging two branches that have different commit histories, git will use a recursive merge strategy, and create a new commit merge commit that connects these two branches.
  3. Confirm that you now have a new merge commit on the name-and-food branch (i.e., use git log).
  4. Confirm that the name-and-food branch contains both the name.txt and food.txt files, created on the earlier branches.
  5. Try switching back to your name branch, confirm that you don't have a food.txt file.
  6. Try switching back to your fav-food branch, confirm that you don't have a name.txt file.

4. Merge Conflicts

Git can automatically merge most things without any help. In the previous case, we added two separate files, and git had no trouble combining them. What if we had made changes to the same file?

Let's try an experiment that will simulate two separate lines of development on the same file:

  1. Create a new branch called animals that points to the same commit as master.
  2. Create a new file called animals.txt and add the names of three farm animals, one per line. For example, your animals.txt might look like this:
horse
cow
chicken

When you're done, commit the new animals.txt file to your animals branch. Now let's start two separate changes that both work on animals.txt:

  1. Create a new branch called water-animals that points to the same commit as animals.
  2. On the water-animals branch, edit animals.txt to add 3 water animals. When you're done, commit your changes to the water-animals branch. For example, your animals.txt might look like this:
horse
cow
chicken
whale
seahorse
dolphin

Let's repeat this process, but this time we'll focus on animals that live in jungles:

  1. Create a new branch called jungle-animals that points to the same commit as animals.
  2. On the jungle-animals branch, edit animals.txt to add 3 jungle animals. When you're done, commit your changes to the water-animals branch. For example, your animals.txt might look like this:
horse
cow
chicken
monkey
python
bird of paradise



After you've picked your bug, add your name and the issue number to the table below

# Name Spoon-Knife Repo (URL) Hosted gh-pages Site (URL)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

Submission

You will have completed your lab when you have done the following:

  • Setup Bramble and Thimble on your local computer, and taken a screenshot of it running in your browser
  • Picked a bug to fix in Thimble
  • Written a blog post discussing what it was like setting up Thimble, what you learned, and which bug you chose. What is the bug about?
  • Added a row to the table above with your name, issue URL, and blog post URL.