1 00:00:00,000 --> 00:00:05,234 [MUSIC] 2 00:00:05,234 --> 00:00:09,507 You have two branches, both have changes you need but when you check one out, 3 00:00:09,507 --> 00:00:12,291 the changes on the other are no longer available. 4 00:00:12,291 --> 00:00:14,560 How do you get access to both at once? 5 00:00:14,560 --> 00:00:16,510 You merge the branches. 6 00:00:16,510 --> 00:00:19,510 Merging takes the contents of your files from one branch and 7 00:00:19,510 --> 00:00:23,390 merges them with the contents of your files on another branch. 8 00:00:23,390 --> 00:00:27,530 You can merge any branch into any other branch, although it's most commonly used 9 00:00:27,530 --> 00:00:30,750 to bring the contents of a topic branch into the master branch. 10 00:00:31,960 --> 00:00:34,220 We've discovered a problem with our code. 11 00:00:34,220 --> 00:00:36,950 If we run our main program we get an error. 12 00:00:36,950 --> 00:00:40,003 It points to an error in our decoder.rb file. 13 00:00:40,003 --> 00:00:40,860 Let's go fix that. 14 00:00:41,950 --> 00:00:45,980 The problem is that our program is trying add a letter onto the end of the string 15 00:00:45,980 --> 00:00:49,643 whether that letter was actually found in the hash full of keys or not. 16 00:00:49,643 --> 00:00:54,481 So we need to make it so that the letter is only added 17 00:00:54,481 --> 00:00:58,990 on in the event that the key is actually found. 18 00:00:58,990 --> 00:01:02,970 Again, don't worry if you don't understand all the details of this. 19 00:01:02,970 --> 00:01:05,830 It's not essential to knowing what's going on with the git repo. 20 00:01:06,980 --> 00:01:10,570 Lets go back to our terminal real quick and try this to make sure it works. 21 00:01:10,570 --> 00:01:14,870 We're still not getting any output, but at least we're not getting any more errors. 22 00:01:14,870 --> 00:01:19,410 Now, as always, code like this should go on a topic branch not your master branch. 23 00:01:19,410 --> 00:01:23,131 We wanna be sure our topic branch is based off the master branch, so 24 00:01:23,131 --> 00:01:25,729 let's check that out, git checkout master. 25 00:01:28,336 --> 00:01:33,136 So I'm gonna check out a new topic branch to hold this code, 26 00:01:33,136 --> 00:01:37,748 we'll call it fix-nils, git checkout -b fix-nils. 27 00:01:37,748 --> 00:01:41,169 And then we'll commit this code to that new branch, 28 00:01:41,169 --> 00:01:45,229 we'll stage the decoder.rb file with git add decoder.rb. 29 00:01:46,530 --> 00:01:51,936 And we'll make a commit, git commit -m fix error with missing letters. 30 00:01:54,434 --> 00:01:57,110 If we run git log we'll be able to see the new commit. 31 00:01:58,960 --> 00:02:02,220 These code changes will only exist on the fix-nils branch. 32 00:02:02,220 --> 00:02:06,560 So for example, if we were to checkout the master branch with git checkout master, 33 00:02:08,200 --> 00:02:11,050 and then if we refresh our editor we can see that those changes 34 00:02:11,050 --> 00:02:12,890 don't exist here on the master branch. 35 00:02:14,620 --> 00:02:17,450 The commit doesn't exist on the master branch's history either. 36 00:02:18,680 --> 00:02:20,720 Now that we have the master branch checked out, 37 00:02:20,720 --> 00:02:23,310 let's merge our fix-nils branch into it. 38 00:02:23,310 --> 00:02:26,150 We do that with the git merge command, git merge and 39 00:02:26,150 --> 00:02:29,669 then the name of the branch we want to merge into the current one, fix-nils. 40 00:02:30,730 --> 00:02:33,270 So that'll take the commits from our fix-nils branch and 41 00:02:33,270 --> 00:02:35,580 merge them into the master branch. 42 00:02:35,580 --> 00:02:40,576 Now if we run git log, We'll see our fix error 43 00:02:40,576 --> 00:02:43,060 with missing letters commit here on the master branch. 44 00:02:44,150 --> 00:02:47,320 And if we go refresh our editor again we'll see that our code changes 45 00:02:47,320 --> 00:02:49,100 are visible on the master branch as well. 46 00:02:50,800 --> 00:02:55,550 Notice in the output for the merge command it says Fast-forward. 47 00:02:55,550 --> 00:02:59,050 This is because we based the fix-nils branch on the master branch, and 48 00:02:59,050 --> 00:03:03,577 we didn't make any additional commits to the master branch before merging fix-nils 49 00:03:03,577 --> 00:03:04,850 into master. 50 00:03:04,850 --> 00:03:07,940 So git merge was able to bring the changes from the fix-nils 51 00:03:07,940 --> 00:03:10,430 branch to the master branch by simply moving 52 00:03:10,430 --> 00:03:13,944 the master branch pointer forward to the latest commit on fix-nils. 53 00:03:15,190 --> 00:03:17,890 Normally, merges are a little more complex than this, and 54 00:03:17,890 --> 00:03:20,090 it's not possible to git to fast forward. 55 00:03:20,090 --> 00:03:22,380 We'll see an example of this in an upcoming video. 56 00:03:23,750 --> 00:03:27,580 You don't have direct control over whether a merge is done by fast forwarding or 57 00:03:27,580 --> 00:03:30,230 not, so it's not something you'll have to remember. 58 00:03:30,230 --> 00:03:33,790 You should just be aware that fast forward merges can happen under certain 59 00:03:33,790 --> 00:03:35,250 circumstances, and 60 00:03:35,250 --> 00:03:38,980 that the resulting history looks a little different than other merges. 61 00:03:38,980 --> 00:03:42,070 So now our bug fix is available on the master branch. 62 00:03:43,710 --> 00:03:45,330 No errors. 63 00:03:45,330 --> 00:03:46,074 One last thing, 64 00:03:46,074 --> 00:03:49,804 now that we've merged the changes, we don't need the fix-nils branch anymore. 65 00:03:53,375 --> 00:03:56,969 In fact, having it around could confuse you later if you don't remember what 66 00:03:56,969 --> 00:03:58,010 it's for. 67 00:03:58,010 --> 00:03:59,930 So we need to delete it. 68 00:03:59,930 --> 00:04:04,620 We do that by running the git branch command and passing it the -d option, 69 00:04:04,620 --> 00:04:08,560 followed by the name of the branch we want to delete, fix-nils. 70 00:04:08,560 --> 00:04:11,728 If the branch weren't merged we'd get a warning before deleting it. 71 00:04:11,728 --> 00:04:15,620 But because we've merged the branch into another branch, git just goes ahead and 72 00:04:15,620 --> 00:04:16,620 deletes it for us. 73 00:04:17,730 --> 00:04:21,007 If we run git branch again we'll see that the fix-nils branch is gone. 74 00:04:22,658 --> 00:04:26,528 So now we've created the topic branch and merged it into our master branch. 75 00:04:26,528 --> 00:04:30,098 You can use the same process to merge any two branches you want. 76 00:04:30,098 --> 00:04:33,968 In the next video, we'll merge changes for master into a topic branch.