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 the previous video, I mentioned that you're generally going to want to check out the `master` branch before creating any topic branches. It's a good idea to ensure your topic branch is based on the `master` branch, if you're planning to merge it back into `master`. Let's take a look at what happens if we create a new branch when a different branch is checked out.
- I'm going to redo my creation of the
add-letters
branch.- But this time I'm going to do it with the
testing
branch checked out instead ofmaster
:git checkout testing
- The
testing
branch includes the two commits that set up our unit tests:git log
- Now let me create and check out a new version of the
add-letters
branch, with a slightly different name:git checkout -b add-letters-2
- But this time I'm going to do it with the
- I'll make the same update to the
decoder.rb
file that I did in the previous video:
1 => 'A',
2 => 'B',
3 => 'C',
4 => 'D',
5 => 'E',
- Now I'll stage that file:
git add decoder.rb
- And I'll commit it:
git commit -m "Add A-E"
- If I run
git log
now, we'll see the commits for thisadd-letters-2
branch:git log
- We'll see the new commit we just added, which is identical to the commit we made to the
add-letters
branch in the previous video. - And below that, we'll see that the log includes the commits that add unit tests!
commit 389e26b7170e0697061c32ea42388d4046090ad2 (HEAD -> add-letters-2)
Author: Jay McGavren <me@example.com>
Date: Fri Sep 28 12:08:50 2018 -0700
Add A-E
commit 01c21ced52c664acf1362fa003ffc47bcde838ce (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
...
- Because the
testing
branch was checked out when we created theadd-letters-2
branch, all the commits in thetesting
branch are now included in theadd-letters-2
branch. - We don't want that. Our unit tests may not be done yet, and we may want to make changes to them. For now, at least, we want commits related to unit tests to exist on the
testing
topic branch, and only thetesting
branch. - Here's something else interesting: those commits still exist on the
testing
branch as well.- We can confirm this by checking out
testing
:git checkout testing
- If we display the log:
git log
- ...We'll see the commits to set up unit tests there as well.
- We can confirm this by checking out
commit 01c21ced52c664acf1362fa003ffc47bcde838ce (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
...
The two commits from the testing
branch that we see on the add-letters-2
branch aren't duplicates of these commits, they are the exact same commits.
- Remember, we said that a branch is just a pointer to a particular commit.
- The history for a branch is determined by the commits themselves. Every commit has a "parent", that is, another commit that it's based on.
- Any Git command that needs to access your branch history looks at each commit, then moves to its parent.
- This is true even for commits on a new branch. The commit's parent may be a commit that another branch points to.
When we created the first add-letters
branch, the master
branch was checked out. So the commit that the master
branch pointed to was used as the parent of the first commit on the add-letters
branch.
But when we created the add-letters-2
branch, the testing
branch was checked out. The commit that the testing
branch pointed to was used as the parent of the first add-letters-2
commit. This is why the two commits that set up unit tests appear on both the testing
and add-letters-2
branches.
If we tried to merge the add-letters-2
branch into the master
branch later, it would be an even bigger problem.
- The commits from the
testing
branch would be merged into themaster
branch, because they're also part of theadd-letters-2
branch. They'd be included even if they weren't ready.
Cherry-picking commits
There's a way to select only particular commits from a branch: the git cherry-pick
command. You can read more about it here.
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