0 1 00:00:00,570 --> 00:00:06,120 Now, the first thing we're going to do is open up a Terminal. And inside Terminal, we're going to navigate 1 2 00:00:06,120 --> 00:00:08,000 to our desktop. 2 3 00:00:08,010 --> 00:00:13,650 So if you're not familiar with the command line and you haven't watched the module on the command line, 3 4 00:00:13,980 --> 00:00:18,390 then I recommend you to take a look at it now because we're going to be using a lot of the commands 4 5 00:00:18,390 --> 00:00:20,790 to create directories and navigate around. 5 6 00:00:20,790 --> 00:00:25,290 So if you're not yet familiar, then it's a good time to take a look at that. 6 7 00:00:26,010 --> 00:00:33,600 So the first thing I'm going to do is I'm going to cd into my desktop. And here, I'm going to create a 7 8 00:00:33,600 --> 00:00:43,920 a new directory called Story. Then I'm going to CD into this Story directory. And if I show you with "ls," 8 9 00:00:43,920 --> 00:00:45,870 you can see that it's completely empty. 9 10 00:00:45,990 --> 00:00:50,420 And similarly, inside Finder, I can show you the same thing. 10 11 00:00:50,430 --> 00:00:54,600 So let's go ahead and create some text files. 11 12 00:00:54,660 --> 00:01:01,590 So I'm going to use touch to create a file called chapter1.txt and then I'm going to open chapter1.txt, 12 13 00:01:01,590 --> 00:01:09,690 and you can either do this by simply writing open and it'll open it inside the default application 13 14 00:01:09,750 --> 00:01:11,880 which is TextEdit on a Mac. 14 15 00:01:12,120 --> 00:01:18,130 Alternatively, you can use a text that's within the Terminal, for example, vim. 15 16 00:01:18,210 --> 00:01:21,770 Now, this is only for people who are really familiar with this. 16 17 00:01:21,780 --> 00:01:24,050 So it's totally up to you what you decide to use. 17 18 00:01:24,090 --> 00:01:30,180 But if you are not familiar with using Terminal-based editors, then it's probably a good idea to just 18 19 00:01:30,180 --> 00:01:33,920 stick with the open command and use TextEdit. 19 20 00:01:33,930 --> 00:01:39,110 So let's go ahead and open chapter1 and let's write something inside, shall we? 20 21 00:01:42,990 --> 00:01:43,350 Okay. 21 22 00:01:43,360 --> 00:01:45,270 So there's my masterpiece done. 22 23 00:01:45,390 --> 00:01:49,440 I'm going to go ahead and hit save and I'm going to quit TextEdit. 23 24 00:01:49,570 --> 00:01:53,190 So that was our first chapter done. 24 25 00:01:53,380 --> 00:02:01,100 So, now let's create a Git local repository and start tracking some of these file changes. 25 26 00:02:01,450 --> 00:02:06,350 So to initialize Git, we simply write "git init." 26 27 00:02:06,340 --> 00:02:12,490 And as you can see, it's initialized and empty Git repository inside the story directory. 27 28 00:02:12,490 --> 00:02:17,230 Now, if you have a look inside Finder, you actually can't see this .git at all. 28 29 00:02:17,320 --> 00:02:18,900 But as we learned before, 29 30 00:02:18,970 --> 00:02:20,720 if you use ls -a, 30 31 00:02:20,920 --> 00:02:26,620 you can see all the hidden files and you can see that .git is right there and it's going to be used 31 32 00:02:26,710 --> 00:02:32,320 to track all your changes, to commit your changes, and to perform version control. 32 33 00:02:32,320 --> 00:02:38,100 So we're currently inside the story directory, and you can also call this the working directory. 33 34 00:02:38,110 --> 00:02:45,250 So as I mentioned before, using Git and learning about version control, it comes with some of its own terminology 34 35 00:02:45,340 --> 00:02:46,570 and language. 35 36 00:02:46,570 --> 00:02:51,370 So, I'm going to try and debunk and simplify a lot of these terms that you'll come across, just so that 36 37 00:02:51,370 --> 00:02:55,440 we can all be on the same page, and we all understand what's going on. 37 38 00:02:55,720 --> 00:03:01,770 So currently, we are inside the working directory which is the story directory. 38 39 00:03:02,050 --> 00:03:07,750 And here in order to start tracking the changes of my files, 39 40 00:03:07,750 --> 00:03:15,350 for example, chapter1.txt, then I need to add this file to what's called a staging area. 40 41 00:03:15,790 --> 00:03:23,230 And that is basically a intermediate place where you can pick and choose which files inside your working 41 42 00:03:23,230 --> 00:03:26,270 directory that you want to commit. 42 43 00:03:26,320 --> 00:03:33,220 So to see what's currently inside your staging area, you can use the Git status command, and it shows 43 44 00:03:33,220 --> 00:03:39,630 you that there are untracked files which will be shown in red, and this is something that simply inside 44 45 00:03:39,630 --> 00:03:41,080 a working directory, 45 46 00:03:41,080 --> 00:03:42,410 so inside here, 46 47 00:03:42,550 --> 00:03:45,560 but it's not yet in the staging area. 47 48 00:03:45,610 --> 00:03:52,570 So in order to add it to the staging area and to start tracking changes in it, then we have to use the 48 49 00:03:52,570 --> 00:03:53,940 command "git add." 49 50 00:03:54,020 --> 00:03:58,930 So we're going to type git add and we're going to type the filename, 50 51 00:03:58,930 --> 00:04:01,730 so in this case, it's chapter1.txt. 51 52 00:04:01,960 --> 00:04:08,710 So go ahead and hit enter. And then if we try using Git status again, you can see that that file has been 52 53 00:04:08,800 --> 00:04:11,820 added as a new file and it is now green. 53 54 00:04:11,830 --> 00:04:16,380 So this is now in the staging area and it's ready to be committed. 54 55 00:04:16,390 --> 00:04:20,260 So let's go ahead and commit this under version control. 55 56 00:04:20,260 --> 00:04:28,480 So the command is git commit and I'm going to use the -m flag to add a commit message. 56 57 00:04:28,480 --> 00:04:31,450 So the commit message is really really important. 57 58 00:04:31,630 --> 00:04:38,140 It's something that helps you keep track of what changes you have made in each commit. 58 59 00:04:38,140 --> 00:04:47,200 So when you create a new save point, you want to be as explicit as possible about what changes were made 59 60 00:04:47,230 --> 00:04:50,380 between the last save point and this current save point. 60 61 00:04:50,380 --> 00:04:58,420 So for our initial commit, we can use something very simple like "Initial Commit" and this shows that this 61 62 00:04:58,420 --> 00:04:59,880 is our starting point. 62 63 00:05:00,100 --> 00:05:05,920 Alternatively, if you want to be slightly more specific, because in our case, we've actually completed 63 64 00:05:05,920 --> 00:05:07,090 Chapter 1. 64 65 00:05:07,090 --> 00:05:11,180 So you can write "Complete Chapter 1." 65 66 00:05:11,200 --> 00:05:16,540 Now, the thing that you'll realize is that usually with commit messages, they are written in the present 66 67 00:05:16,540 --> 00:05:17,050 tense, 67 68 00:05:17,260 --> 00:05:18,600 and this is the best practice. 68 69 00:05:18,610 --> 00:05:25,210 So whereas it would probably make more sense, I guess, at least in my head anyways to write "Completed 69 70 00:05:25,270 --> 00:05:28,200 Chapter 1" as this save point. 70 71 00:05:28,240 --> 00:05:32,680 It's actually by convention that you should always use the present tense. 71 72 00:05:32,680 --> 00:05:36,910 So it's like you are doing it now. You're submitting your changes now. 72 73 00:05:37,000 --> 00:05:44,200 So let's go ahead and hit enter to make our first commit. And you can see what commits you have made 73 74 00:05:44,200 --> 00:05:47,290 by using the git log command. 74 75 00:05:47,560 --> 00:05:53,320 So you can see that this commit was made at this time by this person. 75 76 00:05:53,540 --> 00:06:00,720 And it also has a hash and this hash uniquely identifies this particular commit. 76 77 00:06:00,910 --> 00:06:06,620 And then right at the end, you see this commit message of what this save point was all about. 77 78 00:06:07,870 --> 00:06:12,310 So, now I'm going to go ahead and create two more chapters. 78 79 00:06:12,340 --> 00:06:25,450 So let's just create chapter2.txt and chapter3.text. And now we have three chapters 79 80 00:06:25,720 --> 00:06:29,640 and I'm going to go in and change some of these text files. 80 81 00:06:29,680 --> 00:06:31,030 So let's say... 81 82 00:06:41,270 --> 00:06:43,710 Okay, so that's Chapter 2 done. 82 83 00:06:43,960 --> 00:06:49,330 And finally, let's go ahead and just open chapter3 and edit that as well. 83 84 00:07:00,930 --> 00:07:01,420 All right. 84 85 00:07:01,450 --> 00:07:03,720 So all three files have been changed. 85 86 00:07:03,760 --> 00:07:09,070 And over here in Finder, you can actually get a quick peek at what the contents are which is going to 86 87 00:07:09,070 --> 00:07:14,520 be really useful for me to be able to demonstrate to you what Git is doing behind the background. 87 88 00:07:14,800 --> 00:07:20,860 So, now let's go ahead and add these two new files to our staging areas. 88 89 00:07:20,860 --> 00:07:28,180 So, again, if we use Git status, you can see that there's two files that are untracked which are only in 89 90 00:07:28,180 --> 00:07:31,970 the working directory, and not yet inside the staging area. 90 91 00:07:32,290 --> 00:07:38,630 So we can put it into the staging area by simply adding each of them as we did before, 91 92 00:07:38,650 --> 00:07:42,610 "git add," and writing something like chapter2.txt, 92 93 00:07:42,630 --> 00:07:45,430 and then doing git add chapter3.text. 93 94 00:07:45,640 --> 00:07:51,940 But as you can imagine, if you have quite a few files, then it can get incredibly tedious having to do 94 95 00:07:51,940 --> 00:07:53,580 this one by one. 95 96 00:07:53,590 --> 00:07:56,070 So, of course, there is a better way. 96 97 00:07:56,080 --> 00:08:01,870 So instead of adding these files one by one, we can actually simply just say git add and then use the 97 98 00:08:01,870 --> 00:08:07,050 dot to specify everything inside this current directory. 98 99 00:08:07,060 --> 00:08:09,840 So everything inside the story directory. 99 100 00:08:10,090 --> 00:08:14,740 So, now if I go ahead and hit enter and then let's go to git status again, 100 101 00:08:14,920 --> 00:08:22,150 you can see that the two new files that have been added to the staging area. And now we're going to commit 101 102 00:08:22,270 --> 00:08:26,580 those two files to a new commit to a new save point. 102 103 00:08:26,920 --> 00:08:30,110 And you know what to do if you're following along with me, 103 104 00:08:30,130 --> 00:08:35,570 go ahead and give it a go. 104 105 00:08:35,570 --> 00:08:35,930 All right. 105 106 00:08:35,930 --> 00:08:43,430 So how was that? If you remember, the command is gt to commit and we're going to use -m flag 106 107 00:08:43,430 --> 00:08:51,920 to specify a commit message, and we're going to write a message that is in the present tense. So let's 107 108 00:08:51,920 --> 00:08:58,120 say, "complete chapter 2 and 3." 108 109 00:08:58,940 --> 00:09:05,590 So that's everything I've done between the initial commit and this commit. 109 110 00:09:05,600 --> 00:09:10,230 The only difference is the fact that I've completed now Chapter 2 and Chapter 3. 110 111 00:09:10,250 --> 00:09:13,170 So let's go ahead and and hit enter. 111 112 00:09:13,550 --> 00:09:17,060 So, again, let's check it out using Git log. 112 113 00:09:17,120 --> 00:09:24,170 We can see that we now have two commits, both with different hashes because they are unique and they 113 114 00:09:24,170 --> 00:09:25,230 are different. 114 115 00:09:25,280 --> 00:09:32,990 So the initial one was "Complete Chapter 1" and it was done at this time. And then later on, about five 115 116 00:09:32,990 --> 00:09:36,100 minutes later, I completed Chapter 2 and 3. 116 117 00:09:36,170 --> 00:09:38,110 And that was the second commit. 117 118 00:09:38,480 --> 00:09:41,230 And this is where we are at right now. 118 119 00:09:41,270 --> 00:09:49,480 So you can see by the HEAD, by this word "HEAD," this is the position or the current state that we are in. 119 120 00:09:49,490 --> 00:09:52,720 So I just want to quickly recap what we've just done. 120 121 00:09:52,970 --> 00:09:58,440 So we created a file in our working directory inside our story directory. 121 122 00:09:58,630 --> 00:10:05,990 So the working directory is the folder or the directory where you initialize your Git repository. 122 123 00:10:06,100 --> 00:10:10,390 When we said "git init," we did that inside the story directory, 123 124 00:10:10,460 --> 00:10:12,540 so that becomes a working directory. 124 125 00:10:12,740 --> 00:10:18,890 And from now on, Git is going to try and track the changes that it sees between the working directory 125 126 00:10:19,310 --> 00:10:22,090 and the local repository. 126 127 00:10:22,490 --> 00:10:31,040 So in the beginning, we created a file inside our working directory, inside the story, and then we used 127 128 00:10:31,040 --> 00:10:34,310 "git add" to push it to the staging area. 128 129 00:10:34,310 --> 00:10:39,740 Now, the reason why there is this intermediate staging area because you might wonder, why not just go 129 130 00:10:39,740 --> 00:10:42,470 from the working directory straight to the repository, 130 131 00:10:42,500 --> 00:10:44,690 why do we need this extra step? 131 132 00:10:44,840 --> 00:10:52,430 Well, sometimes you might not want to add all of your files to be tracked or all of your files to be 132 133 00:10:52,430 --> 00:10:53,550 committed. 133 134 00:10:53,600 --> 00:10:59,240 So the staging area is a good place to try and figure out what are the things that you want Git to ignore 134 135 00:10:59,390 --> 00:11:01,970 and what are the things that you want to be tracked. 135 136 00:11:02,210 --> 00:11:09,200 So once we've used "git add," we've put out file into the staging area and we're happy with the changes 136 137 00:11:09,200 --> 00:11:17,160 that we are going to commit, then the next step is to go ahead and commit it using the "git commit" command. 137 138 00:11:17,180 --> 00:11:25,730 So, now our file is inside our local repository, so that .git, and that version is given a name through 138 139 00:11:25,730 --> 00:11:27,340 the commit message. 139 140 00:11:27,350 --> 00:11:35,540 So that means that even if we've messed up our file, we can still use the last version that's under version 140 141 00:11:35,540 --> 00:11:43,820 control and we can use a special command called "git checkout" to revert back or roll back to the last 141 142 00:11:43,820 --> 00:11:46,200 position in our local repository. 142 143 00:11:46,370 --> 00:11:52,080 So let me show you what that looks like in the command line and how we would do that in practice. 143 144 00:11:52,100 --> 00:11:57,410 So at the moment, I've got three nicely written chapters, and I have a feeling my book is going to be 144 145 00:11:57,410 --> 00:11:58,430 a big seller. 145 146 00:11:58,450 --> 00:12:06,530 So, now let's say that I have, you know, I've been working on Chapter 3 and I have completely messed 146 147 00:12:06,530 --> 00:12:13,910 everything up, and I'm just, you know, fell asleep on my keyboard, and I happened to have saved my file. 147 148 00:12:13,910 --> 00:12:20,140 And now if you have a look at it, it's now just mumbo jumbo and I've ruined my masterpiece. 148 149 00:12:20,360 --> 00:12:25,170 But fear not because we have version control and we have Git enabled, 149 150 00:12:25,370 --> 00:12:28,270 so we have nothing to worry about. 150 151 00:12:28,610 --> 00:12:33,750 I can actually revert the changes that I've made locally in my working directory. 151 152 00:12:33,770 --> 00:12:39,290 So at this point, you can use "gIt status" to see that we have modifications in our chapter3.txt file 152 153 00:12:39,280 --> 00:12:44,780 that have not yet been committed or added to the staging area. 153 154 00:12:44,840 --> 00:12:50,920 So if we wanted to, we can actually revert this back to its previous glory. 154 155 00:12:51,230 --> 00:12:57,920 But before we do that, we can use a Git command to check out what are the differences between the current 155 156 00:12:57,920 --> 00:13:03,100 version of Chapter 3 and the last save point in our Git repository. 156 157 00:13:03,230 --> 00:13:06,860 So to do that, you can use the command "git diff," 157 158 00:13:06,950 --> 00:13:12,350 so the difference, and we'll give it the chapter3 file name. 158 159 00:13:12,650 --> 00:13:17,210 And if you hit enter, you can see that this is the part that was deleted, 159 160 00:13:17,210 --> 00:13:23,860 so the part in red. And then this was the part that was added which is our gobbledygook. 160 161 00:13:24,050 --> 00:13:29,600 So, now if I've looked at these differences, it might just be that, you know, there's only a few mistakes 161 162 00:13:29,930 --> 00:13:36,480 in my new version of Chapter 3 and I just want to maybe copy some things over or have a look at how I did 162 163 00:13:36,480 --> 00:13:41,390 certain things previously, and change my current file. 163 164 00:13:41,700 --> 00:13:46,910 But other times, it might be that, you know, it's just you want to torch the new file, 164 165 00:13:46,920 --> 00:13:49,120 you just don't want anything to do with it, 165 166 00:13:49,140 --> 00:13:52,710 and you would much rather roll back to the previous version. 166 167 00:13:52,980 --> 00:13:59,370 So if you want to do that, then there is a command called "git checkout" that is going to be really, really 167 168 00:13:59,370 --> 00:14:00,510 useful for you. 168 169 00:14:00,810 --> 00:14:03,190 So git checkout. 169 170 00:14:03,210 --> 00:14:07,920 And then we're going to specify the name of the file that we want to check out which is chapter3.txt. 170 171 00:14:07,920 --> 00:14:14,170 And if you just watch over here which is the preview of the current version of chapter3.txt, 171 172 00:14:14,170 --> 00:14:23,070 once I hit enter on this command that basically asks to roll back this Chapter 3 to the last version 172 173 00:14:23,070 --> 00:14:26,550 that was committed in our local repository. 173 174 00:14:26,550 --> 00:14:33,900 So if I hit enter, you can see that almost immediately my Chapter 3 has been restored to its previous 174 175 00:14:34,440 --> 00:14:41,610 glorious state, and this is the version of chapter 3 at the last checkpoint at which I committed it. 175 176 00:14:41,640 --> 00:14:46,000 So that was this one which is completed Chapter 2 and 3. 176 177 00:14:46,020 --> 00:14:51,770 So whereas in this lesson, we've looked mostly at local implementations of Git, 177 178 00:14:51,840 --> 00:14:58,460 so saving these versions on our computer locally. In the next lesson, I'm going to talk about GitHub 178 179 00:14:58,830 --> 00:15:02,580 and creating remote repositories. So I'll see you there.