Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
In this video, we'll create our first topic branch.
Source code
Here's the code for the files we'll be using. Don't worry if you don't have Ruby installed; there's no need to run them. Our real goal is to learn how to track changes to these files.
decoder.rb
KEY = {
}
# Define a method that takes an array of numbers to decode.
def decode(numbers)
text = ""
# Process each number in the array.
numbers.each do |number|
# Get the letter that corresponds to this number.
letter = KEY[number]
# Add it onto the string.
text += letter
end
# Return the decoded string.
return text
end
try_decoder.rb
require './decoder'
puts decode([1, 3, 5])
puts decode([1, 18, 3, 1, 4, 5])
Git branches
- You've been working with one branch ever since you started using Git, perhaps without realizing it.
- When you run
git init
, Git creates a new branch for you, calledmaster
. We can try this now:git init
- If you run
git status
, it will show at the top that you're "On branch master".
- When you run
- Now, branches act like alternate timelines for your file contents, but in reality, a branch is just a pointer to a particular commit. Don't worry if you don't understand that right now, we'll be circling back to that idea later.
- So I can stage a file right now:
git add decoder.rb
- And I can make a commit:
git commit -m 'Add decode function'
- So I can stage a file right now:
- If I run
git log
, you can see that commit:
commit e40647094c74d64fb7c4dee7351139c3ba644d9c (HEAD -> master)
Author: Jay McGavren <me@example.com>
Date: Tue Sep 25 08:22:59 2018 -0700
Add decode function
- See that "HEAD -> master" label on the right? That shows that the
master
branch is pointing to this commit.-
HEAD
is itself just a pointer to the branch that's currently checked out. We'll talk more about that later in the course.
-
- Right now our Git repository looks like this. There's only one commit.
- There's also only one branch, named
master
, and it's pointing to that commit. - If I add another commit, the
master
branch will move to point to that commit.- I'll add another file:
git add try_decoder.rb
- And I'll make another commit:
git commit -m 'Add main program'
- If I show the log again, we'll see that the "master" label has moved:
git log
- I'll add another file:
commit 21428a2eee8e55becb6da879228446746ad6f1a8 (HEAD -> master)
Author: Jay McGavren <me@example.com>
Date: Tue Sep 25 08:34:12 2018 -0700
Add main program
commit e40647094c74d64fb7c4dee7351139c3ba644d9c
Author: Jay McGavren <me@example.com>
Date: Tue Sep 25 08:22:59 2018 -0700
Add decode function
- So now we've added a second commit, whose parent is the first commit.
- And the
master
branch has been moved to point to this second commit.
Making commits to the master
branch is widely considered to be a bad idea, though. Most developers treat the master
branch as code that has been thoroughly tested and is always ready to deploy to customers. If you've just finished some code that isn't thoroughly tested and ready to share, it shouldn't be committed to the master
branch.
So where do we commit new code? To a new branch! Let's create one now.
- Suppose we wanted to add some unit tests.
- I'll create a new file, named
test_decoder.rb
. - And I'll save this code to it.
test_decoder.rb
require 'minitest/autorun'
require './decoder'
class TestDecode < Minitest::Test
def test_dad
assert_equal("DAD", decode([4, 1, 4]))
end
end
- I haven't committed this new file yet. When I do, I want it to be on a new branch.
- Commits we make on this new branch will be kept separately from commits we make on
master
or other branches.- We create a branch with the
git branch
subcommand. We typegit branch
followed by the name we want our new branch to have. - Branch names should be in all lower case. If there are multiple words, they should be separated by dashes:
my-branch-name
- But for this branch, we'll just go with a simpler name:
git branch testing
- We create a branch with the
- We can view the list of branches in the repo by typing
git branch
without a new branch name.- The active branch will have a star next to it in the output.
- If we make commits right now, they'll still go on the active branch,
master
. We need to switch to ourtesting
branch so that commits go there instead.- We do that with the
git checkout
subcommand, followed by the name of the branch we want:git checkout testing
- Let me run
git log
again:git log
- You'll see the commits are the same, but there's now a new
testing
branch listed by the most recent commit. - Both the
testing
andmaster
branches are pointing to this commit. - Also notice that the
HEAD
arrow is now pointing totesting
instead ofmaster
. - We'll be talking about the
HEAD
reference more later, but basically this just means thattesting
is now the checked out branch instead ofmaster
. - The commits from the
master
branch are listed here because thetesting
branch is based on themaster
branch.
- We do that with the
commit 21428a2eee8e55becb6da879228446746ad6f1a8 (HEAD -> testing, master)
Author: Jay McGavren <me@example.com>
Date: Tue Sep 25 08:34:12 2018 -0700
Add main program
commit e40647094c74d64fb7c4dee7351139c3ba644d9c
Author: Jay McGavren <me@example.com>
Date: Tue Sep 25 08:22:59 2018 -0700
Add decode function
- The repository looks like this.
- We've added a second branch, but there aren't any commits on it yet.
- The new branch is pointing to the same commit as the
master
branch.
- Now, let's make a commit on this
testing
branch.- I'll stage my file with the unit tests:
git add test_decoder.rb
- And commit it:
git commit -m "Add unit test"
- I'll stage my file with the unit tests:
- If I run
git log
now, you'll see that there's a new commit, and that thetesting
branch pointer has been moved to point there.- The
master
branch, however, is still pointing to the same commit it was before.
- The
commit 3c64f2dea660694d5da1ba7d5d1578f62d7919c8 (HEAD -> testing)
Author: Jay McGavren <me@example.com>
Date: Tue Sep 25 18:25:05 2018 -0700
Add unit test
commit 21428a2eee8e55becb6da879228446746ad6f1a8 (master)
Author: Jay McGavren <me@example.com>
Date: Tue Sep 25 08:34:12 2018 -0700
Add main program
commit e40647094c74d64fb7c4dee7351139c3ba644d9c
Author: Jay McGavren <me@example.com>
Date: Tue Sep 25 08:22:59 2018 -0700
Add decode function
- Here's what the repository looks like overall.
- A commit has been added, and the
testing
branch has been moved to point to that commit. - The
master
branch is still pointing to the same commit.
- When you switch back and forth between branches, the working directory is changed to reflect the current branch.
- Files that don't exist on this branch are removed.
- Contents of each file are updated to reflect their status as of the last commit on this branch.
- The commit history is also updated to reflect the current branch.
Now let's add another commit to the testing
branch.
- I have code for a second test, which I'll add to the
test_decoder.rb
file.
def test_bead
assert_equal("BEAD", decode([2, 5, 1, 4]))
end
- Let me save and close that file.
- Now I'll commit it:
git add test_decoder.rb
git commit -m "Add test with more letters"
- Now that we have a second commit on our
testing
branch, let's try runninggit log
again:git log
- You'll see that the
testing
branch reference has moved to this latest commit. - The
master
branch, meanwhile, is still back at the same commit it was when we originally switched to a different branch.
- You'll see that the
commit 01c21ced52c664acf1362fa003ffc47bcde838ce (HEAD -> testing)
Author: Jay McGavren <me@example.com>
Date: Wed Sep 26 20:00:18 2018 -0700
Add test with more letters
commit 7bec24095bd1604e647912e6a8d4a0ff8061c129
Author: Jay McGavren <me@example.com>
Date: Wed Sep 26 16:24:57 2018 -0700
Add unit test
commit 21428a2eee8e55becb6da879228446746ad6f1a8 (master)
Author: Jay McGavren <me@example.com>
Date: Tue Sep 25 08:34:12 2018 -0700
Add main program
commit e40647094c74d64fb7c4dee7351139c3ba644d9c
Author: Jay McGavren <me@example.com>
Date: Tue Sep 25 08:22:59 2018 -0700
Add decode function
- The repository looks like this.
- The
testing
branch has been updated to point to the latest commit, but themaster
branch is still unmoved.
- The testing branch is local. It exists only on your computer.
- It hasn't been sent over the network to any remote repos, and no network connection was needed to create it.
- It is possible to push the branch to a remote repo. We'll be discussing that in a later stage of this course.
So now you have a new branch to hold your testing code. But what happens if you need to add code that isn't related to testing? We'll try creating another branch in the next video.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up