Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Merging Branches4:34 with Jay McGavren
You have two branches. Both have changes you need, but when you check one out the changes in the other are no longer available. How do you get access to both at once? You merge the branches!
Merging takes the contents of your files from one branch, and merges them with the contents of your files in another branch.
You can merge any branch into any other branch, although it's most commonly used to bring the contents of a topic branch into the
We've discovered a problem with our code. If we run our main program, we get an error:
It points to an error in our
decoder.rbfile. Let's go fix that.
# If letter was found... if letter != nil # Add it onto the string. text += letter end
- Let's check out a new topic branch to commit this to:
git checkout -b fix-nils
- And then commit the file:
git add decoder.rb
git commit -m 'Fix error with missing letters'
- Now, check out the branch you're going to merge into:
git checkout master
- Merge the other branch into the checked out branch:
git merge fix-nils
- Now our bug fix is available on the
- One last thing. Now that we've merged the changes, we don't need the
fix-nilsbranch any more.
- In fact, having it around could confuse you later, if you don't remember what it's for.
- So we need to delete it:
git branch -d fix-nils
- If the branch weren't merged, we'd get a warning before deleting it, but because we've merged the branch into another branch, Git just goes ahead and deletes it for us.
You have two branches, both have changes you need but when you check one out,
the changes on the other are no longer available.
How do you get access to both at once?
You merge the branches.
Merging takes the contents of your files from one branch and
merges them with the contents of your files on another branch.
You can merge any branch into any other branch, although it's most commonly used
to bring the contents of a topic branch into the master branch.
We've discovered a problem with our code.
If we run our main program we get an error.
It points to an error in our decoder.rb file.
Let's go fix that.
The problem is that our program is trying add a letter onto the end of the string
whether that letter was actually found in the hash full of keys or not.
So we need to make it so that the letter is only added
on in the event that the key is actually found.
Again, don't worry if you don't understand all the details of this.
It's not essential to knowing what's going on with the git repo.
Lets go back to our terminal real quick and try this to make sure it works.
We're still not getting any output, but at least we're not getting any more errors.
Now, as always, code like this should go on a topic branch not your master branch.
We wanna be sure our topic branch is based off the master branch, so
let's check that out, git checkout master.
So I'm gonna check out a new topic branch to hold this code,
we'll call it fix-nils, git checkout -b fix-nils.
And then we'll commit this code to that new branch,
we'll stage the decoder.rb file with git add decoder.rb.
And we'll make a commit, git commit -m fix error with missing letters.
If we run git log we'll be able to see the new commit.
These code changes will only exist on the fix-nils branch.
So for example, if we were to checkout the master branch with git checkout master,
and then if we refresh our editor we can see that those changes
don't exist here on the master branch.
The commit doesn't exist on the master branch's history either.
Now that we have the master branch checked out,
let's merge our fix-nils branch into it.
We do that with the git merge command, git merge and
then the name of the branch we want to merge into the current one, fix-nils.
So that'll take the commits from our fix-nils branch and
merge them into the master branch.
Now if we run git log, We'll see our fix error
with missing letters commit here on the master branch.
And if we go refresh our editor again we'll see that our code changes
are visible on the master branch as well.
Notice in the output for the merge command it says Fast-forward.
This is because we based the fix-nils branch on the master branch, and
we didn't make any additional commits to the master branch before merging fix-nils
So git merge was able to bring the changes from the fix-nils
branch to the master branch by simply moving
the master branch pointer forward to the latest commit on fix-nils.
Normally, merges are a little more complex than this, and
it's not possible to git to fast forward.
We'll see an example of this in an upcoming video.
You don't have direct control over whether a merge is done by fast forwarding or
not, so it's not something you'll have to remember.
You should just be aware that fast forward merges can happen under certain
that the resulting history looks a little different than other merges.
So now our bug fix is available on the master branch.
One last thing,
now that we've merged the changes, we don't need the fix-nils branch anymore.
In fact, having it around could confuse you later if you don't remember what
So we need to delete it.
We do that by running the git branch command and passing it the -d option,
followed by the name of the branch we want to delete, fix-nils.
If the branch weren't merged we'd get a warning before deleting it.
But because we've merged the branch into another branch, git just goes ahead and
deletes it for us.
If we run git branch again we'll see that the fix-nils branch is gone.
So now we've created the topic branch and merged it into our master branch.
You can use the same process to merge any two branches you want.
In the next video, we'll merge changes for master into a topic branch.
You need to sign up for Treehouse in order to download course files.Sign up