By the end of this lesson, students will be able to:
- Identify use cases for both checking out a branch and merging that branch into master
- Create and check out a new branch
- Merge a branch into master
- By default, the default branch is the master branch
- Each time we create a new branch, we are are splitting off from the master branch's commit history, and moving the HEAD from the most recent commit on the master branch to the new branch
- HEAD is simply a reference or pointer to the current or most recent commit
What makes a branch special in git, is that we're always on a specific branch, and when we commit, the current branch HEAD moves forward to the new commit. Another way to say that is the HEAD always stays at the tip of the branch.
- Why we want to keep the master branch clean or pure
- Why we want to create branches for adding new features
- How and why we add this code back into the core codebase
Close your laptops and spend 30 seconds memorizing these steps. I will randomly call on students to recall the steps after they are removed from the projector.
- Create and switch to a new branch
- Create and/or change file(s)
- With
git
, add and commit these files to the repo - Switch back to
master
branch - Merge in changes
git checkout -b new_branch
new_branch
could be anything:pizza_branch
,nav_bar_branch
,branch_dressing
, or whatever you'd like to name it- This creates a new branch and checks it out
- Add or change files
- any change to the working tree
git add
the relevant files, thengit commit -m "
[insert commit message here]"
- This step involves 2 parts!!!
git add
&git commit
. Never forgit.
git checkout master
git merge new_branch
Let's say that I just merged my HEAD with a steel beam. Ouch. Now I forgot what I just taught you. Plz help.
- I want to check out a new branch and switch to it.
- How do I do this?
- In my repo, I want to record the changes I've just made.
- How do I do this?
- I want to merge the changes I've just made with the master branch.
- But how!?
- Clone down this repo
- What branches are in this repo? Maybe there are branches we don't even know about yet. How could we tell? Try the commands below in your CLI:
- try
git branch
- then try
git branch -v
- also type
ls
to see what files are in your directory
- See any other branches? if you see a branch try checking it out with
git checkout
<insert_branch_name_here>
- note the difference between
git checkout
andgit checkout -b
ls
again: what changed?
- Make whatever changes you'd to any new files on the branch you've just checked out.
- Very Important: add and commit these changes!
- Switch back to
master
branch. - Merge the branches.
After finishing the 1st bonus, add a gh-pages branch to your Wendy Bite homework, or another repo. You can navigate to: your_username.github.io/name_of_the_repo_that_you_added_a_gh_pages_branch_to
An Excellent Video Detailing the Steps of DeployingGH-Pages
We've learned best practices for adding new code! Preserving the master branch and using branches to add in new features allows us to safely add in new code. This is also great when we fork a repository and want to make changes. With homework, after forking, cloning, check out and create a new branch in the following way git checkout -b firstname_lastname_solution