1 00:00:00,000 --> 00:00:04,855 [MUSIC] 2 00:00:04,855 --> 00:00:09,422 Collaborating with other developers is one of the biggest reasons to use git. 3 00:00:09,422 --> 00:00:10,312 And to do that, 4 00:00:10,312 --> 00:00:15,660 you need to be able to bring changes from their git repositories into your repo. 5 00:00:15,660 --> 00:00:19,764 You also need to be able to share changes from your repo to their repos. 6 00:00:19,764 --> 00:00:22,824 You do that using remote branches. 7 00:00:22,824 --> 00:00:25,203 A remote branch is like a local branch, but 8 00:00:25,203 --> 00:00:29,420 it points to the commit that a branch in a remote repo is at. 9 00:00:29,420 --> 00:00:33,779 You can update a local branch with commits from a remote repository by 10 00:00:33,779 --> 00:00:36,645 merging a remote branch into a local branch. 11 00:00:36,645 --> 00:00:40,549 Normally, remote branches are used with a repo on a different machine, but 12 00:00:40,549 --> 00:00:44,410 they'll also work if we clone a repo to a second local directory. 13 00:00:44,410 --> 00:00:48,606 This will also let us see the effects on both sides as we push and pull commits. 14 00:00:48,606 --> 00:00:50,836 So let's Let's do a local clone. 15 00:00:50,836 --> 00:00:54,011 We'll have our original repo act as the remote repo, and 16 00:00:54,011 --> 00:00:56,335 our clone act as the local repo. 17 00:00:56,335 --> 00:00:59,338 I'm in the original repo right now, so 18 00:00:59,338 --> 00:01:03,805 I'll change to the directory that contains it with cd .. 19 00:01:03,805 --> 00:01:07,737 Remember that the .., stands for the parent directory. 20 00:01:07,737 --> 00:01:10,601 We clone the repo with the git clone command. 21 00:01:10,601 --> 00:01:15,120 In place of a URL, we'll have it clone from the decoder directory. 22 00:01:15,120 --> 00:01:19,012 We'll set up the clone in the decoder local directory. 23 00:01:19,012 --> 00:01:23,070 Let's change into the directory for our so-called local repo, the clone. 24 00:01:24,900 --> 00:01:27,195 As always, when you clone a repo, 25 00:01:27,195 --> 00:01:31,007 the original repo is set up as a remote repo named origin. 26 00:01:31,007 --> 00:01:34,360 We can see this if we run the git remote command. 27 00:01:34,360 --> 00:01:36,228 If we run the git branch command, 28 00:01:36,228 --> 00:01:39,447 we'll see that git has also set up a master branch for us. 29 00:01:39,447 --> 00:01:41,690 That's the only branch visible for now. 30 00:01:41,690 --> 00:01:45,350 There's no sign of the add letters branch for the remote repo. 31 00:01:45,350 --> 00:01:46,229 If we run git log, 32 00:01:46,229 --> 00:01:50,120 we'll see all the commits from the master branch were cloned along with repo. 33 00:01:51,570 --> 00:01:56,092 If we run git status, we'll see something new. 34 00:01:56,092 --> 00:01:59,956 At the top it says on branch master as usual, but 35 00:01:59,956 --> 00:02:05,690 then below that it says your branch is up to date with origin/master. 36 00:02:05,690 --> 00:02:08,630 Origin master is a remote branch. 37 00:02:08,630 --> 00:02:11,276 You can tell a remote branch name when you see one, 38 00:02:11,276 --> 00:02:15,510 because it always has the remote repo name, a slash, and then the branch name. 39 00:02:17,140 --> 00:02:21,942 Let's try using the remote branch to bring a commit from the remote repo to 40 00:02:21,942 --> 00:02:23,033 the local repo. 41 00:02:23,033 --> 00:02:26,090 I'll be using the fetch and merge subcommands for this. 42 00:02:26,090 --> 00:02:29,009 I'll do this twice, once to give you an overview, and 43 00:02:29,009 --> 00:02:31,748 then again explaining every step more carefully. 44 00:02:31,748 --> 00:02:33,458 It'll involve quite a lot of steps, but 45 00:02:33,458 --> 00:02:35,970 this way you'll know exactly what's happening. 46 00:02:35,970 --> 00:02:40,320 In an upcoming video, I'll show you a shortcut to do the same thing. 47 00:02:40,320 --> 00:02:43,030 So first we need to make a commit in the original repo, 48 00:02:43,030 --> 00:02:45,910 the one we're treating as a remote repo. 49 00:02:45,910 --> 00:02:51,265 I'll change into that directory with cd../decoder. 50 00:02:51,265 --> 00:02:53,775 This changes up to the parent directory, and 51 00:02:53,775 --> 00:02:56,889 then immediately changes into the decoder directory. 52 00:02:56,889 --> 00:03:00,937 Let's make sure the master branch is checked out, git checkout master. 53 00:03:03,339 --> 00:03:08,220 Now let's edit the decoder.rb file, and we'll add a new letter conversion. 54 00:03:09,470 --> 00:03:13,401 We'll say number 26 gets converted to the letter Z. 55 00:03:13,401 --> 00:03:20,318 Let's save this, and now we'll stage the file, git add decoder.rb. 56 00:03:20,318 --> 00:03:24,808 And we'll commit it, git commit-m 'Add Z conversion'. 57 00:03:26,963 --> 00:03:30,840 Now we have a new commit on the master branch and our remote repo. 58 00:03:30,840 --> 00:03:35,298 Let's got to our local repo and use the remote branch to copy that commit over. 59 00:03:35,298 --> 00:03:41,554 I need to change to the clone's directory with cd../decoder-local. 60 00:03:41,554 --> 00:03:44,119 Again, that will change to the parent directory, and 61 00:03:44,119 --> 00:03:46,610 into the decoder local subdirectory. 62 00:03:46,610 --> 00:03:48,817 To connect to the remote repo and 63 00:03:48,817 --> 00:03:54,148 update our remote branches in this local repo, we use the git fetch command. 64 00:03:54,148 --> 00:03:59,196 We need to give git fetch an argument with the name of the remote repo to connect to, 65 00:03:59,196 --> 00:04:01,186 so we'll say git fetch origin. 66 00:04:01,186 --> 00:04:06,116 Now, if I run git status again, You'll see that 67 00:04:06,116 --> 00:04:10,120 the master local branch is out of sync with the origin master remote branch. 68 00:04:11,470 --> 00:04:14,977 This is because the origin master branch has been updated, but 69 00:04:14,977 --> 00:04:16,574 the master branch hasn't. 70 00:04:16,574 --> 00:04:21,249 We can update the local master branch by merging the remote origin master branch 71 00:04:21,249 --> 00:04:21,812 into it. 72 00:04:21,812 --> 00:04:26,230 To do that, we use the get merge command, just as we would with any other branch. 73 00:04:26,230 --> 00:04:28,423 Git merge origin/master. 74 00:04:28,423 --> 00:04:33,953 This will merge the remote origin master branch into the local master branch. 75 00:04:33,953 --> 00:04:38,148 If we run git log now, we'll see the commit from our remote repo, 76 00:04:38,148 --> 00:04:39,381 Add Z conversion. 77 00:04:41,188 --> 00:04:43,855 And if we look at the code for the decoder.rb file, 78 00:04:43,855 --> 00:04:46,150 we'll see the code from that commit. 79 00:04:46,150 --> 00:04:49,294 I'll just use the cat program to show this. 80 00:04:49,294 --> 00:04:54,287 You can see the code to convert the number 26 to the letter Z here. 81 00:04:54,287 --> 00:04:58,370 And this is in our local repository. 82 00:04:58,370 --> 00:05:02,880 Now let me do this again, but explaining everything in a little more detail. 83 00:05:02,880 --> 00:05:05,275 I'll need to make another commit in the remote repo. 84 00:05:05,275 --> 00:05:10,757 I'll change into that directory, with cd../decoder. 85 00:05:10,757 --> 00:05:14,633 Let's add another conversion to decoder.rb. 86 00:05:14,633 --> 00:05:18,470 Here in my editor I'm still editing the remote repo. 87 00:05:18,470 --> 00:05:25,026 So here in the decoder.rb, I'll add another conversion for the letter Y. 88 00:05:25,026 --> 00:05:29,180 Let me save this, and stage it here in the remote repo. 89 00:05:31,831 --> 00:05:33,862 And then commit it. 90 00:05:33,862 --> 00:05:36,929 git commit -m 'Add Y conversion. 91 00:05:39,539 --> 00:05:42,120 Now, I'll follow the same process as before. 92 00:05:42,120 --> 00:05:46,009 I'll go to my local repo and use the remote branch to copy that commit over. 93 00:05:46,009 --> 00:05:52,245 I'll change to the clones directory, cd ../decoder-local/. 94 00:05:52,245 --> 00:05:53,355 Now, at this point, 95 00:05:53,355 --> 00:05:57,512 there's no indication in the local repo that the remote repo has been updated. 96 00:05:57,512 --> 00:06:01,359 We'll see that if we run git status. 97 00:06:01,359 --> 00:06:05,990 It says our branch is up to date with the remote origin/master branch. 98 00:06:05,990 --> 00:06:07,390 And that's true, but 99 00:06:07,390 --> 00:06:11,965 only because our remote branch is out of sync with the actual remote repo. 100 00:06:11,965 --> 00:06:16,561 To connect to the remote repo and update our remote branches in this local repo, 101 00:06:16,561 --> 00:06:19,039 we need to use the git fetch command again. 102 00:06:19,039 --> 00:06:21,658 Git fetch origin. 103 00:06:21,658 --> 00:06:25,785 Git fetch will copy overall the commits the remote repo has that our local repo 104 00:06:25,785 --> 00:06:26,750 doesn't. 105 00:06:26,750 --> 00:06:30,358 Those are the objects you see at copying over in the output. 106 00:06:30,358 --> 00:06:34,370 Git fetch also updates the remote branches in your local repo. 107 00:06:34,370 --> 00:06:38,360 Previously the remote origin master branch was pointing at the same commit 108 00:06:38,360 --> 00:06:39,887 as our local master branch. 109 00:06:39,887 --> 00:06:43,416 That's the first commit SHA that you see in the output here. 110 00:06:43,416 --> 00:06:48,123 That's why the git status command reported that master was up-to-date with 111 00:06:48,123 --> 00:06:49,280 origin/master. 112 00:06:49,280 --> 00:06:53,509 But git fetch updates the origin/master remote branch to point to the same commit 113 00:06:53,509 --> 00:06:55,604 as the master branch in the remote repo. 114 00:06:55,604 --> 00:06:59,370 That's the second commit SHA that you see in the output here. 115 00:06:59,370 --> 00:07:03,860 Now if we rerun git status, it'll show that the local master branch is behind 116 00:07:03,860 --> 00:07:06,635 the remote origin master branch by one commit. 117 00:07:06,635 --> 00:07:13,550 We update master by merging origin master into it, git merge origin/master. 118 00:07:13,550 --> 00:07:18,286 You can see that it updates the local master branch to the same commit that was 119 00:07:18,286 --> 00:07:19,841 retrieved by git fetch. 120 00:07:19,841 --> 00:07:22,882 And again, if we run git log on the local master branch, 121 00:07:22,882 --> 00:07:26,206 it will show the new commit merged in from the remote branch. 122 00:07:28,713 --> 00:07:33,189 And if we check the file contents, cat decoder.rb, 123 00:07:33,189 --> 00:07:37,177 we'll see that they've been updated as well. 124 00:07:37,177 --> 00:07:40,175 So now we know how to fetch commits from a remote branch and 125 00:07:40,175 --> 00:07:42,040 merge them into a local branch. 126 00:07:42,040 --> 00:07:44,568 But the master branch was already set up for us. 127 00:07:44,568 --> 00:07:47,423 How do you set up new local branches to merge into? 128 00:07:47,423 --> 00:07:49,060 We'll look at that in the next video.