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.
[MUSIC] 0:00 You have two branches, both have changes you need but when you check one out, 0:05 the changes on the other are no longer available. 0:09 How do you get access to both at once? 0:12 You merge the branches. 0:14 Merging takes the contents of your files from one branch and 0:16 merges them with the contents of your files on another branch. 0:19 You can merge any branch into any other branch, although it's most commonly used 0:23 to bring the contents of a topic branch into the master branch. 0:27 We've discovered a problem with our code. 0:31 If we run our main program we get an error. 0:34 It points to an error in our decoder.rb file. 0:36 Let's go fix that. 0:40 The problem is that our program is trying add a letter onto the end of the string 0:41 whether that letter was actually found in the hash full of keys or not. 0:45 So we need to make it so that the letter is only added 0:49 on in the event that the key is actually found. 0:54 Again, don't worry if you don't understand all the details of this. 0:58 It's not essential to knowing what's going on with the git repo. 1:02 Lets go back to our terminal real quick and try this to make sure it works. 1:06 We're still not getting any output, but at least we're not getting any more errors. 1:10 Now, as always, code like this should go on a topic branch not your master branch. 1:14 We wanna be sure our topic branch is based off the master branch, so 1:19 let's check that out, git checkout master. 1:23 So I'm gonna check out a new topic branch to hold this code, 1:28 we'll call it fix-nils, git checkout -b fix-nils. 1:33 And then we'll commit this code to that new branch, 1:37 we'll stage the decoder.rb file with git add decoder.rb. 1:41 And we'll make a commit, git commit -m fix error with missing letters. 1:46 If we run git log we'll be able to see the new commit. 1:54 These code changes will only exist on the fix-nils branch. 1:58 So for example, if we were to checkout the master branch with git checkout master, 2:02 and then if we refresh our editor we can see that those changes 2:08 don't exist here on the master branch. 2:11 The commit doesn't exist on the master branch's history either. 2:14 Now that we have the master branch checked out, 2:18 let's merge our fix-nils branch into it. 2:20 We do that with the git merge command, git merge and 2:23 then the name of the branch we want to merge into the current one, fix-nils. 2:26 So that'll take the commits from our fix-nils branch and 2:30 merge them into the master branch. 2:33 Now if we run git log, We'll see our fix error 2:35 with missing letters commit here on the master branch. 2:40 And if we go refresh our editor again we'll see that our code changes 2:44 are visible on the master branch as well. 2:47 Notice in the output for the merge command it says Fast-forward. 2:50 This is because we based the fix-nils branch on the master branch, and 2:55 we didn't make any additional commits to the master branch before merging fix-nils 2:59 into master. 3:03 So git merge was able to bring the changes from the fix-nils 3:04 branch to the master branch by simply moving 3:07 the master branch pointer forward to the latest commit on fix-nils. 3:10 Normally, merges are a little more complex than this, and 3:15 it's not possible to git to fast forward. 3:17 We'll see an example of this in an upcoming video. 3:20 You don't have direct control over whether a merge is done by fast forwarding or 3:23 not, so it's not something you'll have to remember. 3:27 You should just be aware that fast forward merges can happen under certain 3:30 circumstances, and 3:33 that the resulting history looks a little different than other merges. 3:35 So now our bug fix is available on the master branch. 3:38 No errors. 3:43 One last thing, 3:45 now that we've merged the changes, we don't need the fix-nils branch anymore. 3:46 In fact, having it around could confuse you later if you don't remember what 3:53 it's for. 3:56 So we need to delete it. 3:58 We do that by running the git branch command and passing it the -d option, 3:59 followed by the name of the branch we want to delete, fix-nils. 4:04 If the branch weren't merged we'd get a warning before deleting it. 4:08 But because we've merged the branch into another branch, git just goes ahead and 4:11 deletes it for us. 4:15 If we run git branch again we'll see that the fix-nils branch is gone. 4:17 So now we've created the topic branch and merged it into our master branch. 4:22 You can use the same process to merge any two branches you want. 4:26 In the next video, we'll merge changes for master into a topic branch. 4:30
You need to sign up for Treehouse in order to download course files.Sign up